【问题标题】:GraphQL resolve GraphQLObjectTypeGraphQL 解析 GraphQLObjectType
【发布时间】:2017-09-19 13:52:55
【问题描述】:

我正在使用express-graphql 以及以下查询:

invitations {
    sent 
    received
}

架构定义(简化)如下:

const InvitationType = new GraphQLObjectType({
    name: 'InvitationType',
    description: 'Friends invitations',
    fields: {
        sent: {
            type: new GraphQLList(GraphQLString),
            description: 'Invitations sent to friends.',
            resolve() {
             return ['sentA'];
            }
        },
        received: {
            type: new GraphQLList(GraphQLString),
            description: 'Invitations received from friends.',
            resolve() {
             return ['receivedA', 'receivedB'];
            }
        }
    }
});

// Root schema
const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'RootQueryType',
      fields: {
          invitations: {
              type: InvitationType // no resolve() method here.
          }
      }
    })
});

但是,resolve 方法永远不会为 sent 和 received 字段调用。上面的查询返回:

{data: {invitations: {sent: null, received: null}}}

有没有办法解决嵌套字段(sent 和 received)而不在父字段(invitations)上定义 resolve() 方法?

【问题讨论】:

    标签: javascript express graphql


    【解决方案1】:

    这对我有用!根据GraphQL Documentation,如果resolve方法返回一个非标量,执行将继续。所以下面的代码有效:

    // Root schema
    const schema = new GraphQLSchema({
        query: new GraphQLObjectType({
          name: 'RootQueryType',
          fields: {
            invitations: {
              type: InvitationType,
              resolve: () => ({}) // Resolve returns an object.
            }
          }
        })
    });
    

    希望这会有所帮助。干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-29
      • 2019-03-02
      • 2019-08-19
      • 2018-02-08
      • 1970-01-01
      • 2019-03-13
      • 2021-11-22
      • 2017-07-02
      相关资源
      最近更新 更多