0

我有一个拼接的 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,
          });
        },
      },
    },
});

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

4

1 回答 1

0

info.mergeInfo.delegateToSchema后台可以调用远程 GraphQL 应用程序(更多细节)。

因此,根据设计,远程解析器无权访问本地根/上下文/信息/参数,您需要在远程字段的参数中发送所有必需的数据。例如:

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

我不知道你的情况,但在使用删除模式时不要忘记模式转换。

于 2019-03-08T11:47:50.723 回答