graphql-schema-modules
Modularize and decouple GraphQL schema and resolvers.
Installation
$ npm install --save graphql-schema-modules
Features
- Keep different query/mutation/subscription definitions in separate files
- Keep resolvers and GraphQL definitions modularized
- Load GraphQL from directory
Usage
graphql/shared.js
const typeDefs = ` enum UserType { MEMBER, ADMIN } type schema { query: Query mutation: Mutation }`;
graphql/users.js
const typeDefs = ` type User { id: Int! type: UserType! } input UserInput { type: UserType! } input UserFilters { type: UserType } type Query { user: User! users (filters: UserFilters = {}): [User!]! } type Mutation { userCreate (input: UserInput!): User! }`; const resolvers = Query: async { return id: 1 type: 'MEMBER' ; } async { return ; } Mutation: async { return id: 1 type: 'MEMBER' ; } ;
graphql/posts.js
const typeDefs = ` type Post { id: Int! title: String! } input PostInput { title: String! } type Mutation { postCreate (input: PostInput!): Post! } type Query { posts: [Post!]! }`; const resolvers = Mutation: async { return id: 1 title: 'Post1' ; } Query: async { return ; } ;
buildSchema.js
;;;;; const typeDefs resolvers = ;// OR// const { typeDefs, resolvers } = loadModules(__dirname + '/graphql'); console; const schema = ; ;
The output will be:
type Post { id: Int! title: String!} input PostInput { title: String!} # This is where magic happens - mutations are mergedtype Mutation { postCreate(input: PostInput!): Post! userCreate(inut: UserInput!): User!} # This is where magic happens - queries are mergedtype Query { posts: [Post!]! user: User! users(filters: UserFilters = {}): [User!]!} enum UserType { MEMBER ADMIN} type schema { query: Query mutation: Mutation} type User { id: Int! type: UserType!} input UserInput { type: UserType!} input UserFilters { type: UserType}