【问题标题】:GraphQL Apollo schema delegation: info.mergeInfo is undefinedGraphQL Apollo 模式委托:info.mergeInfo 未定义
【发布时间】:2019-10-06 00:42:26
【问题描述】:

我遵循了官方的doc to delegate a graphql schema,这表明要这样做,必须使用delegateSchema 方法,该方法可以在传递给解析器的参数info 的属性mergeInfo 上找到:

resolver: (parent, args, ctx, info) => {
  return info.mergeInfo.delegateSchema({
    // Schema delegation options...
  })
}

但是info 参数上没有属性mergeInfo!所以我收到这条错误消息:GraphQL Error GraphQL error: Cannot read property 'delegateToSchema' of undefined,考虑到这些是info 的顶级属性,这是正常的:

console.log(Object.keys(info))
[
  'fieldName',
  'fieldNodes',
  'returnType',
  'parentType',
  'path',
  'schema',
  'fragments',
  'rootValue',
  'operation',
  'variableValues',
  'cacheControl'
]

看起来mergeInfo 甚至没有在type definition of the GraphQLResolveInfo object 中提及

文档过时了还是我遗漏了什么?

谢谢

【问题讨论】:

    标签: graphql apollo


    【解决方案1】:

    mergeInfo 仅在您使用 mergeSchemas 将两个或多个模式拼接在一起时注入到 info 对象中。此外,它将注入到您作为模式拼接的一部分添加的字段的解析器中——模式的解析器不会受到影响(委托实际上没有意义 在现有模式之一的上下文中的另一个模式)。

    这是一个简单的例子:

    const { makeExecutableSchema, mergeSchemas } = require('graphql-tools')
    
    const schemaA = makeExecutableSchema({
      typeDefs: `
        type Query {
          foo: String
        }
      `,
    })
    const schemaB = makeExecutableSchema({
      typeDefs: `
        type Query {
          bar: String
        }
      `,
    })
    const typeDefs = `
      extend type Query {
        foobar: String
      }
    `
    const schema = mergeSchemas({
      schemas: [schemaA, schemaB, typeDefs],
    })
    

    这里,foobar 的解析器将无法访问 mergeInfo,但 foobar 的解析器将。

    【讨论】:

    • 看起来 graphql-tools 的 v6 没有在 schemas 数组中使用 typeDefs,而是在一个新的 typeDefs 键中接受它。你见过这个吗,你知道它是如何与 mergeInfo 交互的吗?
    猜你喜欢
    • 2021-04-20
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2021-12-23
    • 2019-03-10
    • 2019-04-22
    • 2021-03-25
    相关资源
    最近更新 更多