从 Apollo 插件访问 graphql 解析器参数



我正在尝试向我的ApolloServer graphql API添加一个插件,该插件使用传入的参数作为键来记录端点的输出。

但是,当进行查询时,它看起来像

query($x: String!, $y: String!, $z: String!) {
foo(user: { a: $x, b: $y, c: $z }) {
bar
}
}

该插件看起来像

{
requestDidStart() {
return {
willSendResponse({ response }: {response: GraphQLResponse }) {
console.log(`user ${ARGS.A} has made a request`);
console.log('response sent to user': response);
},
}
}
}

我尝试从请求中访问变量,但鉴于变量可以使用客户端想要的任何名称,这似乎是一个死胡同。有没有更好的方法来访问插件内部的args

这样,您就可以在到达解析器之前访问变量。

async requestDidStart() {
return {
async didResolveOperation({ operation, request, contextValue }) {
console.log(request.variables)
/*
{
a: "blabla",
b: "blabla"
.
.
}
*/   
},
};
},

最新更新