The simplest GraphQL API with NodeJS – Apollo Server – REST

Here you can see a small exercise of queries to a public Pokemon API where you can consult the REST API with NodeJS, and then with the help of Apollo Server provide your own GraphQL API.

In this blog we highlight the importance and ease it gives us for this case Graphql. There is currently a lot of information about Graphql, if you want to know more details, if you need more information or you do not have something clear you can consult Graphql or comment your question and we will gladly give you an answer, personally I recommend the official documentation, It is quite clear and concise, however it does not detract from any of the other sources that know Graphql very well.
With respect to its comparison with REST there are also many blogs, videos, “influencers” in the technological world with articles on the subject; some will say that REST is better, others will say Graphql is better, others only make comparisons and others think that the integration between the two could work and solve many headaches.

The idea of ​​this little practice is to combine the two technologies in the simplest and most readable way possible with node js as server.

Thus,
– We create a my_project folder.
– We enter the my_project folder and type npm i and create / configure the package.json from the terminal.
– We must have the following architecture creating the folders and files:

src
    index.js
package.json

Our package.json must contain the following dependencies:

To clarify a bit:
apollo-server: it will allow us to build our Graphql API.
Graphql: is our language of queries, that is, the way we will request our data from the API.
node-fetch: we will use it as a channel to make calls to our API.

Ready this, I show you the easiest and fastest way for our API to be configured and everything in the src/index.js file should look like this:

What does the code do?

typeDefs: I create the type of object I am consulting and define the fields that I am going to consult / use.

resolvers: are the methods responsible for calling the query action.

This explanation is extremely brief, since as I announced above regarding all the resources that Graphql has.

When writing to the npm run start terminal, the Graphql interface will open in our default browser to start our queries.

As we can see, our Graphql API is working by connecting to the Pokemons API.

But, what is it for? Well, most API’s are made in REST, and we are exposed that at some point we need to make our Graphql API from data that is only consulted via REST. In addition to that, I think that we should always experience all the technologies that are offered to us, and not necessarily discard existing ones, we could instead and depending on each project strengthen the knowledge with this type of integrations.

You can get the code here: github.com

I hope you take advantage and remember any questions, comments or input is welcome in our comments section.

About 4Geeks:

4Geeks is a global product development and growth marketing company, and all-in-between, focused on 10X ROI for startups, small and mid-size companies around the world. 4Geeks serves industries like E-Commerce & Retail, Startups, HealthTech, Marketing, Banking & FinTech and Real Estate. Headquartered in United States, and nearshore development centers in Mexico and Costa Rica. Pura Vida!

[30% off] Detect missed growth opportunities for your businessClaim an Audit
+