GraphQL Schema and Types (Part-2)
In the Part-1 of our GraphQL series, we learned about the advantages of GraphQL over other API standards. In short, GraphQL is a data query and manipulation language for APIs. It is internally a POST API (REST standard) with a query or mutation in the body as a payload. We’ll come to know more about query and mutation in upcoming blogs.
In case you like this blog, each clap 👏 is appreciated! ✨
What is a GraphQL Schema?
At its core, a GraphQL schema defines the structure and capabilities of your API. It acts as a contract between the server and the clients, specifying the available data types, operations, and relationships. It is defined in a file with *.graphql or *.graphqls extension. The below code is an example of how schema looks.
type User {
id: ID!
name: String!
email: String!
age: Int
posts: [Post]
}
GraphQL Type System
The GraphQL type system defines the various data types that can be queried or mutated in the API. Here are some fundamental types provided by GraphQL:
a. Scalar Types: Scalar types represent primitive values, such as String, Int, Float, Boolean, and ID. They are atomic and cannot have subfields.
We can also provide our custom scalar like the below:
scalar Date
b. Object Types: Object types represent complex entities with multiple fields, each of which can be of a scalar or object type. Object types can define relationships between other object types, forming a graph-like structure.
c. Enumeration Types: Enumeration types define a fixed set of possible values. They are useful when you have a predefined list of options, such as the status of an order (PENDING, PROCESSING, COMPLETED).
enum OrderStatus {
PENDING
PROCESSING
COMPLETED
}
d. List and Non-Null Types: GraphQL allows the creation of lists ([Type]) and non-null types (Type!) to represent arrays and mandatory fields, respectively. These modifiers enhance flexibility in defining the shape of the data.
type Character {
name: String!
appearsIn: [Episode]!
}
e. Interfaces Types: An Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface.
interface Animal {
id: ID!
name: String!
}
type Dog implements Animal {
id: ID!
name: String!
breed: String!
}
f. Union types: Union types are very similar to interfaces, but they don’t get to specify any common fields between the types.
union SearchResult = Human | Droid | Starship
g. Input types: So far, we’ve only talked about passing scalar values, like enums or strings, as arguments into a field. But you can also easily pass complex objects. In the GraphQL schema language, input types look exactly the same as regular object types, but with the keyword input
instead of type
.
input ReviewInput {
stars: Int!
commentary: String
}
Conclusion
GraphQL’s schema and type system provide a powerful mechanism for building flexible, self-documented APIs, and precisely tailored to clients’ needs. By defining the schema, types, and relationships, developers can efficiently query and mutate data while ensuring type safety and eliminating over-fetching or under-fetching issues commonly associated with RESTful APIs.
In case you were unable to understand some concepts, you will get to know all about them in the upcoming blogs, till then Stay tuned! ✌️