我希望联合服务,但让联邦网关也包含自己的模式和逻辑,这些模式和逻辑将代理REST端点以实现简单性。现在看来,我需要联邦网关服务、联邦graphql服务和其余的<->graphql桥服务。无论如何,在我们的例子中,rest-graphql网关至少暂时可以生活在联邦网关中,以避免不必要的引导和维护。
看起来阿波罗联邦网关有localServiceList,似乎正是为了这个目的。一个示例配置:
const gateway = new ApolloGateway({
serviceList: [
{ name: "some-service", url: "http://localhost:40001/graph" }
],
localServiceList: [
{ name: "rest-bridge", typeDefs }
]
});但是它并没有做到这一点:如果有localServiceList,它跳过serviceList。
,所以问题是:在阿波罗联邦网关中,这也有可能保存自己的模式和逻辑吗?
发布于 2020-05-06 13:58:42
是的,可以这样做:
import { buildFederatedSchema } from '@apollo/federation';
import {
ApolloGateway,
LocalGraphQLDataSource,
RemoteGraphQLDataSource
} from '@apollo/gateway';
import gql from 'graphql-tag';
const localServices = {
foo: {
schema: {
typeDefs: gql`
// ...
`,
resolvers: {
// ...
}
}
},
bar: {
schema: {
typeDefs: gql`
// ...
`,
resolvers: {
// ...
}
}
}
};
const remoteServices = {
baz: {
url: 'http://baz.local/graphql'
},
qux: {
url: 'http://qux.local/graphql'
}
};
const services = {
...localServices,
...remoteServices
};
// By providing a protocol we trick ApolloGateway into thinking that this is a valid URL;
// otherwise it assumes it's a relative URL, and complains.
const DUMMY_SERVICE_URL = 'https://';
const gateway = new ApolloGateway({
// We can't use localServiceList and serviceList at the same time,
// so we pretend the local services are remote, but point the ApolloGateway
// at LocalGraphQLDataSources instead...
serviceList: Object.keys(services).map(name => ({
name,
url: services[name].url || DUMMY_SERVICE_URL
})),
buildService({ name, url }) {
if (url === DUMMY_SERVICE_URL) {
return new LocalGraphQLDataSource(
buildFederatedSchema(
services[name].schema
)
);
} else {
return new RemoteGraphQLDataSource({
url
});
}
}
});
const apolloServer = new ApolloServer({
gateway,
subscriptions: false
});https://stackoverflow.com/questions/57304658
复制相似问题