我试图通过jq-yoga api网关将jwt传递给其他graphql-yoga微服务。我似乎无法使其正常工作,并且我能找到的所有帖子都直接使用了apollo服务器。当我运行以下graphqlContext是未定义的。
async function createRemoteSchema(uri: string) {
const ContextLink = setContext((_, previousContext) => ({
headers: {
authorization: previousContext.graphqlContext.headers.authorization,
},
}));
const link = ApolloLink.from([ContextLink, new HttpLink({ uri, fetch })]);
const schema = await introspectSchema(link);
const executableSchema = makeRemoteExecutableSchema({
schema,
link,
});
return executableSchema;
}
const remoteSchemas = await Promise.all(['http://localhost:8080','http://localhost:8081'].map(createRemoteSchema));
const schema = mergeSchemas({
schemas: [
...remoteSchemas,
makeExecutableSchema({
typeDefs,
resolvers
}),
],
});
const graphql = new GraphQLServer({
schema,
context: ({ request }) => ({
headers: request ? request.headers : null
})
});