【问题标题】:Hasura GraphQL Endpoint behind Apollo Federated GatewayApollo 联合网关背后的 Hasura GraphQL 端点
【发布时间】:2020-02-06 18:28:47
【问题描述】:

是否有人成功地将 Hasura GraphQL 端点放置在 Apollo 联合网关后面?我知道 Hasura 想要充当联邦点,但我宁愿不这样做......目前的想法是创建一个具有远程模式的 apollo 服务器来连接 Hasura,然后 that 在网关...寻找关于这是否可能的任何想法或指导?

我想说这不是因为我看不到任何尝试过的人。我不确定 Hasura 端点是否允许。 “本身”以这种方式联合起来。

我已经开始了这个过程,但最初无法获得一个带有远程方案的 Express Apollo 服务器来连接到 Hasura 端点,所以一个较小的问题是这是否可能。

干杯。

【问题讨论】:

    标签: graphql hasura federation apollo-federation


    【解决方案1】:

    可以使用简单的解决方法在 apollo-gateway [1] 上安装 Hasura。基本要求是在您的 graphql 模式 [2] 中有一个名为 _service 的字段。 _service 字段不过是 Schema Definition Language (SDL) 格式的 Hasura 模式。

    您可以使用远程模式 [3] 将此字段添加到您的 query 类型。这是一个示例远程架构:

    const { ApolloServer } = require('apollo-server');
    const gql = require('graphql-tag');
    const hasuraSchema = require('./schema.js');
    
    const typeDefs = gql`
    
      schema {
        query: query_root
      }
    
      type _Service {
        sdl: String
      }
    
      type query_root {
        _service: _Service!
      }
    
    `;
    
    const resolvers = {
        query_root: {
            _service: () =>  { return {sdl: hasuraSchema} },
        },
    };
    
    const schema = new ApolloServer({ typeDefs, resolvers });
    
    schema.listen({ port: process.env.PORT}).then(({ url }) => {
        console.log(`schema ready at ${url}`);
    });
    

    这里的键值是const hasuraSchema,它是 SDL 格式的 Hasura 模式,即

    // schema.js
    
    const hasuraSchema = `
    
    # NOTE: does not have subscription field
    schema {
      query: query_root
      mutation: mutation_root
    }
    
    type articles {
      id: Int!
      title: String!
    }
    
    type query_root {
     ...
    }
    
    type mutation_root {
     ...
    }
    `
    
    module.exports = hasuraSchema;
    

    您可以使用许多社区工具自动获取 Hasura 架构的 SDL,包括 graphql-js [4] 或 graphqurl [5]。

    此处发布了一个完全自动化的示例:https://gist.github.com/tirumaraiselvan/65c6fa80542994ed6ad06fa87a443364

    注意:apollo-gateway 当前不支持 subscriptions [6],因此您需要从生成的 SDL 中的 schema root 中删除 subscription 字段,否则会抛出一个奇怪的错误。

    1. 这仅允许通过 apollo-gateway 为 Hasura 提供服务,并不意味着它启用了联合功能。
    2. https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#fetch-service-capabilities
    3. https://docs.hasura.io/1.0/graphql/manual/remote-schemas/index.html
    4. https://graphql.org/graphql-js/utilities/#printintrospectionschema
    5. https://github.com/hasura/graphqurl#export-schema
    6. https://github.com/apollographql/apollo-server/issues/2360#issuecomment-531849628

    【讨论】:

    • 这很有帮助,非常感谢。我很快就会试试这个。从理论上讲,你可以使用这个 apollo 服务器创建一个远程模式,然后可以在 apollo 联合服务器后面使用它吗?
    • could you use this apollo server to then create a remote schema, that could then be used behind an apollo federated server? 我不是 100% 确定你的意思。在上面的解决方案中,您添加了在 Hasura 中显示为远程模式的 apollo-server。然后在阿波罗门户登上哈苏拉。
    • 我明白是的,抱歉,也许我应该更好地解释一下自己。我想实现:Hasura -> Apollo Server -> Apollo Federated Gateway -> Client ...我认为根据您分享的内容,现在可以实现了吗?
    猜你喜欢
    • 2023-02-11
    • 2021-08-19
    • 2020-11-05
    • 1970-01-01
    • 2020-12-21
    • 2021-11-14
    • 1970-01-01
    • 2020-01-28
    • 2020-04-04
    相关资源
    最近更新 更多