Loading GraphQL Schemas from Different Sources — GraphQL Tools documentation.
These utils are useful for scanning, loading and building a GraphQL schema from any input.
You can specify a GraphQL endpoint, local introspection JSON file, code file that exports a
GraphQLSchema, AST string and .graphql files (with support for glob expression).
All found schema files can be merged into a complete schema. There is support for #import syntax
(formerly known as graphql-import).
The user is given the option of implementing their own loader (implement the interface
SchemaLoader).
#import can also resolve npm packages (and other require.resolve specifiers) with a require:
prefix, from the importing file’s directory:
# import Post, Comment from "require:blog-graphql-types/schema.graphql"# import Shared from "require:./shared.graphql"
Binding to HTTP Server
You can extend the loaded schema with resolvers
import { createServer } from 'http'import { join } from 'node:path'import { createYoga } from 'graphql-yoga'import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'import { loadSchema } from '@graphql-tools/load'import { addResolversToSchema } from '@graphql-tools/schema'async function main() { // Load schema from the file const schema = await loadSchema(join(__dirname, './schema.graphql'), { loaders: [new GraphQLFileLoader()] }) // Write some resolvers const resolvers = {} // Add resolvers to the schema const schemaWithResolvers = addResolversToSchema({ schema, resolvers }) const yoga = createYoga({ schema: schemaWithResolvers }) const server = createServer(yoga) server.listen(4000, () => { console.log('Server is running on http://localhost:4000') })}main().catch(error => { console.error(error) process.exit(1)})
Loaders
There are a lot of loaders that load your schemas and documents from different sources. You need to
provide those loaders under loaders parameter like below:
GraphQLFileLoader
This loader loads your GraphQLSchema from .graphql files like below:
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'import { loadSchema } from '@graphql-tools/load'import { addResolversToSchema } from '@graphql-tools/schema'// schema is `GraphQLSchema` instanceconst schema = await loadSchema('schema.graphql', { // load from a single schema file loaders: [new GraphQLFileLoader()]})// You can add resolvers to that schemaconst schemaWithResolvers = addResolversToSchema({ schema, resolvers: { Query: { // ... } }})
This loader also supports glob pattern;
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'import { loadSchema } from '@graphql-tools/load'const schema = await loadSchema('graphql/**/*.graphql', { // load files and merge them into a single schema object loaders: [new GraphQLFileLoader()]})
If you use loadDocuments, it gives you an array of document source objects;
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'import { loadDocuments } from '@graphql-tools/load'const documents = await loadDocuments('graphql/**/*.graphql', { // load files and merge them into a single schema object loaders: [new GraphQLFileLoader()]})
JsonFileLoader
This loader handles schema introspection and document nodes in .json files.
Introspection is handled in the example below:
import { JsonFileLoader } from '@graphql-tools/json-file-loader'import { loadSchema } from '@graphql-tools/load'import { addMocksToSchema } from '@graphql-tools/mock'const schema = await loadSchema('schema-introspection.json', { loaders: [new JsonFileLoader()]})// Mocked non-executable schema generated from an introspectionconst mockedSchema = addMocksToSchema({ schema })
This loader handles json files if they represent DocumentNode, and returns an array of document
sources.
import { JsonFileLoader } from '@graphql-tools/json-file-loader'import { loadDocuments } from '@graphql-tools/load'const documents = await loadDocuments('**/*-document.json', { loaders: [new JsonFileLoader()]})
CodeFileLoader
This loader extracts GraphQL SDL string, exported GraphQLSchema and DocumentNode from TypeScript
and JavaScript code files. Let’s say you have the following code file;
const ME_QUERY = gql` query Me { me { id name username age } }`
And the following code will extract Me query operation from that code file without executing it
using GraphQL Tag Pluck. It understands /* GraphQL */ magic comment and
gql literals. You can configure GraphQL Tag Pluck using pluckConfig.