Creating a GraphQL API with Node.js easily

A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.

The core server code of any GraphQL API it’s always basically the same, create the HTTP server, get your resolvers and schema, set the /graphql endpoint, in development set the endpoint for GraphiQL and if you are going to use subscriptions also create a subscription server and attach it to your HTTP server.

That’s a lot of boilerplate code! And Grial will handle all of that for you and let you only think about your application business logic and API layer. How? Grial it’s a serie of many little libraries, the core are @grial/cli and @grial/server which would create your API server and run it with a single command.

Grial also have many connectors little libraries that allow you to easily connect your API to different data sources and enable the creation of a big API that consumes data from Redis, MongoDB, Rest APIs, file disk, etc.

Let’s code an API!

Time to code, we’ll create a directory for the API and install this modules.

The last one it’s a connector for the faker module and we are going to use it to fake our API data. Then we are going to add the following script to our package.json file.

That script will use @grial/cli to run our @grial/server, it’ll also try to read our environment variables from a .env file, we can then create one with this basic variables.

By default it will set PORT=3000, every Grial module have the required and possible environment variables in the readme.

Define the schema

After that we are ready to write our API code, let’s define our schema, Grial let us write it inside a schema.gql or a schema.graphql file, so we’re going to define something like this in one of them.

Write the resolvers

Now we need to create a resolvers.js files. Along with the schema these are the only required files.

We exported many keys but we could also use module.exports and just export a single object with the keys Query and User.

My own personal recommendation is to use many exports, also as your resolvers grow create a resolvers directory with a file Query.js, Mutation.js, Subscription.js and one file for each type we need a custom resolver.

Create the models

As we saw we received the model User from our context object, Grial automatically will read our models.js file and instantiate each one. We will create this file now.

That’s our User model, again we used a name export but we could have done a single export with an object. If you check we are creating an async function for our model, that’s because we could require to run asynchronous code before creating our resolvers (maybe to synchronize the model with a database).

Import the connectors

We also received faker in our model, that’s our connector, each model receive a single parameter with each environment variable and connector that of course allow a single model to get data using multiple connectors.

So, we receive the connector, but how Grial knows that? Simple, just create a connectors.js object and export each connector you want to use.

Those are our API connectors, we can use this file to also define custom connectors, maybe using third-party APIs clients. Every connector will receive every environment variable and it’s going to use some of them, the Faker connector use FAKER_LOCALE and FAKER_SEED and have default values.

If you want to use the same connector multiple times you can wrap them with a high order function which receive the environment variables and pass new ones.

Running the app

With that done we have our API code ready, just run yarn start or npm start and you will see something like this in your terminal.

We can then access to http://localhost:3000/ide and try the API. As you can see Grial also set you a subscriptions endpoint by default, if you create a PubSub instance and add subscriptions to your schema and resolvers you can start using them without worries.

You can also try this application running in production accessing to and another API built with Grial fetching data from a Rest API

The second example wrap the JSONPlaceholder API into a GraphQL one and add tests for the models and resolvers.

Final word

Grial allow to customize its behavior using a grial.config.js file with the keys graphqlConfig, graphiqlConfig and subscriptionConfig, they let you customize GraphQL, GraphiQL and the subscription server configurations, useful to include some token based authentication and other features.

It’s also pluggable, you can use the programmatically API to integrate it inside any HTTP server or application, it can be Next.js, Express.js, etc.

You can contribute to Grial and help create an awesome GraphQL framework with more connectors, features, tools, examples and a completed docs.

Senior Software Engineer & Technical Writer—

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store