Created
November 27, 2018 09:21
-
-
Save imekachi/53567b17f30a1084b9e8965b3bf44e6f to your computer and use it in GitHub Desktop.
Fetch introspectionQuery from your graphQL api and create `graphql.schema.json` file. Works with local host with https(self-signed certificate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('isomorphic-unfetch') | |
const fs = require('fs') | |
const path = require('path') | |
const https = require('https') | |
// by-pass self-signed certificate rejection on local server | |
const agent = new https.Agent({ | |
rejectUnauthorized: false, | |
}) | |
// CHANGE THIS TO YOUR API URL | |
const URL_API_ENDPOINT = '' | |
const writeFileDestination = path.resolve(__dirname, './graphql.schema.json') | |
fetch(URL_API_ENDPOINT, { | |
agent, | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ | |
operationName: 'IntrospectionQuery', | |
variables: {}, | |
query: ` | |
query IntrospectionQuery { | |
__schema { | |
queryType { | |
name | |
} | |
mutationType { | |
name | |
} | |
subscriptionType { | |
name | |
} | |
types { | |
...FullType | |
} | |
directives { | |
name | |
description | |
locations | |
args { | |
...InputValue | |
} | |
} | |
} | |
} | |
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 | |
ofType { | |
kind | |
name | |
ofType { | |
kind | |
name | |
ofType { | |
kind | |
name | |
ofType { | |
kind | |
name | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`, | |
}), | |
}) | |
.then(result => result.json()) | |
.then(result => { | |
fs.writeFile(writeFileDestination, JSON.stringify(result), err => { | |
if (err) { | |
console.error('Error writing fragmentTypes file', err) | |
} else { | |
console.log('GraphQL schemas successfully extracted!') | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment