The Guild LogoThe Guild Monogram

Search docs

Search icon

Products by The Guild

Products

Hive logoHive blurred logo

Hive

Schema Registry for your GraphQL Workflows

Skip to main content
Version: Current

Get Started

To start using GraphQL Modules, all you need is to install it's package and graphql.

We highly recommend to use TypeScript for writing your backend, since it provides support for Reflection (if you plan to use dependency injection) and makes it easier to develop API services.

Installation#

yarn add graphql graphql-modules

Your first GraphQL module#

To create a Module, use createModule:

import { createModule, gql } from 'graphql-modules';
export const myModule = createModule({  id: 'my-module',  dirname: __dirname,  typeDefs: [    gql`        type Query {            hello: String!        }    `,  ],  resolvers: {    Query: {      hello: () => 'world',    },  },});

Each module contains GraphQL Type definitions, unique id and optionally resolvers.

That's not everything it can do, Module accepts also Providers (Dependency Injection) and Middlewares.

What happened here?#

We are using createModule to declare our module, and name it as my-module. Naming is important, because it help you to locate issues in your type definition.

We also added dirname and pointed it to __dirname in order to make it simpler later to match exception to the correct file. It's optional, but useful.

Next, there is typeDefs and resolvers which you should already know if you are familiar with GraphQL. It defines the type we have in that module, and the implementation behind it.

At this point, this module acts like a types "capsule" you can load and import to various GraphQL Applicationss.

Use your Module#

As mentioned before, Modules create Application, so let's create one. We are importing the module we created earlier, and provide it to the application:

import { createApplication } from 'graphql-modules';import { myModule } from './my-module';
// This is your application, it contains your GraphQL schema and the implementation of it.const application = createApplication({  modules: [myModule],});
// This is your actual GraphQL schemaconst mySchema = application.schema;

Application doesn't allow providing schemas or resolvers, since it's only a loader of your various modules.

Use your Application#

Now that you have Module, Application and you got your GraphQLSchema, you need to make it available to consumption.

GraphQL-Modules allow you to do much more, like managing the lifecycle of your execution, encapsulate your HTTP request and more. To do that in the most optimal and flexible way, we need to wrap the GraphQL execution flow. Some GraphQL servers implementation allow this kind of flexibility, and some doesn't.

But we got you covered, and provided solution for all popular GraphQL server implementations.

Your GraphQL Application exposes createExecution and createSubscription methods, which are just plug-and-play replacements for the default functions from graphql-js.

If you are using Apollo-Server, you can use createSchemaForApollo to get a schema that is adapted for this server, and integrates with it perfectly.

/src/server.ts
import { ApolloServer } from 'apollo-server';import { application } from './application';
const schema = application.createSchemaForApollo();
const server = new ApolloServer({  schema,});
server.listen().then(({ url }) => {  console.log(`🚀 Server ready at ${url}`);});

Tutorial#

If you're interested in a step by step tutorial, one of our community members Godwin Ekuma wrote an amazing article explaining "How to modularize GraphQL schema with GraphQL Modules".