Last active
July 10, 2019 06:55
-
-
Save vvakame/0d92c9101e6db6fa6f5f2ab714bca00e to your computer and use it in GitHub Desktop.
*.graphql to schema.json
This file contains 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
// from https://blog.apollographql.com/three-ways-to-represent-your-graphql-schema-a41f4175100d | |
// https://blog.apollographql.com/modularizing-your-graphql-schema-code-d7f71d5ed5f2 | |
const fs = require("fs"); | |
const glob = require("glob"); | |
const { graphqlSync, getIntrospectionQuery } = require("graphql"); | |
const { makeExecutableSchema } = require("graphql-tools"); | |
const typeDefs = glob | |
.sync("../graphql/*.graphql") | |
.map(file => fs.readFileSync(file, { encoding: "utf8" })); | |
const graphqlSchemaObject = makeExecutableSchema({ | |
typeDefs: typeDefs, | |
resolverValidationOptions: { | |
requireResolversForResolveType: false, | |
}, | |
}); | |
{ | |
const introspectionQuery = getIntrospectionQuery(); | |
const result = graphqlSync(graphqlSchemaObject, introspectionQuery).data; | |
fs.writeFileSync("schema.json", JSON.stringify(result, null, 2)); | |
} | |
{ | |
// from https://www.apollographql.com/docs/react/advanced/fragments.html#fragment-matcher | |
const result = graphqlSync(graphqlSchemaObject, ` | |
{ | |
__schema { | |
types { | |
kind | |
name | |
possibleTypes { | |
name | |
} | |
} | |
} | |
} | |
`); | |
const filteredData = result.data.__schema.types.filter( | |
type => type.possibleTypes !== null, | |
); | |
result.data.__schema.types = filteredData; | |
fs.writeFileSync('./src/apollo-utils/fragmentTypes.ts', `export const introspectionQueryResultData = ${JSON.stringify(result.data, null, 2)};`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment