我有一个本地模式和一个远程模式。本地模式包含远程模式的子集,应将其代理到远程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: [],
});
}
但是mergeInfo
字段上没有info
。
要获取我正在使用的远程模式:
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请求?
info.mergeInfo
仅在使用mergeSchemas([])
创建架构时可用。您可以通过以下方式将delegateToSchema
导入为函数:import { delegateToSchema } from "apollo-server-express";