graphql.js:即使解析器返回sequilize查询的值,该字段也为null

时间:2018-02-24 23:59:12

标签: node.js sequelize.js graphql-js apollo-server

我有以下graphql架构,我正在尝试执行businessUser查询

type Query {
  businessUser(id: String!): BusinessUser
}

type BusinessUser {
  id: ID!,
  firstName: String,
  lastName: String,
  email: String,
  posts: [BusinessPostFormat]
}

type BusinessPostFormat {
  postId: Int,
  user: String,
  isActive: Boolean,
  postText: String
}

在我的解析器中,我使用businessPost表加入Post表,以检索'isActive'列。当我打印出Sequilize返回的内容时,我发现我正在准确地找回应该映射到BusinessPostFormat类型的数据对象:

post {
    dataValues: 
     { postId: '188',
       postText: 'blah blah blah',
       user: 'userName',
       isActive: false },

然而,当我在graphiql中执行查询时,isActive字段将返回null。我不明白为什么会发生这种情况,如果我能看到解析器正在返回BusinessPostFormat类型所期望的那个?

下面的解析器功能:

const resolvers = {
  Query: {
    businessUser(_, args) {
      return BusinessUser.findById(args.id);
    }
},
BusinessUser: {
    posts(businessUser) {
      return businessUser.getPosts({
        attributes: {
          include: [[Sequelize.literal('business_posts.is_active'), 'isActive']],
          exclude: ['business_user_id']
        },
        include: [{ model: BusinessPost, attributes: ['is_active'] }]
      })
    },
  }

1 个答案:

答案 0 :(得分:1)

也许你错过了Sequelize使用实例http://docs.sequelizejs.com/manual/tutorial/instances.html的事实,在这种情况下,这可能是缺失的事实。 试试吧。

return Entries.findAll().then(entries => {
  return entries.map(e => e.get({ plain: true }));
})