TECH Insights

Viden og indsigter fra IT Minds' it-talenter

How to architecture your JavaScript API using NestJS with a GraphQL API example part 2/2.

Skrevet af Mindster og Software Developer hos IT Minds on 05-11-19 09:01
Mindster og Software Developer hos IT Minds

This is a follow up article to the previous introduction to NestJS (read the prior article here). After the introduction and setup, we are now ready to start building our resolvers for our API.

Build APIs in NestJS

When we are building APIs in NestJS, we primarily work in 3 different types of files. The files .service, .resolvers and .types.graphql. all have their own responsibilities:

The Types

The types is where we define the Queries, Mutations InputTypes and other GraphQL specifications which we want in our Schema. Our setup in the previous article (take a look in app.module.ts) join all our .graphql-files in one Schema.

The Service

If we think of a service in Angular, we primarily use these to prevent the component itself against performing requests to resources. Hence we use the power of dependency injection to inject these services to the component and call the service when need be for resources.

Same goes for the service in NestJS! Due to this separation we can easily test the functionality of services and its "users", so to speak. We use these services to acquire resources. Usually I use the service for business specific logic as well. Say you want to make some recommendation logic for our products.

The Resolvers

Resolvers are just resolvers as we know them. Here we define the logic for our Queries and Mutations. Usually I keep these as clean as possible and force logic into the services. Typically this results in delegation of the arguments from the Mutation and Query to a service.

With these type of files in mind, I think it is time for us to start creating the API.

 

Product Types to our Schema

We start by creating our Product-type and a corresponding inputType. We want to be able to create a Product and hereafter find it again by its ID. Hence we create a Query and a Mutation as well:

Screenshot 2019-11-05 at 08.58.14

As we save the file you might find that the compiler generated a new graphql.ts-file. In this file you will see generated interfaces from the GraphQL types we just defined.

Screenshot 2019-11-05 at 08.58.56

Now we can finally create our resolvers in our product.resolvers.ts-file. Looking in the file we notice it looks a lot like a service class from Angular. With the power of decorators and dependency injection we notice how the framework helps us being consistent with our resolvers.

Screenshot 2019-11-05 at 08.59.36

All we need now is to implement the service and we are ready to test the resolvers. The products.service.ts-file is where we fetch the data from a database or other sources.

Screenshot 2019-11-05 at 09.00.10

Now run the application and see that it's working ! You can use either the playground or Postman/Insomnia.

You are now ready to go HAM and implement your new NodeJs server in a new strict way!

Insights fra de bedste softwaretalenter

En del af IT Minds’ DNA er, at vi spotter og tiltrækker de bedste softwaretalenter, som er stærke i de nyeste teknologier og frameworks. Derfor vil vi gerne dele ud af vores viden og erfaringer vores konsulenter opsamler, når de afprøver og arbejder med nye teknologier og frameworks.

Seneste indlæg