Skip to content

Instantly share code, notes, and snippets.

@craigbeck
Created April 6, 2016 20:20
Show Gist options
  • Save craigbeck/b90915d49fda19d5b2b17ead14dcd6da to your computer and use it in GitHub Desktop.
Save craigbeck/b90915d49fda19d5b2b17ead14dcd6da to your computer and use it in GitHub Desktop.
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
}
}
}
}
@tvvignesh
Copy link

Very helpful. Thanks.

@falconmick
Copy link

cheers

@konsumer
Copy link

Thanks! I used it to make this

@rkaneko
Copy link

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
Copy link

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
Copy link

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

@danstarns
Copy link

danstarns commented Mar 8, 2020

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

async function main() {
  const introspectionQuery = getIntrospectionQuery();

    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
Copy link

@danstarns what is the query here?

@danstarns
Copy link

@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
Copy link

@danstarns thank you.

@24601
Copy link

24601 commented Jan 15, 2021

The graphql reference implementation on JS has changed slightly and introspectionQuery is no longer exported as above. For newer versions of graphql (after graphql/graphql-js#2718), the above will break/not work, however, this seems to work after adapting to getIntrospectionQuery:

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

async function main() {
  const introspectionQuery = getIntrospectionQuery();

    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();

@danstarns
Copy link

The graphql reference implementation on JS has changed slightly and introspectionQuery is no longer exported as above. For newer versions of graphql (after graphql/graphql-js#2718), the above will break/not work, however, this seems to work after adapting to getIntrospectionQuery:

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

async function main() {
  const introspectionQuery = getIntrospectionQuery();

    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();

Good call updated getIntrospectionQuery 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment