Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Introspection query for GraphQL
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
args {
...InputValue
}
onOperation
onFragment
onField
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
@mfix22

This comment has been minimized.

Copy link

@mfix22 mfix22 commented Mar 8, 2017

This is awesome. Thanks @craigbeck!

@mr-beerkiss

This comment has been minimized.

Copy link

@mr-beerkiss mr-beerkiss commented Feb 1, 2018

Very useful, thanks for posting!

@tvvignesh

This comment has been minimized.

Copy link

@tvvignesh tvvignesh commented Mar 25, 2018

Very helpful. Thanks.

@falconmick

This comment has been minimized.

Copy link

@falconmick falconmick commented Apr 20, 2018

cheers

@konsumer

This comment has been minimized.

Copy link

@konsumer konsumer commented Jun 20, 2018

Thanks! I used it to make this

@rkaneko

This comment has been minimized.

Copy link

@rkaneko rkaneko commented Nov 4, 2018

@craigbeck

Thank you for your nice gist.

According to graphql-js issue #1429, graphql-js v14 dropped support onOperation, onFragment and onFields fields of directives.

This IntrospectionQuery is invalid for GraphQL API using v14 or later.

Instead of above fields, we should use the locations field.

query IntrospectionQuery {
  __schema {
    queryType {
      name
    }
    mutationType {
      name
    }
    subscriptionType {
      name
    }
    types {
      ...FullType
    }
    directives {
      name
      description
      args {
        ...InputValue
      }
-      onOperation
-      onField
-      onFragment
+     locations
    }
  }
}
@m14t

This comment has been minimized.

Copy link

@m14t m14t commented Nov 7, 2018

I was looking for the following when I came across this gist:
https://graphql.org/graphql-js/utilities/#introspectionquery

const {introspectionQuery } = require('graphql');
@johndpope

This comment has been minimized.

Copy link

@johndpope johndpope commented Jul 25, 2019

npm install -g graphql-introspect
graphql-introspect URL > schema.graphql
@danstarns

This comment has been minimized.

Copy link

@danstarns danstarns commented Mar 8, 2020

const path = require("path");
const fs = require("fs");
const fetch = require("node-fetch");
const {
    introspectionQuery,
    printSchema,
    buildClientSchema
} = require("graphql");

async function main() {
    const response = await fetch(
        "https://<URL_HERE>/graphql",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({ query: introspectionQuery })
        }
    );

    const { data } = await response.json();

    const schema = buildClientSchema(data);

    const outputFile = path.join(__dirname, "./result.gql");

    await fs.promises.writeFile(outputFile, printSchema(schema));
}

main();
@nagarajcruze

This comment has been minimized.

Copy link

@nagarajcruze nagarajcruze commented Sep 8, 2020

@danstarns what is the query here?

@danstarns

This comment has been minimized.

Copy link

@danstarns danstarns commented Sep 8, 2020

@danstarns what is the query here?

introspectionQuery is an exported member of graphql, the query is that.

const { introspectionQuery } = require("graphql");

console.log(introspectionQuery);
@nagarajcruze

This comment has been minimized.

Copy link

@nagarajcruze nagarajcruze commented Sep 9, 2020

@danstarns thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.