【发布时间】:2019-07-27 14:04:32
【问题描述】:
我创建了一个使用远程模式的 ApolloServer。远程模式需要一个授权令牌,我可以从请求中获取到我的阿波罗服务器。这是阿波罗服务器的代码。我可以在 customFetch 函数中对令牌进行硬编码,一切正常,但我想将调用时获得的令牌传递给我的服务器。
import {makeRemoteExecutableSchema, introspectSchema, mergeSchemas} from 'graphql-tools';
import {HttpLink} from 'apollo-link-http';
import {ApolloServer} from 'apollo-server';
import fetch from 'node-fetch';
// create executable schemas from remote GraphQL APIs
const customFetch = (uri, options) => {
// How do I set the token from my server req??
options.headers.Authorization =
'Bearer eyJhbGciOiJIUzI1NiIsI Not A Real Token kf5iOg9SkxDBVtQnLJuz3hXEDA';
return fetch(uri, options);
};
const createRemoteExecutableSchemas = async () => {
let schemas = [];
const link = new HttpLink({
uri: 'http://remote.graphql.server:5555/graphql',
fetch: customFetch,
});
const remoteSchema = await introspectSchema(link);
const remoteExecutableSchema = makeRemoteExecutableSchema({
schema: remoteSchema,
link,
});
schemas.push(remoteExecutableSchema);
return schemas;
};
const createNewSchema = async () => {
const schemas = await createRemoteExecutableSchemas();
return mergeSchemas({
schemas,
});
};
const runServer = async () => {
const schema = await createNewSchema();
const server = new ApolloServer({
schema
});
server.listen().then(({url}) => {
console.log(`???? Server ready at ${url}`);
});
};
try {
runServer();
} catch (err) {
console.error(err);
}
【问题讨论】:
标签: apollo-server