GraphQL, what is it?

Quick explanation of GraphQL for beginners with examples. Why use it? How does it work? Where to start?

Find more tutorials explaining the basics of a subject in ten minutes with the tag what is it.

GraphQL is a technology used to query an API. It is available for main programming languages just by adding the library to your project: no need for a new machine or any system installation.

GraphQL has nothing to do with SQL or any database related technology. There is however some similarities in the basic concepts:

  • There is a client and a server.
  • The client sends a human readable request containing some filters and the expected fields in the response.
  • These fields can be complex objects, allowing to follow links between data.
  • The language uses a vocabulary based on the data handling: query, mutation.

Example of GraphQL query

{ tutorial(id: "graphql-what-is-it") { title, description, tags { name }, author { name, social } } }

Example of GraphQL response

{ "data": { "tutorial": { "title": "GraphQL, what is it?", "description": "Quick explanation of GraphQL for beginners with examples. Why use it? How does it work? Where to start?", "tags": [ { "name": "what-is-it" }, { "name": "api" }, { "name": "graphql" } ], "author": { "name": "Nicolas Gautron", "social": { "linkedin": "https://fr.linkedin.com/in/nicolasgautron", "twitter": "https://twitter.com/gautronnicolas", "github": "https://github.com/nicolas-gautron" } } } } }

GraphQL is mainly used through HTTP in order to replace a REST architecture.

The main advantages of GraphQL over REST:

  • Only one HTTP request for querying linked data.
  • Only the needed data for your use case are retrieved.
  • The request variables and the response fields are strongly typed by the GraphQL engine.
  • You can name the request, easing the logging and the debugging: improving the maintainability.

Any HTTP client can be used. Postman, for instance, natively include GraphQL in its last versions.

Learn more about GraphQL on the official site.

Have a nice day :)