What is @graphql-tools/graphql-file-loader?
The @graphql-tools/graphql-file-loader package is a utility that allows you to load GraphQL schema definitions and type definitions from .graphql and .gql files. It integrates with other GraphQL tools to enable you to construct your GraphQL schema in a modular and organized way using separate files for different parts of your schema.
What are @graphql-tools/graphql-file-loader's main functionalities?
Loading GraphQL schema files
This feature allows you to load a GraphQL schema from a .graphql or .gql file synchronously. The `loadSchemaSync` function is used in conjunction with the `GraphQLFileLoader` to read the schema file and construct a GraphQLSchema object.
const { loadSchemaSync } = require('@graphql-tools/load');
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');
const schema = loadSchemaSync('path/to/schema.graphql', {
loaders: [new GraphQLFileLoader()]
});
Loading GraphQL type definitions
This feature allows you to load GraphQL type definitions from a file. The `loadTypedefsSync` function is used to read type definitions from a .graphql or .gql file and can be used to compose a schema programmatically.
const { loadTypedefsSync } = require('@graphql-tools/load');
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');
const typeDefs = loadTypedefsSync('path/to/types.graphql', {
loaders: [new GraphQLFileLoader()]
});
Other packages similar to @graphql-tools/graphql-file-loader
graphql-import
The graphql-import package allows you to import and use .graphql files in your JavaScript and TypeScript projects. It provides functionality similar to @graphql-tools/graphql-file-loader but with a different API and additional features like importing specific types from a schema file.
graphql-tag
graphql-tag is a package that provides a template literal tag to parse GraphQL queries. While it does not directly load schema files, it is commonly used to define GraphQL queries and fragments within JavaScript files, which can then be composed into a full schema.