【问题标题】:Apollo: passing root to resolver with info.mergeInfo.delegateToSchemaApollo:使用 info.mergeInfo.delegateToSchema 将 root 传递给解析器
【发布时间】:2019-07-26 23:04:33
【问题描述】:

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

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

【问题讨论】:

    标签: graphql apollo graphql-tools


    【解决方案1】:

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

    因此,根据设计,远程解析器无权访问本地根/上下文/信息/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。

    【讨论】:

      猜你喜欢
      • 2020-06-30
      • 2019-11-06
      • 2022-07-20
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 2021-01-22
      相关资源
      最近更新 更多