create-root-schema
🌿 Combines schemas and types to help you modularize your GraphQL service
GraphQL is awesome, but there isn't yet a standardized way to break up schemas in a way that makes sense. By enforcing a simple standard (exporting certain variables) on all your schema files, you can break the service up in a way that makes sense for your project.
We believe this system allows you to make very few boilerplate changes to extend your service while also being very explicit—we don't like unnecessary magic.
ℹ️ This is just a very thin wrapper extending/replacing makeExecutableSchema
.
See graphql-tools
for more documentation.
Usage
All your schema files can export typeDefs
, resolvers
, and
schemaDirectives
. All are optional.
To add functionality, each file may:
- Extend a root type:
Query
and/orMutation
- Add or extend custom typesD
- Add resolvers for the types
- Add a custom scalar/enum
- Example usage
- Read the
graphql-tools
documentation more more information
- Add a custom schema directive
- Example usage
- Read the
graphql-tools
documentation more more information
// Example Users schema file; const typeDefs = /* GraphQL */ ` type User { lastSeen: Timestamp name: String } # Extend the root types to expose logic... extend type Query { user(id: ID!): User users: [User] }`; const resolvers = Query: ;
Then, import the schema parts into a single file and create a root schema:
; ;;; // NOTE: Choose one of these options… // - option 1: get the combined schema:device notification user; // - option 2: both combine the schema and convert to an executable schema:device notification user;
apollo-server-express
v2
Usage with // ./schemas/index.js; ;;; brand device notification user;
// ./index.js;;; const app = ; const server = schema; server; app;
graphql-yoga
Usage with // ./schemas/index.js; ;;; brand device notification user;
;; const server = schema;serverstart console;
Extra Options
This is just a thin wrapper around makeExecutableSchema
. Any options passed as the second argument will be forwarded directly to makeExecutableSchema
.
; // See `graphql-tools` docs for more information;
Install
With Yarn or npm installed, run:
yarn add create-root-schema
# ...or, if using `npm`
npm install create-root-schema
License
MIT