Last active
March 11, 2021 17:49
-
-
Save nanorepublica/48ec69db23da173d56be56398800d010 to your computer and use it in GitHub Desktop.
Quick script to get a GraphQL schema file for Content Models from Prismic.io graphql endpoint.
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 { buildClientSchema, printSchema } = require("graphql"); | |
const fs = require("fs"); | |
const { PrismicLink } = require('apollo-link-prismic'); | |
const { gql, ApolloClient, InMemoryCache } = require("apollo-boost"); | |
const prismicReposistory = "<prismic.io repository name>" | |
const accessToken = "<access token from the repo API settings page" | |
const ouputFile = "./prismic.graphql" | |
const apolloClient = new ApolloClient({ | |
link: PrismicLink({ | |
uri: `https://${prismicReposistory}.prismic.io/graphql`, | |
accessToken: accessToken, | |
}), | |
cache: new InMemoryCache() | |
}); | |
// Query taken from Insomnia API client | |
const introspectionQuery = gql` | |
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 | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}` | |
apolloClient.query({query:introspectionQuery}).then(({data}) => { | |
const graphqlSchemaObj = buildClientSchema(data); | |
const sdlString = printSchema(graphqlSchemaObj); | |
fs.writeFile(ouputFile, sdlString, function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
}).catch(error => { | |
console.error(error); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG thank you so much!!!