Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of GraphQL's ID Scalar?

Tags:

graphql

https://graphql.org/learn/schema/#scalar-types

I have read the description

ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

But in practice, what actually changes if I use ID instead of String ?

like image 525
ed' Avatar asked Aug 27 '18 07:08

ed'


People also ask

What does scalar mean in GraphQL?

Scalars are “leaf” values in GraphQL. There are several built-in scalars, and you can define custom scalars, too. (Enums are also leaf values.) The built-in scalars are: String , like a JSON or Ruby string.

Does GraphQL require ID?

Require id field for all GraphQL entities in all queries and mutations.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).

What are the three types of operations in GraphQL?

GraphQL works by sending operations to an endpoint. There are three types of operations: queries, mutations, and subscriptions. A query is sent through an HTTP POST call to retrieve data. A mutation is also sent through a HTTP POST and is used to modify data.


1 Answers

Nothing changes as it is mentioned in the description. ID is serialized exactly the same as String type; therefore, if you were to replace ID with String, computer would see no difference in it.

However, what is important to you is that YOU as a programmer know for sure that it's a unique identifier which will be useful when you paginate records, modify apollo cache, iteratively create jsx components.

So in practice, it has a very practical implication - it conveys important meaning for the programmer that the field is unique for the type.

like image 170
forJ Avatar answered Sep 22 '22 15:09

forJ