Setup a GraphQL server in seconds using graphql-serve

GraphQL Serve

TL;DR — you can run a GraphQL backend without code using the graphql-serve package

Learning GraphQL is difficult, more so with all the boilerplate that is needed to create applications using it. The Graphback project is driven by the need to simplify GraphQL adoption.

The motivation of the Graphback project has always been to automatically generate boilerplate code for server. The project is a collection of packages that:

  • Decorate schema with out of the box data queries and mutations basing on data models.
  • Generate GraphQL resolvers either at runtime or as static code in your project.
  • Generate database migrations from data models for relational databases.

All of these features can be accessed through the graphback-cli package.

Introducing GraphQL serve

The newest addition to the Graphback family of packages starting with 0.11.0 is graphql-serve.

While graphback-cli operates typically on code graphql-serve provides a full GraphQL server embedded in the command line tool.

It generates a fully-featured GraphQL server using Node.js, Apollo server and MongoDB database. GraphQL serve supports GraphQL config for further customisation. For example you can add an additional Offix plugin that will provide conflict resolution and data sync queries.

Usage

Install the graphql-serve package with npm or yarn:

npm i -g graphql-serve

or

yarn add graphql-serve

All you have to do then, is to define your data models. Data models are one or more files with the .graphql extension. Here are the contents of an example model file, let’s say Note.graphql:

""" @model """
type Note {
id: ID!
title: String!
description: String
likes: Int
}

Graphback uses these files to generate a GraphQL schema and resolvers. The definition of the model is similar to a GraphQL schema except it has some annotations that are used by Graphback to determine which types need a table in the database as well as relationships between those tables. All of these are covered in the Graphback docs here.

Put this in the current working directory and run the following command(for yarn users, please prepend yarn to all the commands below):

$ gqls serve . --port=8080
Snap!

This command takes the data model files in the current working directory and starts a GraphQL server that listens on port 8080. It also has the GraphQL Playground from Apollo GraphQL so you can test queries by going to the endpoint on a web browser.

If you just want to see the resulting generated GraphQL schema, use the print-schema command:

$ gqls print-schema .

This will only print the GraphQL schema generated from the data model files in the current working directory.

Why we created GraphQL serve

With just one command, you can have a GraphQL server running on your machine that you can use to test a frontend application or just test GraphQL. GraphQL serve supports data loading and has comprehensive support for all types of relationships

Unlike mocking alternatives, the MongoDB database used by default in graphql-serve persists through queries. We also provide a way to configure your own database using graphql-config, this is documented here. Thank you for reading!

Try graphql-serve today and let us know what you think!

--

--

--

Writing about stuff that I find cool

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Recollections of Early Predictive Text Software

Confusion Matrix Image Cut Off (Python, AWS Lambda, Scikit-learn)

The Two Generals Problem

So I decide to build my own Kubernetes (replica set feature) for fun over weekend.

Free Download Mastering the Requirements Process: Getting Requirements Right (3rd Edition) Pre…

Project Loom, a new chapter in Java

Building a Fintech App from Idea to Prototype

HTTP/2 — A Revision of the Major Protocol of the World Wide Web

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
Shibani Shankar Dash

Shibani Shankar Dash

Writing about stuff that I find cool

More from Medium

Custom Actions in Botframework Composer with Node.JS (Part 2)

Integrate AWS GraphQL in your Ionic/Angular applications using Amplify.

A Perfect Match: NestJs & Cloud Functions (2nd gen) & Nx WorkSpace

Debugging NodeJS Google Cloud Functions Locally with Functions Framework in VSCode