Micrograph
Micrograph is a small (~375 LOC) library that makes developing large GraphQL applications with complex business logic pretty easy. Micrograph generalizes root queries and root mutations across every type in your data model. In other words, you can bootstrap a pretty large GraphQL application by writing a few functions. Afterwords, you can continue to add/modify types in your data model without adding and linking GraphQL-specific object types.
Micrograph supports middleware hooks so that you can easily intercept queries and mutations:
import { createMiddleware } from 'micrograph';
const middleware = createMiddleware();
middleware.before('createUser', (args, ctx, next) {
if (!isAuthenticated(ctx.req.headers.authorization)) {
throw new Error('Unauthorized');
}
next();
});
export default middleware;
Micrograph is still in beta and the API may change over time. I've used it in production, but I generally don't recommend it until the API completely stabilizes.
The gist
Micrograph requires the creation of two files: schema.js
and queries.js
. You can optionally create two more files: mutations.js
and middleware.js
. The tutorial explains creating these files. Micrograph compiles these files into a GraphQLSchema
that can be plugged into your favorite GraphQL server library. These files enable your application to grow by focusing on things that matter, such as middleware and schema types.
For example, let's say your data model includes users and blogs. You'll define your schema, create some root queries and mutations, and specify some middleware. A week later, you decide to add more types to your data model. With Micrograph, you simply need to edit your schema.js
and add some more middleware hooks if you choose. You don't need to add more root queries or root mutations - Micrograph automatically takes care of that.
Your app's complexity can grow without worrying about creating new GraphQL input types, object types, etc.
Getting started
First, install the Micrograph package. Micrograph relies on the cohere schema library, so install that as well. cohere
is a simple ~250 LOC schema lib.
npm install micrograph cohere graphql --save
Now, we need to define the schema, queries, mutations, and middleware. These steps are outlined in the tutorial.