Middleware: GraphQL

Davin Iddo
Moodah POS
Published in
3 min readDec 6, 2019

Why Middleware?

In Moodah, we used a middleware to connect the back-end and the front-end. The reason for this is that we don’t want the front-end to take care of the API end-point, that’s where GraphQL comes in. GraphQL will take care of any multiple API calls and handle the naming conventions.

Apollo Server

To use these GraphQL functions, we are using an Apollo Server. The use of Apollo enables the front-end to make a calls and receive the information back from the back-end, not only that, Apollo works well with the serverless environment that Moodah is in.

There will be 2 types of function: Query and Mutation. The Query function is to only fetch the necessary information from the back-end, on the other hand, the Mutation function is to change/alter the information that’s reside in the back-end.

To show how we do it, I will be showing some examples that we have in our project.

MUTATION

One of the most important task is to perform an authentication to the back-end. We use the Mutation function to create a token to connect to the back-end server.

The codes above show how we do the authentication. The first we are to see is the type, which is called SignInType. The type consists of data that we expect to get from the backend, which are: username, isSuperuser, and the sessionToken.

Next is the args, which is the argument/input that the middleware will receive from the front-end. The SignInInputType for the authentication consists of db(database), username, and password. Then the args will be passed down into the resolve argument.

Now inside the resolver, in Moodah, we created configureService that will require 3 parameter; operation, error, and success. This service will create a secure connection to our back-end automatically inside it. After putting the arguments, we used an extension library called nodoo that calls the http simulation in the operation argument, in this case we are to create a none authentication, that does not have any credential, and followed immediately by createAuthenticate.

The two next arguments are simply what happen when the operation resulted in a success or failure, in our case when the error happened we have a message ready to tell us what went wrong (most of the time), while when the opposite happen we will sent the 3 datas expected back into the front-end.

CRUD (Create, Read, Update, Delete)

Apollo server also let us to call a function that can access and alter the database under a function. These functions are all necessary actions that we can called to the Odoo API.

The database require the token for us to be able to access the information lay within. We have the getDataSet function that receive the context as an argument, the context consist of the sessionToken to access this database.

The getDataSet function and Create Function

For the three functions in CRUD; Create, Update, and Delete we will have to use the Mutation for we are about to change the database itself. Most of them will need to sent the modelName and the ids to select which part of the database that they want to alter.

QUERY

Now for the last CRUD, which is the Read function, is the only one that we will be using inside the Query rather than the Mutation, although the placement is different most of the code structure are the same. The difference is Read will be counted as separated under the Query value.

There’s more to say on how we call the functions from Odoo, the used of Nodoo helps greatly in this matter.

Conclusion

With the use of GraphQL as a Middleware in our project, we are able to provide a clean structured of our codes. In doing so, we are able to make the flow of our project to be easier to maintain throughout our time in working on them.

--

--

Davin Iddo
Moodah POS

A useless blob whose memory is worse than a goldfish