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.
- This only allows serving Hasura via apollo-gateway and does not mean it enables federation capabilities.
- https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#fetch-service-capabilities
- https://docs.hasura.io/1.0/graphql/manual/remote-schemas/index.html
- https://graphql.org/graphql-js/utilities/#printintrospectionschema
- https://github.com/hasura/graphqurl#export-schema
- https://github.com/apollographql/apollo-server/issues/2360#issuecomment-531849628