GraphQL Server with Deno and Oak Framwork
Tags: deno, graphql, typescript, node
Published: 2020-07-03 16:15:24

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qm3od2fxsw1dgg67vssw.jpg)

In my previous topic, I have explained about below things
1. What is GraphQL?
2. Comparing REST API and GraphQL
3. GraphQL Terms

##Creating A GraphQL Server With Deno

**Our Deno dependencies**

[Oak](https://deno.land/x/oak_graphql) : A middleware framework for Deno’s http server, including a router middleware.

[Oak-GraphQL](https://deno.land/x/graphql_deno) : A simple graphql middleware for oak deno framework.

[GraphQL-Deno](https://deno.land/x/graphql_deno) : This is a one-to-one port of graphql-js to Deno. It is working, fully adapted to Deno and exports both TS types and JS code.

First we are making the basic deno server with **oak** framework and **oak_graphql**. We are creating the common dependency file for import packages.

{% gist https://gist.github.com/singhcool/256bed5b6d5bdfba2977867f2b93852d.js %}

{% gist https://gist.github.com/singhcool/146b4a627219c6b87f92ffbcdc5488e2.js %}

##GraphQL Schema##

A GraphQL schema is a description of the data clients can request from a GraphQL API. **It also defines the queries and mutation functions that the client can use to read and write data from the GraphQL server.** In other words, you specify your client or application UI data requirements in your GraphQL schema.

The schema is written using the GraphQL schema language (also called **Schema Definition Language**, SDL).

*With it, you can define object types and fields to represent data that can be retrieved from the API as well as root types that define the group of operations that the API allows.*

**Object Types**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6pdpctuy9qde79synr07.png)

**Root Types**

*The root types are the query type, mutation type, and subscription type, which are the three types of operations you can run request from a GraphQL server.*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ktaoyqbt7xh6f5p07i1z.png)

Let’s go ahead and create a schema. Add a new file `src/schema/author.ts` with the following content

{% gist https://gist.github.com/singhcool/6c84fe5e7717b9aa66a5d95890fd8c62.js %}

What we have above is the GraphQL schema. In it, we defined a **Author** type with four fields, **Post** type with five fields and a root Query type with two fields.

**GraphQL Query**

The two fields in the root Query type defines what queries/operations the server can execute. The **getAuthor** return a **Author based on the id passed as an argument to query**. The **getPost** return a **Post based on the id passed as an argument to query.**

**GraphQL Mutation**

The GraphQL Mutation is used to perform the **Create, Update and Delete** Operation. The **createAuthor, createPost** perform the insert operation based on Input object.

##Resolver##

Our API is able to run two query operations

one to retrieve a Author with Array of Author’s post based on its id and another one retrieve a Post based on its id. The next step for us is to define how these queries get resolved so that the right fields are returned to the client.

**GraphQL has an execution algorithm.** The implementation of this execution algorithm is **what transforms the query from the client into actual result, by moving through every field in the schema, and executing their “resolver” function to determine its result.**

Add the below code to `src/resolver/author.ts`


{% gist https://gist.github.com/singhcool/a3ac803802bed2bbcea4d3a868815331.js %}

Every resolver function receives the below four arguments

1. **parent** : It contains the result of the previously executed resolver in the call chain.

2. **args** : These are the arguments provided to the field in the GraphQL query. Following our example, this will be the id argument for the getAuthor query getAuthor(_id: ID): Author.

3. **context** : This is an object that every resolver can read from or write to. You can keep objects that give access to database or that contain information from the HTTP request headers here. The context object is the same across resolvers, and you can write contextual information to it as needed.

4. **info** : It holds field-specific information relevant to the current query as well as the schema details.

##GraphQL Query Language##

Open the command line and run the below command

`denon run --allow-net --allow-env --allow-write --allow-read --allow-plugin --unstable server.ts`

Now the server running on **http://localhost:8080/graphql**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0ul2703ypgjduu4dkgeh.png)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ix8wcv988xigyz6l928f.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3yygbwjf2q2pl4fqcdes.png)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wkg3lz5mma479wem2y3w.png)

##Conclusion##

If you have suggestions please let me know in the comments section🙋‍♂️

Thank You!🖤

Here is the [GitHub Repository](https://github.com/singhcool/deno-server-graphQL) for all the source code.

In the next section will be covering following things

1. GraphQL Subscription

2. GraphQL with Client side Integration with React JS