What is graphql-import?
The graphql-import npm package allows you to import and merge GraphQL schema files. This is particularly useful for modularizing your GraphQL schema by splitting it into multiple files and then combining them into a single schema.
What are graphql-import's main functionalities?
Importing GraphQL Schema Files
This feature allows you to import a GraphQL schema from a .graphql file. The `importSchema` function reads the file and returns the schema as a string, which can then be used with GraphQL tools like Apollo Server.
import { importSchema } from 'graphql-import';
const typeDefs = importSchema('path/to/schema.graphql');
Merging Multiple Schema Files
You can use the `importSchema` function to merge multiple GraphQL schema files into a single schema. This is useful for organizing your schema into smaller, more manageable pieces.
import { importSchema } from 'graphql-import';
const typeDefs = importSchema('path/to/rootSchema.graphql');
Using #import Syntax
The `graphql-import` package supports a special `#import` syntax that allows you to import types from other GraphQL files directly within your schema definition. This makes it easy to modularize your schema.
# import User from 'user.graphql'
# import Post from 'post.graphql'
type Query {
users: [User]
posts: [Post]
}
Other packages similar to graphql-import
merge-graphql-schemas
The `merge-graphql-schemas` package provides similar functionality by allowing you to merge multiple GraphQL schema files into one. It also supports schema stitching and type merging, making it a robust alternative to `graphql-import`.
graphql-tools
The `graphql-tools` package from Apollo provides a set of utilities for building and manipulating GraphQL schemas. It includes functions for schema stitching, merging, and transforming schemas, offering a more comprehensive toolkit compared to `graphql-import`.
graphql-modules
The `graphql-modules` package allows you to create modular and reusable GraphQL schemas. It provides a higher-level abstraction for organizing your schema and resolvers, making it a more feature-rich alternative to `graphql-import`.
graphql-import
Import & export definitions in GraphQL SDL (also refered to as GraphQL modules)
Install
yarn add graphql-import
Usage
import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'
const typeDefs = importSchema('schema.graphql')
const resolvers = {}
const schema = makeExecutableSchema({ typeDefs, resolvers })
Example
Assume the following directory structure:
.
├── a.graphql
├── b.graphql
└── c.graphql
a.graphql
type A {
first: String
second: Float
b: B
}
b.graphql
type B {
c: C
hello: String!
}
c.graphql
type C {
id: ID!
}
Running console.log(importSchema('a.graphql'))
procudes the following output:
type A {
first: String
second: Float
b: B
}
type B {
c: C
hello: String!
}
type C {
id: ID!
}
Please refer to src/index.test.ts
for more examples.
Development
The implementation documentation documents how things are implemented under the hood. You can also use the VSCode test setup to debug your code/tests.
Related topics & next steps
- Static import step as build time
- Namespaces
- Support importing from HTTP endpoints (or Links)
- Create RFC to add import syntax to GraphQL spec