Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 17, 2022 03:01 pm GMT

How to Build a GraphQL server with NodeJS and Express

Being able to work with APIs and write queries is crucial to any developers skill set. While working with REST APIs may be the standard, more and more teams opt to setup their APIs using GraphQL.

In this article, well set up our own GraphQL server with Node.JS(ES6) and write a few queries to gain insight into how GraphQL works. Lets take a quick overview of the pros of using GraphQL before we begin to build our demo.

Advantages of using GraphQL

GraphQL is a query language used by developers to create flexible, and fast APIs which ensure that the clients receive only the data they requested. Other advantages of using GraphQL include:

  • The ability to describe data offered by APIs on the server-side, as well as to send queries to the GraphQL server from the client-side.
  • A declarative format with the query responses decided by the server which helps give better results.
  • Queries that are composed of a hierarchical set of fields. Its shaped just like the data that it returns making it intuitive to work with.
  • GraphQL is a strongly typed query language. The type of data is defined at the server-side while describing data and validated inside a GraphQL type system.

Using GraphQL

Well be using GraphQL along with Express and NodeJS to create a simple server that returns data for an image or set of images depending on the query. To build a server with GraphQL we need the following:

  1. The GraphQL schema - which describes the type and hierarchy of a database. It also defines the query endpoints to which a client can send a request.

  2. The Resolver - to help attach a function to each query from the schema that executes when a client request contains that query.

  3. The Data - This will be the data that our server returns when it receives a query. For our demo purposes, we will create an array of image objects and return data as per the query.

Creating our Server

Without further ado, lets build our GraphQL server using Express and NodeJS.

First, well create our node project with
npm init

In our package.json, we then need to add the following field:

Image description

Since well be creating an ES6 Module.

Next, were going to install Graphql and Express with:
npm i graphql express express-graphql -save

Finally, well add the following javascript file to setup our server

Note that we used the buildSchema method provided by graphql to define our schema. The root method then is our resolver function. It attaches functions to the various queries available to the user. We need to define those functions in our file as well.

Using express we created an app variable that uses the graphql endpoint and the graphqlHTTP middleware. This middleware takes the resolver function and schema as parameters. We also set the graphiql option to true, which enables the GraphiQL in-browser tool which is used to test our endpoint by giving it queries.

Sending queries using GraphiQL

Inside our terminal, we can run the following command to start our server:

node graphql_server_demo.js

If you have named your file something else, obviously use that instead of the name above.

You can then go to http://localhost:5000/graphql to access GraphiQL, which will let us easily test queries
This should launch a GraphiQL interface that looks like this:

Image description

In our demo code, we have two endpoints

  1. image: gets a single image by id
  2. images: gets images by a categoryFor the image endpoint, we can write a query like this:

Image description

The $imageId is a variable that we can pass using the Query Variables section

Image description

The result for the above query will be

Image description

By removing or adding properties inside our query we can also modify what data is returned. Similarly, for our images endpoint, we can create a new query that will take the image category as a variable and return a list of images that match the category.

Image description

With the following parameter
Image description

Yields:

Image description

And there we have it! Weve set up our own GraphQL server using Express and NodeJS. Of course, weve only scratched the surface of what GraphQL is capable of, and as the data were working with gets more complex, GraphQL is going to become more and more useful.

When youre reading to deploy your server, check out Codesphere, the most intuitive cloud platform youve ever used.

Let us know what youre going to build down below!


Original Link: https://dev.to/codesphere/how-to-build-a-graphql-server-with-nodejs-and-express-2g8j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To