简体   繁体   English

graphql.js:即使解析程序返回值以进行静态化查询,字段也为空

[英]graphql.js: field is null even though resolver is returning value for sequilize query

I have the following graphql schema and I am trying to execute businessUser query 我具有以下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
}

In my resolver I am joining my Post table with my businessPost table to retrieve a 'isActive' column. 在我的解析器中,我将Post表与businessPost表联接在一起以检索“ isActive”列。 When I print out what Sequilize is returning I see that I am getting back exactly the data object that should map to the BusinessPostFormat type: 当我打印出Sequilize返回的内容时,我看到我正在准确地获取应该映射到BusinessPostFormat类型的数据对象:

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

However when I execute the query in graphiql the isActive field is coming back as null. 但是,当我在graphiql中执行查询时,isActive字段返回为null。 I dont understand why thats happening if I can see that the resolver is returning exactly what BusinessPostFormat type is expecting? 如果我看到解析器返回的正是BusinessPostFormat类型的期望值,我不明白为什么会这样?

resolver function below: 解析器功能如下:

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'] }]
      })
    },
  }

Maybe you are missing the fact that Sequelize uses instances http://docs.sequelizejs.com/manual/tutorial/instances.html and maybe that is the missing fact in this case. 也许您错过了Sequelize使用实例http://docs.sequelizejs.com/manual/tutorial/instances.html的事实,也许在这种情况下这是缺少的事实。 Try it like this. 这样尝试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM