Skip to content

Instantly share code, notes, and snippets.

@schettino
Forked from tirumaraiselvan/shim.js
Created February 20, 2020 20:31
Show Gist options
  • Save schettino/53203acfa0c41094fa8768d586081bc5 to your computer and use it in GitHub Desktop.
Save schettino/53203acfa0c41094fa8768d586081bc5 to your computer and use it in GitHub Desktop.
Mount Hasura on Apollo federated gateway
const { ApolloServer } = require("apollo-server");
const gql = require("graphql-tag");
const fetch = require("node-fetch");
const {
introspectionQuery,
buildClientSchema,
printSchema
} = require("graphql");
const typeDefs = gql`
schema {
query: query_root
}
type _Service {
sdl: String
}
type query_root {
_service: _Service!
}
`;
const hasuraURL = "https://somehasuraurl.com/v1/graphql";
async function getHasuraSchema() {
return await fetch(hasuraURL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
query: introspectionQuery
})
})
.then(res => res.json())
.then(introspectionJSON => {
delete introspectionJSON.data.__schema.subscriptionType; // apollo-gateway does not support subscriptions as of now and having subscription type throws a wierd error!
return printSchema(buildClientSchema(introspectionJSON.data));
});
}
const resolvers = {
query_root: {
_service: async () => {
var hasuraSchema = await getHasuraSchema();
return { sdl: hasuraSchema };
}
}
};
const schema = new ApolloServer({ typeDefs, resolvers });
schema.listen({ port: process.env.PORT }).then(({ url }) => {
console.log(`schema ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment