-
-
Save schettino/53203acfa0c41094fa8768d586081bc5 to your computer and use it in GitHub Desktop.
Mount Hasura on Apollo federated gateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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