Create a GraphQL Schema and Resolver for Querying

In this blog, I will discuss about GraphQL, it’s schema creation, how to use resolvers and how to use query by passing arguments. Lets first understand what is GraphQL. 
GraphQL is a query language for your API and a server-side runtime
for executing queries by using a type system you define for your data.
GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data. So I used MongoDB as my database. A GraphQL service is created by defining types and fields on those types.For example, if I want to create an user api so I will define a type by name “User” and add fields that are related to the User type. 
Below I created a UserType.js file

[cc lang=”javascript”]
const graphql = require(‘graphql’);
exports = module.exports = new graphql.GraphQLObjectType({
name: ‘User’,
description: ‘Describes an user’,
fields: () => ({
name : {
type: graphql.GraphQLString
email: {
type: graphql.GraphQLString
language: {
type: graphql.GraphQLString,
password: {
type: graphql.GraphQLString
mobile: {
type: graphql.GraphQLString

 Lets walk through the above UserType.js File in following steps. 
Here at line-1 I used a graphql module. I installed it using npm.At line-2 I 
used export to export the whole User Type. We always need to create a GraphQL 
Object while creating a schema. So, I created a new GraphQLObject that 
would contain my schema ofUserType. At line-3 I assign a name to my schema.
Remember thatevery schema should always have a unique  name.At line-4 it 
gives a basic description of the user type. At line-5 I assigned fields to 
User type. A function is created and is getting all user fields in return.
Each field is assigned by a field name and its type. GraphQL provides 
various data types.
Now, we know how to create a  simple schema. I will discuss some advance things about  schema in my future blogs.
Lets move further and see how to  create a query, arguments that are passed in the query and how to get the queried information using resolver.
                                  In JAVASCRIPT
                       Below I created a UserQueryField.js

[cc lang=”javascript”]
const graphql = require(‘graphql’);

const UserType = require(‘./UserType’);

const NewUserResolver = require(‘./UserGetResolver’);

module.exports = {
userQuery: {
type: UserType,
description: ‘Fetches a particular user by ID!’,
args: {
id: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
description: ‘Id for the user.’
resolve: NewUserResolver

Lets walk through the above UserQueryField.js File in following steps. 
Here at line-1 I used a graphql module. At line-3 I require my UserType
from the same directory where I created UserQueryField.js. At line-5 I require
a resolver named "UserGetResolver". I will use this resolver to do my main 
task i.e respond to the graphql query or mutation request.Line-7 is just 
exporting the data contained in the curly braces. Line-8  describes the query.
Line-9 contains type and in this I added UserType. The Type tells that the 
query resolver will return a JSON containing all the User fields.Line-11 
represents the args that we will pass while querying.In Line-17 we call 
the resolver.We normally pass three arguments with the resolver. For example
the above resolver (i.e resolve : NewUserResolver)can also be written like 
resolve : NewUserResolver(parent, args, request) . Here argument-1 i.e 
parent tells about the hierarchy. Argument-2 i.e args contains the arguments
that will be passed while querying. Third argument is the request which 
actually contains the request object when we call a query.

Till here we created a schema and a basic query which take arguments. Now let’s understand how to use resolver.
                              In JAVASCRIPT
                       Below I created a UserGetResolver.js

[cc lang =”javascript”]
const keystone = require(‘keystone’),
UserData = keystone.list(‘User’).model,
mongoose = require(‘mongoose’),
ObjectId = mongoose.Types.ObjectId;

function convertToObjectId(id) {
try {
return ObjectId(id);
} catch (c) {
return null;

getUserResult = (_id, request, cb) => {
// getting user data of particular id
UserData.findOne({_id: convertToObjectId(_id)}).exec((err, _user) => {
if (err) cb(new Error(‘Id is not valid’));
else if (_user) {
// returning data to the cb
cb(null, _user);
else cb(new Error(‘No user for session!’));

exports = module.exports = (parent, args, request) => ( new Promise((resolve, reject) => { /
getUserResult(, request, (err, results) => {
if (err) return reject(err);
// return fulfill and user data in promise

Lets walk through the above UserGetResolver.js File in following steps. 
At line-1 I require keystone. KeystoneJS makes it easy to build database-driven
websites, applications and APIs in node.js. Line-2 I linked a user type model 
which store all the fields of user in mongodb. At line-3 & line-4 I used mongoose 
and objectId. This is used to convert a given id into object id because mongodb 
store any id in the form of objectId. At line-16 I used the mongo find query and
passed the id to find the user. So here at line 17 if error comes then I send 
an error as a resolver.As you can see at line-26 a promise is returned. That 
means if an error comes a reject promise with an error is returned and if 
there is no error a resolve promise with the resolved data is returned. At 
line-20  a callback is returned which contains the user data, and the same
data is passed to the resolve promise. We can see the resolve promise in line
Now we know everything about graphql i.e from schema to resolver. Lets see some examples to see how graphql query actually works.
Before that lets see one more file which actually integrates all the above files and gives one unique query name.
                              In JAVASCRIPT
                       Below I created a graphql.js

[cc lang =”javascript”]
const keystone = require(‘keystone’),
graphqlExpress = require(‘express-graphql’),
graphql = require(‘graphql’);

const UserQueryField = require(‘./User/query/UserQueryField’)
const MyGraphQLSchema = new graphql.GraphQLSchema({
* Queries (Read only)
* */
query: new graphql.GraphQLObjectType({
name: ‘Query’,
description: ‘The root of all queries’,
fields: () => ({
user: UserQueryField.userQuery
exports.get = graphqlExpress({
schema: LiveBizGraphQLSchema,

At line-6 I created the main schema which contains the parent query. This means that we added all the queries in the root query. Rest is the same as discussed above.
Let’s now check some examples.
The object below is user data stored in mongodb.

"_id" : ObjectId("59a4009128834251d5107a28"),
 "updatedBy" : ObjectId("59a3ff6428834251d5107a24"),
 "updatedAt" : ISODate("2017-08-30T12:11:12.713Z"),
 "createdBy" : ObjectId("59a3ff6428834251d5107a24"),
 "createdAt" : ISODate("2017-08-28T11:37:54.040Z"),
 "password" : "$2a$10$ulh2E15PLUdEfpb82ApbEOFUPtMjFo5MYS38.MuwKERLbunylJcpe",
 "name" : {
 "last" : "Singh",
 "first" : "Amanpreet"
 "email" : ""

I will discuss about mutation in Graphql in my next blog. If you have any doubt or find some mistake please tell me in comments. Thanks.






2 responses to “Create a GraphQL Schema and Resolver for Querying”

  1. It’s hard to come by well-informed people in this particular subject, however,
    you seem like you know what you’re talking about!

  2. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

Recent Post

  • Mastering Software Testing Strategies: Your Guide

    Implementing best software testing strategies is a crucial part of software development, ensuring that digital products meet industry standards. Defined by the International Software Testing Qualification Board, it encompasses a range of activities, both static and dynamic, throughout the software’s lifecycle. As an essential component of the Software Development Life Cycle (SDLC), the Software Testing […]

  • Your Complete Guide to Securing Your Kubernetes Cluster: Essential Tips for Beginners

    In today’s digital landscape, Kubernetes has emerged as the go-to solution for managing applications at scale. However, amidst its power lies a crucial concern: security. Ensuring the safety of your Kubernetes cluster is paramount to protecting your applications, data, and infrastructure. This comprehensive guide will walk you through essential practices and tips to help you […]

  • How to use Cloudinary in Node.js to upload images?

    It’s simple to use Cloudinary to upload images in a Nodе.js application, which can improve your app’s imagе management capabilities. Wе’ll go over how to sеt up and incorporate Cloudinary in your Nodе.js application for imagе uploads and how to use Cloudinary nodejs in this blog article. What is Cloudinary? Cloudinary is a cloud-based media […]

  • Mobile App User Experience- 10 Biggest Mistakes First-Time Founders Make 

    With the mobile app industry hitting a whopping 206 Billion in value (as per Grand View Research), it is no wonder more entrepreneurs are jumping into the mobile app market with their innovative ideas and life-changing solutions. But for those taking their first plunge, it is crucial to understand that creating an app isn’t just […]

  • Building Draggable Components in React

    As user interaction becomes more prevalent in today’s applications, sometimes it’s nice to use draggable elements in your apps. Consider website builders like Trello or Wix. They have drag-and-drop features that make it easy and convenient for users. Hello everyone! In this article, I will show some simple ways to create draggable components in React […]

  • How to Fetch Data from an API with JavaScript?

    In web development, integrating data from external sources is a common task. Application Programming Interfaces (APIs) play an important function in changing data amongst systems. In this blog, we’ll explore the way to Get information from an API with the use of JavaScript, supplying step-by-step the resource of-step commands with complete code examples of how […]