过去几天我一直在研究 Graphql,同时为一个新项目做一些技术调查。
实际上,我有多个网站,它们具有通用的 Rest API 实现和架构。
理想情况下,我希望能够独立查询它们以获取每个站点的客户和订单等信息,但我也希望能够将结果合并到单个端点中。
作为概念证明,我创建了一个带有 RestDataSource 的 Apollo 服务器,以提供一个 Graphql 端点来包装 Rest API。
我想我正在努力解决的是如何将其扩展到多个站点和最佳应用程序架构。
我是否应该为每个站点创建一个单独的 Apollo Server,即
http://localhost/site1:4000
http://localhost/site2:4000
http://localhost/global:4000
然后我将在应用程序中创建:
/server/site1/
/server/site2/
/server/global
/common/typedefs
/common/resolvers
拥有多个服务器配置感觉好像有很多重复的工作。
有什么想法吗?
同样,任何有关将端点合并在一起的建议都将不胜感激
当前代码如下所示:
//pull in the env configuration
require('dotenv').config();
//pull in application specific configuration
var config = require('./config.json');
//Apollo Server and GraphQL
const { ApolloServer, gql } = require('apollo-server');
var { graphql, buildSchema } = require('graphql');
//Prospect API
const { ProspectAPI } = require('./prospectapi');
//Define the GraphQL schema for prospects
const typeDefs = gql`
type Prospect {
date_created_gmt: String
first_name: String
last_name: String
email: String
billing: Address
meta_data: [Metadata]
}
type Address {
address1: String
address2: String
city: String
postcode: String
country: String
state: String
phone: String
}
type Metadata {
id: String
key: String
value: String
}
type Query {
prospectByEmail(email: String!): [Prospect]
prospectByID(id: String!): Prospect
latestProspects: [Prospect]
}
`;
//define the resolvers
const resolvers = {
Query: {
prospectByEmail: async (_, { email }, { dataSources }) => {
return dataSources.prospectAPI.getProspectByEmail(email);
},
prospectByID: async (_, { id }, { dataSources }) => {
return dataSources.prospectAPI.getProspectByID(id);
},
latestProspects: async (_, __, { dataSources }) => {
return dataSources.prospectAPI.getLatestProspects();
},
},
};
//instantiate the apollo server
const server = new ApolloServer({
cors: true,
typeDefs,
resolvers,
dataSources: () => {
return {
prospectAPI: new ProspectAPI(config.endpoint, config.user, config.pass)
};
},
});
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`? Server ready at ${url}`);
});
答案 0 :(得分:0)
更多的 Google Foo 将我引向阿波罗联盟和子图,看起来它可以解决问题