Import & export definitions in GraphQL SDL (also referred to as GraphQL modules)
This is a custom fork of
graphql-import
created by prisma.io.
yarn add @flybondi/graphql-import
const { importSchema } = require('@flybondi/graphql-import');
const { makeExecutableSchema } = require('graphql-tools');
const typeDefs = importSchema('./schema.graphql');
const resolvers = {};
const schema = makeExecutableSchema({ typeDefs, resolvers });
Assume the following directory structure:
.
├── schema.graphql
├── posts.graphql
└── comments.graphql
schema.graphql
# import Post from "posts.graphql"
type Query {
posts: [Post]
}
posts.graphql
# import Comment from 'comments.graphql'
type Post {
comments: [Comment]
id: ID!
text: String!
tags: [String]
}
comments.graphql
type Comment {
id: ID!
text: String!
}
Running console.log(importSchema('schema.graphql'))
produces the following output:
type Query {
posts: [Post]
}
type Post {
comments: [Comment]
id: ID!
text: String!
tags: [String]
}
type Comment {
id: ID!
text: String!
}
You can also import every .graphql
files in your project using globs patterns.
Assume the following directory structure:
.
├── movies.graphql
├── books.graphql
movies.graphql
type Movie {
id: ID!
name: String
director: String
seen: Boolean!
}
type Query {
movie(id: ID!): Movie
}
books.graphql
type Book {
id: ID!
name: String
read: Boolean!
}
type Query {
book(id: ID!): Book
}
type Mutation {
readBook(id: ID!): Book
}
Running console.log(importSchema('*.graphql'))
produces the following output:
type Query {
movie(id: ID!): Movie
book(id: ID!): Book
}
type Mutation {
readBook(id: ID!): Book
}
type Book {
id: ID!
name: String
read: Boolean!
}
type Movie {
id: ID!
name: String
director: String
seen: Boolean!
}
Check original Prisma full documentation for graphql-import
.
Refactored (and maintained) with 💛 by Flybondi.