DEV Community

ckmonish2000
ckmonish2000

Posted on • Updated on

Express GraphQL

So this post is not gonna tell you what GraphQL is and how it's better over restful API's rather this is a gist to act as a tool to revise the basic concepts on GQL like how to define a query or mutation or how to write a resolver using a GraphQL server called Express GraphQl.

Dependencies & initial setup

First and foremost we have to install the dependencies for the project , I'm assuming you have your bare bones node project already setup.

Next run one of the command down below based on which package manager you are using :

yarn add express express-graphql graphql

or

npm i --save express express-graphql graphql

after that set up the server in your index.ts file

express js setup

Queries & Mutations

Once you have set up your GraphQL end point you'll have to setup your GraphQL Schema which is basically a combination of your GraphQL mutations, queries and subscriptions.

to set up a Schema you have to first import 2 things

const {GraphQLSchema, GraphQLObjectType} = require('graphql')

the GraphQLSchema object is used to create a new instance of the schema and while initializing the object it take a JSON value as a parameter, This parameter will further contain the details for the queries and mutation.

Down below I'm creating a file called test.ts which will export our schema

building a schema

once you have exported you schema import it and pass it into the your index.ts file so that your graphql end point knows where to look for the queries and mutations.

app.use("/graphql", graphqlHTTP({
schema: Schema,
graphiql: true,
}))

So let's begin by creating a query in a file called queries.ts

creating queries with express GQL

here let's first take a look at the GraphQLObjectType which takes in 2 parameters:

  1. name: indicates the name for the given return type of the resolver.

  2. fields indicates the values that a mutation or query will return so the fields have 2 sub fields i.e the type of the field and a custom function called resolve.

In type you add the type of value a particular field is gonna return and in resolve function your basically gonna return the value of the given type.

and if you notice you can also create custom types like user in the above example it's not very different from creating a query,
The only key difference is that

a. In a custom object type you do not add the resolve function

b. you can use the custom object type as a return type for any other resolver.

and that's all we need to know in order to define a query and to define a mutation you can pretty much use the same steps but just pass the mutations in the mutations part of the schema.

Arguments

Now when you are building a mutation your gonna need to accept some parameters for that you can use the args key when defining a given query or mutation and extract them from the resolve function like shown down below.

adding arguments with express GQL

That's all for this post follow me for more such quick guides and handbooks

Top comments (0)