Skip to content

Instantly share code, notes, and snippets.

@arjun-kava
Created January 22, 2019 06:33
Show Gist options
  • Save arjun-kava/0bd38e0c8feec360284677b1ee31728a to your computer and use it in GitHub Desktop.
Save arjun-kava/0bd38e0c8feec360284677b1ee31728a to your computer and use it in GitHub Desktop.

Revisions

  1. arjun-kava created this gist Jan 22, 2019.
    21 changes: 21 additions & 0 deletions printSchemaWithDirectives.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import { print, isSpecifiedScalarType, isSpecifiedDirective } from 'graphql';

    export function printSchemaWithDirectives(schema) {
    const str = Object
    .keys(schema.getTypeMap())
    .filter(k => !k.match(/^__/))
    .reduce((accum, name) => {
    const type = schema.getType(name);
    return !isSpecifiedScalarType(type)
    ? accum += `${print(type.astNode)}\n`
    : accum;
    }, '');

    return schema
    .getDirectives()
    .reduce((accum, d) => {
    return !isSpecifiedDirective(d)
    ? accum += `${print(d.astNode)}\n`
    : accum;
    }, str + `${print(schema.astNode)}\n`);
    }