【问题标题】:Graphql: Multiple Apollo Servers and Merged ResultsGraphql:多个 Apollo 服务器和合并结果
【发布时间】:2021-08-21 16:39:07
【问题描述】:

过去几天我一直在研究 Graphql,同时为一个新项目做一些技术调查。

实际上,我有多个网站,它们具有通用的 Rest API 实现和架构。

理想情况下,我希望能够独立查询它们以获取每个站点的客户和订单等信息,但我还希望能够将结果整合到单个端点。

作为概念证明,我创建了一个带有 RestDataSource 的 Apollo 服务器,以提供 Graphql 端点来包装 Rest API。

我想我正在努力解决的问题是如何将其扩展到多个站点和最佳应用程序架构。

我是否应该为每个站点创建一个单独的 Apollo 服务器,即

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}`);
    });

【问题讨论】:

    标签: node.js graphql apollo-server


    【解决方案1】:

    更多 Google Foo 将我带到 Apollo Federation 和子图,看起来它可以解决问题

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 2018-01-05
      • 1970-01-01
      • 2019-03-01
      • 2020-11-08
      • 2021-10-04
      • 2017-11-07
      • 2021-12-01
      • 2019-12-19
      相关资源
      最近更新 更多