GETTING STARTED WITH GRAPHQL ( Part A )

GraphQL ?

GraphQL is a server-side application layer technology developed by Facebook for query execution using existing data. RESTful API calls can be improved via GraphQL. You can fetch and update your data using a declarative method provided by it.

GraphQL Operation

graphQL unlike REST that everyone is familiar with needs some terms to be properly understood which are as follows;

  • Query in graphQL handles read operations

  • Mutation in graphQL handles write and delete operations

  • Type in graphQL represents a kind of object from which you can fetch your service alongside the field it contains

  • Schema in graphQL are collections of graphQL types

  • Resolver is a function that produces response for graphQL queries and mutations

How Does GraphQL Works:

  • A schema is built ( graphQL type ) that defines possible data you can query on data.

  • We implement either a query or mutation on the built schema ( remember query means reading data and mutation has to deal with writing or deleting from data )

  • Resolver validates query/mutation and executes

A perfect graphQL flow must contain a resolver and schemas

Therefore the flow here to build a simple graphQL are:

  • Create your Schema

  • Implement a resolver that returns corresponding data

There is a saying that states seing is believeing, I attached some code for a better understanding of some concepts I highlighted earlier.

An array of object

let characters = [{
    id: 1,
    name: 'Travis Fimmel',
    role: 'Ragnar',
    movie: 'Vikings'
}, {
    id: 1,
    name: 'Joseph Morgan',
    role: 'Klaus',
    movie: 'Vikings'
}]

A schema that conatins list of types

const typeDefs = `
    type Query {
        brief: String!
        info: [Character!]!
    }
    type Character {
        id: ID!
        name: String!
        role: String!
        movie: String!
    }
`

Resolver Function that executes/implements query/mutation

const resolvers = {
    Query: {
        brief: () => `Best Movie Characters`,
        info: () => characters,
    },
    Character: {
        id: (parent) => parent.id,
        name: (parent) => parent.name,
        movie: (parent) => parent.movie,
    }
}

Here is just a very brief concept we need to understand in graphQL before digging deep to writing code.

My next blog will be a second part on how to integrate apollo client with node JS to execute some query and mutations. Till then anticipate, Thank you...