Skip to content

Instantly share code, notes, and snippets.

@ManUtopiK
Forked from rmarscher/fetch-graphql-schema.js
Last active September 1, 2019 22:06
Show Gist options
  • Select an option

  • Save ManUtopiK/57ee2df06ce8830627d4dfc28e632053 to your computer and use it in GitHub Desktop.

Select an option

Save ManUtopiK/57ee2df06ce8830627d4dfc28e632053 to your computer and use it in GitHub Desktop.

Revisions

  1. ManUtopiK revised this gist Sep 1, 2019. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion fetch-graphql-schema.js
    Original file line number Diff line number Diff line change
    @@ -30,5 +30,16 @@ const graphql = new ApolloClient({
    });

    graphql.query({ query }).then((result) => {
    console.log(JSON.stringify(result, null, ' '));
    // 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));
  2. @rmarscher rmarscher created this gist Sep 16, 2016.
    34 changes: 34 additions & 0 deletions fetch-graphql-schema.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // 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) => {
    console.log(JSON.stringify(result, null, ' '));
    }).catch((err) => console.error(err));