If you define your type definitions inside a .graphql
file, you can read it in one of several ways:
1.) Read the file yourself:
const { readFileSync } = require('fs')
// we must convert the file Buffer to a UTF-8 string
const typeDefs = readFileSync(require.resolve('./type-defs.graphql')).toString('utf-8')
2.) Utilize a library like graphql-tools
to do it for you:
const { loadDocuments } = require('@graphql-tools/load');
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');
// this can also be a glob pattern to match multiple files!
const typeDefs = await loadDocuments('./type-defs.graphql', {
file,
loaders: [
new GraphQLFileLoader()
]
})
3.) Use a babel plugin or a webpack loader
import typeDefs from './type-defs.graphql'