Hasura GraphQL Endpoint behind Apollo Federated Gateway
Asked Answered
V

2

6

Has anyone successfully placed a Hasura GraphQL endpoint, behind an Apollo Federated Gateway? I know Hasura wants to act as the point of federation but I would rather not do that...current thought is to create an apollo server with a remote schema to connect Hasura, and then but that beind the gateway...looking for any thoughts or guidance on whether this is possible?

I'm tempted to say it's not because I can't see anyone who has attempted it. I'm not sure if the Hasura endpoint will allow. "itself" to be federated in this way.

I've started the process but intially haven't been able to get an Express Apollo Server with a remote schemea to connect to the Hasura endpoint so a smaller question is whether or not that is even possible.

Cheers.

Virendra answered 9/10, 2019 at 12:6 Comment(0)
S
8

It is possible to mount Hasura over apollo-gateway [1] using a simple workaround. The fundamental requirement is to have a field called _service in your graphql schema [2]. The _service field is nothing but the Hasura schema in the Schema Definition Language (SDL) format.

You can add this field to your query type using a Remote Schema [3]. Here is a sample remote schema:

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

The key value here is const hasuraSchema which is the Hasura schema in the SDL format i.e.

// 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;

You can get the SDL of the Hasura schema automatically using many community tools including graphql-js [4] or graphqurl [5].

A completely automated example is posted here: https://gist.github.com/tirumaraiselvan/65c6fa80542994ed6ad06fa87a443364

NOTE: apollo-gateway doesn't currently support subscriptions [6] so you will need to remove the subscription field from the schema root in the generated SDL, otherwise it throws a weird error.

  1. This only allows serving Hasura via apollo-gateway and does not mean it enables federation capabilities.
  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
Sorcery answered 10/10, 2019 at 6:3 Comment(3)
This is so helpful, thank you so much. I will try this soon. Theorectically, could you use this apollo server to then create a remote schema, that could then be used behind an apollo federated server?Virendra
could you use this apollo server to then create a remote schema, that could then be used behind an apollo federated server? I am not 100% sure what you mean here. In the solution above, you add the apollo-server that is shown as a remote schema in Hasura. And then mount Hasura in apollo-gateway.Sorcery
I understand yep, apologies, maybe I should explain myself better. I want to achieve: Hasura -> Apollo Server -> Apollo Federated Gateway -> Client ...I think that's now possible based on what you shared?Virendra
S
0

Since v2.10, you can now add Hasura behind an Apollo Federation gateway natively. See the docs here: https://hasura.io/docs/latest/data-federation/apollo-federation/

Sorcery answered 11/8, 2022 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.