我想将我的 graphql 服务中的一个字段委托给远程服务。我试图遵循文档,但我遗漏了一些东西。
const {wrapSchema, introspectSchema} = require('@graphql-tools/wrap');
const got = require('got');
const {print} = require('graphql');
const {delegateToSchema} = require('graphql-tools');
const executor = async ({document, variables}) => {
const query = typeof document === 'string' ? document : print(document);
return got.post('http://localhost:6677/graphql', {json: {query, variables}).json();
};
const makeRemoteSchema = async () => {
return wrapSchema({
schema: await introspectSchema(executor),
executor
});
};
const conversations = async ({id}, _, context, info) => {
const result = await delegateToSchema({
schema: await makeRemoteSchema(),
operation: 'query',
fieldName: 'conversations',
args: {
consultationParticipantId: id,
},
context,
info
});
return result;
};
结果是null
。我可以看到它正在使用我的执行程序进行自省调用,通过解析字段时它使用默认执行程序,这基本上是一个无操作。通过单步执行delegateToSchema
代码,它似乎想要schema
成为一个schemaConfig
. 所以我尝试wrapSchema
像这样删除:
const makeRemoteSchema = async () => {
return {
schema: await introspectSchema(executor),
executor
};
}
它使用我的执行程序而不是默认执行程序,但现在在执行程序中,query
被解析为空字符串,并且variables
是一个空对象。
你能看出我做错了什么吗?
我在用着:
"graphql-tools": "^7.0.4",
"@graphql-tools/wrap": "^7.0.8",
我也试过v6.2.4。