Nodejs and Firebase Cloud Functions: GraphQL API

Tram Ho

Introduce

In the previous section , we have completed the Authentication section for the API that works on Firebase Cloud Functions, in this section, we will continue to build the API based on GraphQL and Firebase Realtime DB.

Stack used this time:

  1. The Authentication Module is obtained from the previous section
  2. Firebase Cloud Functions: Serverless (FaaS) Platform
  3. Firebase Realtime Database
  4. Express: Nodejs framework (router, middleware)
  5. Apollo Server: GraphQL server tools

Learn and install

GraphQL

To begin, we’ll find out what the GraphQL pants are, right? There are many explanations on the viblo that are very specific and easy to understand, so I’ll explain briefly in the scope of this article. You can read this article to better understand GraphQL offline 😄 I will summarize by quoting the main points of the article Let’s learn about GraphQL

So, what is GraphQL?

GraphQL is a Graph Query Language for API. It was developed by Facebook. GraphQL has been almost completely replacing REST since its inception, being much more efficient, powerful and flexible.

How is GraphQL different from REST?

The problem that REST is having is that the response of the REST data is too much or too little. In both cases, the performance of the application is significantly affected. The solution that GraphQL offers is to allow data declaration where a client can determine exactly the data they need from an API.

Moreover, instead of having many endpoints like REST, GraphQL has only one endpoint. The client and server interact with each other via the POST protocol

GraphQL Schema Definition Language (SDL)

In order for the Client and the Server to have a common and unified understanding, we need to define the Schema to see what data the Client can access and what the Server returns to the client. Example Schema of User

(!! Means a schema field will be required)

Fetching Data (Query) and Mutation

The client will have to specify which fields are needed to send to the server

For example

Mutations are ways that clients transfer data to the server to perform operations: Create, Update, Delete

For example:

Above are some concepts to use in this article, now go into your code 😄

Dependencies

In addition to the init firebase like the previous articles, this time we need to install some more libraries

Add the necessary libraries to the index.js file

Defining Schema for Apollo Server

I will define a schema as Clothes. In Query, there will be clothes return Clothes, like Mutation will have an addClothes method

Defining resolvers for Apollo Server

Similar to typeDef, Resolver will tell Apollo Server where to get data.

  1. clothes : To fetch all the data from Firebase, because GraphQL needs array data while Firebase returns Object so we need to transform a bit
  2. addClothes : Add a record to Firebase

Finally we will create an instance of ApolloServer, then apply it to the express app

The complete index.js file now:

We start deploying using the command firebase deploy --only functions

Experiment

After successfully deploying we can use some GraphQL client tools like GraphiQL, Postman I will use Postman to test you guys. 😄 As I mentioned above, the Client and Server communicate with each other via the POST method, so they will set up for Postman similar to the image below:

Mutation

I will add a record to the DB and get my name back using the GraphQL query as shown below

Query

Get the name attribute for all records

Get more style attributes as well

Conclusion

Above is my article on Google Firebase services with Nodejs and incorporating some other components that I learned. In addition to the services I used the last 3 articles, there are many other services to learn and apply, see you in the following articles. 😆

Share the news now

Source : Viblo