You are on page 1of 4

GraphQL

Introduction To GraphQL

GraphQL is a query language for your API, and a server-side runtime for
executing queries using a type system you define for your data. GraphQL was
created in 2012 and open sourced by Facebook in 2015. In 2019, Facebook and
others created the GraphQL Foundation as a neutral, non-profit home for the
GraphQL assets and ongoing collaboration, and hosted by The Linux
Foundation. GraphQL isn't tied to any specific database or storage engine and is
instead backed by your existing code and data. A GraphQL service is created by
defining types and fields on those types, then providing functions for each field
on each type.

Companies using GraphQL are

Facebook, Instagram, Shopify, Twitter, StackShare, Stack, The New York


Times, Tokopedia, And Many more.

https://graphql.org/learn/
The Difference Between REST APIs and GraphQL

A REST API is an architectural concept for network-based software. GraphQL,


on the other hand, is a query language, a specification, and a set of tools that
operates over a single endpoint using HTTP. When using a REST API to fetch
information, you’ll always get back a complete dataset. For example, if you
wanted to request information from two objects, you’d need to perform two
REST API requests.

Conversely, if you wanted to gather some information from a specific endpoint,


you couldn’t limit the fields that the REST API returns; you’ll always get a
complete data set. This phenomenon is referred to as over fetching. GraphQL
uses its query language to tailor the request to exactly what you need, from
multiple objects down to specific fields within each entity. GraphQL would take
X endpoint, and it can do a lot with that information, but you have to tell it what
you want first. The advantage of REST APIs is its simplicity

In essence, GraphQL is extremely powerful, once you know how to use it.
Because you are only fetching the data that you require, you limit the amount of
processing required. As you begin to look at automation, these savings really
start to add up.
https://twitter.com/SavvasStephnds/status/1400452152221261824?s=08
The Schema Definition Language (SDL)
GraphQL has its own type system that’s used to define the schema of an API.
The syntax for writing schemas is called Schema Definition Language (SDL).

Most types in your schema will just be normal object types, but there are two
types that are special within a schema:

schema {
query: Query
mutation: Mutation
}

Type system

If you've seen a GraphQL query before, you know that the GraphQL query
language is basically about selecting fields on objects. So, for example, in the
following query

{
hero {
name
appearsIn
}
}
The Query types

Most types in your schema will just be normal object types, but there are two
types that are special within a schema that is query and mutation.
Every GraphQL service has a query type and may or may not have
a mutation type. These types are the same as a regular object type, but they are
special because they define the entry point of every GraphQL query.
For example, here the GraphQL service has Query type
with hero and droid fields:

type Query {
hero(episode: Episode): Character
droid (id: ID): Droid
}

Mutation types
Mutations work in a similar way as Query - you define fields on
the Mutation type, and those are available as the root mutation fields you can
call in your query.

type Mutation {
createMessage(input: MessageInput): Message
updateMessage(id: ID, input: MessageInput): Message
}

You might also like