Express GraphQL
Tags: node, graphql, typescript, api
Published: 2022-07-20 08:56:00

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d6imn805osnkt8gl4qy.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vwx78joph4fqr4wa0ts.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eyifhqhezh27nrkyg7z.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c54pmxtcyk90y4ue1383.png)
 

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