【问题标题】:Looking to use delegateToSchema in nestjs resolver希望在 nestjs 解析器中使用 delegateToSchema
【发布时间】:2020-05-16 04:15:55
【问题描述】:

我有一个本地架构和一个远程架构。 本地架构包含远程架构的子集,并且应该将这些代理到远程 graphql 服务器。

通常我会使用类似的东西:

public async getCar(
  obj: any,
  args: any,
  context: any,
  info: any
): Promise<Car> {
  return info.mergeInfo.delegateToSchema({
    args: {
      id: context.userId,
    },
    context: {},
    fieldName: 'car',
    info,
    operation: 'query',
    schema: carExecutableSchema,
    transforms: [],
  });
}

mergeInfoinfo 字段中不可用。

要获取我正在使用的远程架构:

const fetchRemoteSchema = async (): Promise<GraphQLSchema> => {
  const http = new HttpLink({
    fetch: nodeFetch,
    uri: process.env.CAR_GRAPHQL_URL,
  });

  const link = setContext((request, previousContext) => ({
    headers: {
      Authorization: `Basic ${process.env.CAR_BASIC_AUTH_TOKEN}`,
    },
  })).concat(http);

  const carSchema = await introspectSchema(link);

  const executableSchema = makeRemoteExecutableSchema({
    link,
    schema: carSchema,
  });
  return executableSchema;
};

我在模块中使用它来初始化graphql

GraphQLModule.forRootAsync({
    imports: [CarModule],
    inject: [CarService],
    useFactory: (mainAppService: MainAppService) => ({
      context: async ({ req }) => {
        return {
          userId: 1
        };
      },
      definitions: {
        outputAs: 'interface',
        path: '../../myschema.gql',
      },
      typePaths: ['./**/*.graphql'],
    }),
  }),

我怎样才能使mergeInfo 可用?还是有更简单的方法来委派 graphql 请求?

【问题讨论】:

    标签: node.js express graphql nestjs


    【解决方案1】:

    info.mergeInfo 仅在使用 mergeSchemas([]) 创建架构时可用。您可以将delegateToSchema 作为函数导入:import { delegateToSchema } from "apollo-server-express";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多