• Latest
GraphQL Core Concepts You Should Definitely Know

GraphQL Core Concepts You Should Definitely Know

January 31, 2022
The Legend of Bum-bo launches with 10 unobtainable achievements

The Legend of Bum-bo launches with 10 unobtainable achievements

July 4, 2022
My Hero Academia Battle Royale Gets New Trailer and Beta Announcement

My Hero Academia Battle Royale Gets New Trailer and Beta Announcement

July 4, 2022
Dragonflight Explains New Talent Tree System, Previews Priest Trees

Dragonflight Explains New Talent Tree System, Previews Priest Trees

July 4, 2022
New Cult Of The Lamb Trailer Highlights Best Practices For Keeping Your Cult Happy

New Cult Of The Lamb Trailer Highlights Best Practices For Keeping Your Cult Happy

July 4, 2022
9to5Mac Daily: January 14, 2022 – iOS 15 adoption, iPhone 14 cameras

9to5Mac Daily: July 04, 2022 – More on AirPods Pro 2, Steve Jobs award

July 4, 2022
PowerA’s Kirby Controller Is The Perfect 30th Anniversary Accessory

PowerA’s Kirby Controller Is The Perfect 30th Anniversary Accessory

July 4, 2022
iQOO 9T is coming to India: company confirms Snapdragon 8+ Gen 1 chipset

iQOO 9T is coming to India: company confirms Snapdragon 8+ Gen 1 chipset

July 4, 2022
Kirby And The Walk Down Memory Lane: A Series Retrospective

Kirby And The Walk Down Memory Lane: A Series Retrospective

July 4, 2022
Understanding OAuth 2.0 – DZone Security

Understanding OAuth 2.0 – DZone Security

July 4, 2022
Best July 4th deals: Latest from Apple, Google, and more

Best July 4th deals: Latest from Apple, Google, and more

July 4, 2022
NativeScript vs. Flutter: A Comparison

NativeScript vs. Flutter: A Comparison

July 4, 2022
A look back at early smartphones and PDAs

A look back at early smartphones and PDAs

July 4, 2022
Advertise with us
Monday, July 4, 2022
Bookmarks
  • Login
  • Register
GetUpdated
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

GraphQL Core Concepts You Should Definitely Know

January 31, 2022
in Software Development
Reading Time:5 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


GraphQL is a remarkable tool to build APIs. However, it is quite different from REST. This can make it difficult for developers who have been accustomed to working with REST. There are some core concepts of GraphQL that are important to understand how GraphQL actually works.

We will be covering those core concepts in this post. However, if you are completely new to GraphQL, you should start with our Introduction to GraphQL and then, return to this post.

1 – Schema Definition Language (SDL)

GraphQL has its own type system. This system is used to define the schema of an API. Basically, the syntax for writing schemas is known as Schema Definition Language or SDL.

Let us see what a schema looks like:

type Author {
   name: String!
   country: Int!
}

This schema has two fields – the name of the author and the country. Both of these fields are of type String. The ! mark signifies that these are mandatory fields.

We can also express relationships between types. For example, an author can write a book. We can describe this relation as below:

type Book {
   title: String!
   publishYear: Int!
   author: Author!
}

We can also place the other side of the relation as follows:

type Author {
   name: String!
   country: Int!
   books: [Book!]!
}

Basically, this is a one-to-many relationship between author and book. Here, the books field is an array of books.

2 – Queries

Queries are one of the fundamental reasons for using GraphQL. At its core, GraphQL is a query language.

In REST API, we fetch data from specific endpoints. Each endpoint has a particular structure. In other words, the client needs to adhere to the API structure. Basically, the request URL determines the query parameters in a REST API.

However, GraphQL has a considerably different approach. Instead of multiple endpoints, a GraphQL server typically exposes only one endpoint. The structure of the response is not fixed. Instead, it is quite flexible. Basically, the client specifies what data is required and the server responds with the required data.

This means that the client needs to send more information to the server. This information is nothing but the query.

Let’s look at a simple example of a query:

Here, allBooks is the root part of the query. What follows the root is the payload of the query. In this particular example, we only specify a single field i.e. title.

The query will return something as below:

{
  "allBooks": [
    { "title": "Eye of the World" },
    { "title": "The Way of Kings" },
    { "title": "The Mistborn" }
  ]
}

Each book only has the title field in the response even though there might be more fields in the schema definition.

In case we want to fetch more fields, we can tweak our query as below:

{
  allBooks {
    title
    publishYear
  }
}

Here, we also added publishYear in the query.

GraphQL also supports hierarchical queries. For example, if we wish to also query books along with the author, we can describe the same structure in the query.

{
  allAuthors {
    name
    books {
      title
    }
  }
}

With this simple tweak, the GraphQL server will make sure to return author information along with the books written by that author.

3 – Mutations

While queries are great, we also need to update information using APIs. GraphQL supports updating data using the concept of mutations. We can create, update as well as delete data using mutations.

Mutations also follow a similar structure as queries. However, the only difference is the use of the mutation keyword.

Let’s take a simple example of creating a new author:

mutation {
  createAuthor(name: 'Brandon Sanderson', country: "USA") {
    name
    country
  }
}

A mutation also has a root field. Here, the root field is createAuthor. This field takes two arguments – the name of the author and the country. Just like with queries, we can also ask for return parameters. In our case, we are asking for the name and country. Basically, after successful execution of the mutation, the server will return the output with the name and country of the newly created author.

We can also ask the server to return an id in case we have an id field in the schema. See below:

mutation {
  createAuthor(name: 'Brandon Sanderson', country: "USA") {
    id
  }
}

4 – Subscriptions

Another one of the important GraphQL Core Concepts is around the topic of subscriptions. In many modern applications, it is important to have a real-time connection between server and client so that the client can be immediately informed about important events.

Subscriptions don’t follow the typical request-response cycle. When a client subscribes to an event, it will hold the connection. When a particular event occurs, the server pushes the data to the client.

Subscriptions are also written using the same format as queries and mutations. See the below example:

subscription {
  newAuthor {
    name
    country
  }
}

Once the client sends the above subscription request to the server, a connection is opened between them. Whenever a new mutation happens that creates a new author, the server sends information to the client as below:

{
  "newAuthor": {
    "name": "Robert Jordan",
    "country": "USA"
  }
}

Conclusion

As you can see, we have successfully gone through the Core Concepts of GraphQL. We understood the concept of Schema and how your clients can interact with GraphQL servers using queries, mutations, and subscriptions.

If you have any comments or queries about this post, please mention them in the comments section below.



Source link

ShareSendTweet
Previous Post

Mobile Mondays: 10 things on my iPhone 14 wish list

Next Post

Swords Of Legends Online To Go F2P As Part Of The Launch Of Its First Expansion

Related Posts

Understanding OAuth 2.0 – DZone Security

July 4, 2022
0
0
Understanding OAuth 2.0 – DZone Security
Software Development

In a traditional client-server authentication model, a resource owner shares their credentials with the client so that the client can...

Read more

NativeScript vs. Flutter: A Comparison

July 4, 2022
0
0
NativeScript vs. Flutter: A Comparison
Software Development

With the growing demand for lifestyle and communication apps, mobile app development has become a booming industry. Building apps for...

Read more
Next Post
Swords Of Legends Online To Go F2P As Part Of The Launch Of Its First Expansion

Swords Of Legends Online To Go F2P As Part Of The Launch Of Its First Expansion

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?