Today, I decided to dive into the GraphQL hype! (Maybe a few years too late - but it's alright. What's important is we never stop learning!)
"One Endpoint to Rule Them All"
The concept is fairly simple to understand. Unlike REST API where we consume different endpoints depending on the resource we need, in GraphQL you only need one for everything.
That's not enough details (I'm sure), so I am going to list a few resources that I have personally curated to help you jumpstart your weekend project:
- What is GraphQL? - by LevelUpTuts, discussing the fundamentals
- REST vs GraphQL APIs, the Good, the Bad, the Ugly - by Derric Gilling, an in-depth comparison between REST and GraphQL
- Building a GraphQL Server - by TraversyMedia, discussing concepts through application
- Integrating Apollo and Express to build a Node.js GraphQL API - by Maximiliano Duthey, for structure and integration-specific code tutorial
I was following this great tutorial by XOOR, when the end of the article I encountered an error in ApolloServer:
graphqlExpress is not a function
You can still follow through the tutorial without updating Apollo-Server-Express, with a few changes in the code, as follows:
gql tag in your schema
gql tag is used for editor syntax highlighting and auto-formatting with Prettier.
The tutorial divides the language types in a
schema directory, which is great for code maintainability, but it also means you have to include the tag per file (
As you can see, the code is more readable as it is being autoformatted. By the way, I'm using Atom editor.
bodyParser is already included in the
apollo-server-express, so you no longer need the
body-parser package. Moreover,
graphiqlExpress are replaced by
ApolloServer, which you can wrap as a middleware to
GraphQLExpress have been replaced by
ApolloServer with the additional
resolvers as parameters.
ApolloServer, like so:
Take note of the
schema variable, as it is imported in
That's it! If you followed the instructions properly, the GraphQL Playground should show up in your specified endpoint.