-
-
Save ManUtopiK/57ee2df06ce8830627d4dfc28e632053 to your computer and use it in GitHub Desktop.
Get root schema of graphql endpoint
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
// A script that can pull down the result of the introspection query | |
// from a running graphql server. | |
// Dependencies: | |
// npm i -S isomorphic-fetch graphql-tag graphql apollo-client | |
// Usage: | |
// node fetch-graphql-schema [graphql url] | |
// Example: | |
// node fetch-graphql-schema https://example.com/graphql > graphql-schema.js | |
// Using require instead of import so we don't need to transpile for NodeJS 6.x | |
require('isomorphic-fetch'); | |
const parse = require('graphql-tag/parser').parse; | |
const introspectionQuery = require('graphql/utilities/introspectionQuery').introspectionQuery; | |
const ApolloPkg = require('apollo-client'); | |
const { | |
createNetworkInterface, | |
addTypename, | |
} = ApolloPkg; | |
const ApolloClient = ApolloPkg.default; | |
const GRAPHQL_URL = process.argv.slice(-1)[0]; | |
const query = parse(introspectionQuery); | |
const graphql = new ApolloClient({ | |
networkInterface: createNetworkInterface(GRAPHQL_URL), | |
queryTransformer: addTypename, | |
}); | |
graphql.query({ query }).then((result) => { | |
// print raw | |
console.log('raw', JSON.stringify(result, null, ' ')); | |
// queries | |
console.log('query', result.__schema.types.find(type => type.name === "query_root")) | |
// mutations | |
console.log('mutation', result.__schema.types.find(type => type.name === "mutation_root")) | |
// subscriptions | |
console.log('subscription', result.__schema.types.find(type => type.name === "subscription_root")) | |
}).catch((err) => console.error(err)); |
Author
ManUtopiK
commented
Sep 1, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment