阿波罗:使用info.mergeInfo.delegateToSchema将根传递给解析器

时间:2019-03-05 13:03:51

标签: graphql apollo graphql-tools

我有一个已缝合的graphql模式。某些类型字段使用info.mergeInfo.delegateToSchema

解析

下面是一个示例(来自apollo docs):

const mergedSchema = mergeSchemas({
  schemas: [
    transformedChirpSchema,
    authorSchema,
    linkTypeDefs,
  ],
  resolvers: {
    User: {
      chirps: {
        fragment: `... on User { id }`,
        resolve(user, args, context, info) {
          return info.mergeInfo.delegateToSchema({
            schema: chirpSchema,
            operation: 'query',
            fieldName: 'chirpsByAuthorId',
            args: {
              authorId: user.id,
            },
            context,
            info,
          });
        },
      },
    },
});

是否可以在root解析器中访问chirps?这样在root中有所有父字段吗?当然,另一种方法是为此目的使用context,但是从代码角度来看,我想使用root会更好,因为在某些情况下我已经使用root值。

1 个答案:

答案 0 :(得分:0)

内幕info.mergeInfo.delegateToSchema可以调用远程GraphQL应用程序(更多details)。

因此,根据设计,远程解析器无权访问本地root / context / info / arg,因此需要在用于远程字段的参数中发送所有必需的数据。例如:

const mergedSchema = mergeSchemas({
  schemas: [
    transformedChirpSchema,
    authorSchema,
    linkTypeDefs,
  ],
  resolvers: {
    User: {
      chirps: {
        fragment: `... on User { id }`,
        resolve(user, args, context, info) {
          return info.mergeInfo.delegateToSchema({
            schema: chirpSchema,
            operation: 'query',
            fieldName: 'chirpsByAuthorId',
            args: {
              // author is InputType at remove schema with similar user structure
              author: user,
            },
            context,
            info,
          });
        },
      },
    },
});

我不知道您的情况,但在使用删除架构时不会忘记schema-transforms

相关问题