DEV Community

Opeyemi Bamidele
Opeyemi Bamidele

Posted on

Getting Started With Graphql

Introduction

GraphQL is one of the most exciting technologies that caught a lot of developers attention in this year . A feasible alternative to RESTful APIs, GraphQL APIs provide a much more to the point that it is easy to read and write relational data between client and server.

Two of the more exciting pieces of technology within the GraphQL ecosystem are graphql-server and apollo-client, built by the folks at Apollo. In this post, we will focus on how you can get started with GraphQL , getting the basic server up and runing with graphql-server.

Architecture

GraphQL is an application layer query language .Before we dive into the nitty-gritty of our server, let's take a quick moment to cover some basic GraphQL terms:

A Query traverse related objects and their fields, letting clients fetch lots of related data in one request.
A Mutation is a read-write operation made against a GraphQL server with this, it allows you add and delet users.
A Resolver are a set of application specific functions that interact with your underlying datastores according to the query and mutation  operation
A Schema A schema is constructed of a set of object types--which are maps of named fields, each of which is another type, either another object, or a scalar base type.
A Type defines the shape of output / response data that can be returned from the GraphQL server, including fields that are edges to other Types.
An Input is like a Type, but defines the shape of input data that is sent to a GraphQL server.
A Scalar is where the actual data of the system lives and they are of primitive Type, such as a String, Int, Boolean, Float, etc.
Let get started

Here we will be doing graphQL.Js, so let's get started by doing simple "welcome" server so that we can get our hands dirty by implementing it. Before starting, make sure you have preferrably Node v6 installed. Open your command-line utitlity or terminal and type the command below:

node -v
//to check your node version 
Enter fullscreen mode Exit fullscreen mode

If you don't already have NodeJS configured, go the URL: https://nodejs.org and follow the instructions to install it.
To create a new project and install GraphQL.js in your current directory, do:

npm init
npm install graphql –save
Enter fullscreen mode Exit fullscreen mode

The --save flag is to add it as a dependency in your application so that anyone that installs your application automatical installs the dependencies when npm install is initiated.

*Writing Code *

Therefore to handle GraphQL queries, we need a schema that defines the Query type, and we need an API root with a function called a “resolver” for each API endpoint. For an API that just returns “Welcome”, we can put this code in a file named server.js:

var { graphql, buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => {
    return 'Hello world!';
      },
};

// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});
Enter fullscreen mode Exit fullscreen mode

then right click on the project folder, click on open terminal and run this:
node server.js
Then you should see the GraphQL response printed out:
{ data: { hello: 'Welcome' } }
For more information visit: graphql.org

Top comments (0)