GraphQL Schema

GraphQL Schema


GraphQL server usages a schema refer to the form of available data. This schema describes a chain of command of types with fields. Those are settled from the back-end data stores. The schema as well states accurately which queries and mutations are existing for clients to perform.

In this article, we will learn the important building blocks of a schema and how to make one for the GraphQL server.


A GraphQL schema is essential to any GraphQL server application. It refers to the functionality existing to the client uses that connect to it. We may use any programming language to form a GraphQL schema and shape an interface around it.

The GraphQL runtime outlines a generic graph-based schema. That is to publish the competencies of the data service it signifies. Client applications may query the schema inside its capabilities. This method decouples clients from servers and permits together to change and scale freely.

All we require to prepare is to identify the types for the API using the GraphQL schema language. That is in use as an argument to the build schema function.

The GraphQL schema language cares for the scalar types of String, Int, Float, Boolean, and ID. Therefore, we can use these openly in the schema we pass to build schema.

Each type is nullable by default. It’s real to return null as any of the scalar types. Usage an exclamation point to specify a type cannot be nullable. Therefore, String! is a non-nullable string.

Surround the type in square brackets to use a list type. Therefore, [Int] is a list of integers. Each of these types maps directly to JavaScript. As we can just return plain old JavaScript objects in APIs that return these types.


var express = require('express');

var { graphqlHTTP } = require('express-graphql');

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

// Construct a schema, using GraphQL schema language

var schema = buildSchema(`

  type Query {

    quoteOfTheDay: String

    random: Float!

    rollThreeDice: [Int]



// The root provides a resolver function for each API endpoint

var root = {

  quoteOfTheDay: () => {

    return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';


  random: () => {

    return Math.random();


  rollThreeDice: () => {

    return [1, 2, 3].map(_ => 1 + Math.floor(Math.random() * 6));



var app = express();

app.use('/graphql', graphqlHTTP({

  schema: schema,

  rootValue: root,

  graphiql: true,



console.log('Running a GraphQL API server at localhost:4000/graphql');


Field definitions

We define many of the schema types which have one or more fields. Every field returns data of the type stated. A field’s return type may be a scalar, object, enum, union, or interface.

# This Book type has two fields: title and author

type Book {

  title: String  # returns a String

  author: Author # returns an Author


Function Syntax of makeExecutableSchema

This function takes a single argument {} of Object type. The syntax for using this function is described as;

import { makeExecutableSchema } from 'graphql-tools';

const jsSchema = makeExecutableSchema({


   resolvers, // optional

   logger, // optional

   allowUndefinedInResolve = false, // optional

   resolverValidationOptions = {}, // optional

   directiveResolvers = null, // optional

   schemaDirectives = null,  // optional

   parseOptions = {},  // optional

   inheritResolversFromInterfaces = false  // optional






Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 421

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top