What is @graphql-tools/import?
@graphql-tools/import is a utility package that allows you to import and merge GraphQL type definitions from multiple files. This is particularly useful for organizing your GraphQL schema into smaller, more manageable pieces.
What are @graphql-tools/import's main functionalities?
Importing Type Definitions
This feature allows you to import and merge GraphQL type definitions from multiple files. The `loadFilesSync` function loads all the type definition files from the specified directory, and `mergeTypeDefs` merges them into a single schema.
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');
const typesArray = loadFilesSync(path.join(__dirname, './schemas'));
const typeDefs = mergeTypeDefs(typesArray);
console.log(typeDefs);
Using SDL Imports
This feature allows you to use SDL (Schema Definition Language) imports within your GraphQL schema files. The `importSchema` function reads a schema file and resolves any `#import` statements, merging the imported types into a single schema.
const { importSchema } = require('@graphql-tools/import');
const typeDefs = importSchema('./path/to/schema.graphql');
console.log(typeDefs);
Other packages similar to @graphql-tools/import
graphql-import
graphql-import is a package that allows you to import and merge GraphQL type definitions using SDL imports. It is similar to @graphql-tools/import but is more focused on SDL imports specifically, whereas @graphql-tools/import offers a broader range of utilities for schema management.
merge-graphql-schemas
merge-graphql-schemas is a package that provides utilities to merge multiple GraphQL schemas into one. It is similar to @graphql-tools/import in that it helps manage and organize GraphQL schemas, but it does not support SDL imports directly.