Search code examples
graphqlapollo

GraphQL Apollo schema delegation: info.mergeInfo is undefined


I followed the official doc to delegate a graphql schema which indicates that in order to do so, one must use the method delegateSchema that can be found on the property mergeInfo of the argument info passed down to resolvers:

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

But there is no property mergeInfo on the info argument ! And so I get this error message: GraphQL Error GraphQL error: Cannot read property 'delegateToSchema' of undefined which is normal considering these are the top-level properties of info:

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

It even looks like that mergeInfo is not mentioned in the type definition of the GraphQLResolveInfo object

Is the doc outdated or am I missing something ?

Thanks


Solution

  • mergeInfo is only injected into the info object when you use mergeSchemas to stitch together two or more schemas. Additionally, it will only be injected into the resolvers for fields you add as part of the schema stitching -- resolvers for the schemas will not be impacted (it doesn't really make sense to delegate to another schema in the context of one of the existing schemas anyway).

    Here's a simple example:

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

    Here, the resolvers for foo and bar will not have access to mergeInfo, but the resolver for foobar will.