Skip to content

Instantly share code, notes, and snippets.

@mitchellsimoens
Created February 1, 2019 13:53
Show Gist options
  • Save mitchellsimoens/eaa0579b07772aabf8d9c876cd2cd6d2 to your computer and use it in GitHub Desktop.
Save mitchellsimoens/eaa0579b07772aabf8d9c876cd2cd6d2 to your computer and use it in GitHub Desktop.
buildAST.js
const { buildSchema, execute, parse } = require('graphql');
const idl = `
type Query {
version: String!
name: String!
}
extend type Query {
hello: String!
}
`;
const fullTypeFragment = `
fragment FullType on __Type {
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}`;
const inputValueFragment = `
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}`;
const typeRefFragment = `
fragment TypeRef on __Type {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
ofType {
kind
name
description
}
}
}
}
}
}
}
}`;
const query = `
query IntrospectionQuery {
__schema {
queryType { name description kind}
mutationType { name description kind }
subscriptionType { name description kind }
types {
name
kind
description
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
${fullTypeFragment}
${inputValueFragment}
${typeRefFragment}
`;
const parsed = parse(query);
const schema = buildSchema(idl);
const introspection = execute(schema, parsed);
console.log(JSON.stringify(introspection, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment