Build a GraphQL Server (From Scratch) | Deno Advanced
#
IntroductionGraphQL is already known for its good things. GraphQL helps to build super scalable APIs. It reduces coupling between code and data provider. Deno is one of the fastest-growing frameworks/languages to build APIs. However, Deno community still new. So there is a very little framework in the Deno community. Some of them are in a very early stage of development.
In this article, I will explain how you can easily integrate the GraphQL nodejs module with Deno lang. This is a very basic implementation. However, Still, there is a lot of scope for improvements. This is not the end. This is just beginning.
#
Prerequisite- Deno installed on system
- Basic knowledge of Deno read more..
- Basic understanding of routing read more..
- Basic knowledge of GraphQL read more..
#
Steps#
1. Basic project directory setupFirst, create folders and file as described below.
Here, src will be the directory containing all code. model is to hold all the database/models. We will create all resolvers in resolvers directory. Similarly, routes and schema to keep routes and schemas respectively. src/server.ts will be an entry for the server to start with.
#
2. Velociraptor as script runnerDeno does not have a task runner like npm. However, in Deno community, There are lots of modules we can use. I like Velociraptor. It is easy to start with an easy to integrate.
Once you install velociraptor using deno, It will be available as executable as vr. You can try to validate using vr --version
. It will print the current version of the velociraptor.
Let's add basic scripts in src/server.ts
Tips
velociraptor supports JSON, yaml and many more formats. I like yaml version cool so I am using it.
Explained:
- allow to allow net, read, and env permission to Deno executable
- scripts to add command.
Let's add basic hello world sever in server.ts
Oak#
3. Basic server usingWe are going to use Oak to build our backend server. It is the most stable and community supported module out there.
Run:
Output:
info
You may get a pop-up to allow network access to Deno when you try to run the server.
Explained:
- Import oak module to create an Application.
- dotevn to allow create environmental variable.
- deno_util to create a basic logger.
- app.use to create basic route. Currently, It will respond Hello GraphQL to every request.
#
4. Basic mock databaseSince the purpose of this tutorial is not to teach you how to bind database and all. So we will mock the database as close as a real app.
Create a database.ts file touch src/model/database.ts
note
You can get the whole file in github repo.
Explained:
A very basic mock database with getUsers method, which returns users based on a predicate. If there is no predicate, It will return all users else apply a filter using a predicate.
#
5. Create a /graphql endpoint to handle graphql POST requestGraphQL specs support POST to query server. We will use the same. To create a scalable routing. We will create our route in the routes folder and append it in Oak Application using the callback function.
Create files touch src/routes/index.ts src/routes/graphql.ts
Let's create gqlrouter
Let's update index.ts to append routes to Server.
Explained:
- graphql.ts, We have just created an instance of Oak route. It can accept a get request and a post request at endpoint "/graphql".
- All the GraphQL request contains query and variables as data payload.
- Currently, return
query and variables
in return response. - index.ts to combine routes and apply some other middleware to Oak Application.
Let's update our server.ts
Run:
vr run start
Query using Postman:
Query using CURL:
Output:
#
6. Create GraphQL schema and resolverGraphQL Executor requires schema and resolver to execute the query. For that, we need to create schema and resolver. Schema can be written in String and compile to code using buildSchema helper method. Resolvers are basic functions, will be called by graphql executor, and act on the query.
To use GraphQL Executor we have to install/use graphql module from cdn.skypack.dev
Explained:
- You can use json-to-graphql, To generate graphql schema
- buildSchema compile and validate schema to generate code in runtime.
note
Currently, graphql does not support multi-file schema(Query). So we have to write all queries in index.ts. Hopefully, In future, we can use graphql tools.
Let's create resolvers for users and hello query
Explained:
- user.ts and hello.ts contains an object with Query in it. The query has the function same name as it is defined in GraphQL Schema.
- index.ts accumulate all the resolvers.
#
7. Route to handle Query and executeNow we have created schema and resolver function. We can handle GraphQL requests and responses to the query. For that, we need to update our src/routes/graphql.ts.
Query using Postman:
Query using CURL:
Output:
Congrats, Your GraphQL Server is ready to serve(🚀) the request.
#
LimitationsAs I mentioned earlier, Deno is still very new and the community is also very new. The above app has a lot of limitations. However, We shouldn't stop exploring it. Some of the limitations are highlighted below.
- Support for multiple Query/Aliases
- Merge Schema from multiple files
- Conflict in resolvers
- Validations and Proper Error Handling
#
Source CodeI hope you like this tutorial. let me know your feedback in the comment. Please support(🙏🙏) by subscribing and clapping on https://deepak-v.medium.com/.