![]() |
VOOZH | about |
GraphQL is a powerful open-sourceQuery Language for APIs. In 2012 it was first developed by a team of developers in Facebook and then it was later made public to the general people by making it open-source. Now it is being maintained by the GraphQL community.
GraphQL is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. The benefit of using single endpoint queries is that they help the user in reducing fetching too much data or less amount of data than required.
Lists and Non-Null are two important data types in GraphQL that are used to define GraphQL Schema. They are useful in defining the structure and constraints of data returned by GraphQL queries. Lists make the query fetching easier by helping the user to fetch the sequence of values required. Non-null constraints on a field help the users prevent errors in the code while fetching the data.
Prerequisites:
Below are the prerequisites that we need to install before executing the GraphQL query on our laptop or desktop.
Syntax :
type Query{
listName : [ dataType ], //Nullable List
}
Syntax :
type Query{
listName : [ dataType] ! //Non-nullable List
}
In the below example, create a GraphQL schema to define a list of users.
Let's create a schema and save the file as schema.graphql.
type Query {
users: [User]! #Non-Null List - User
}
type User {
userID: ID! #Non-Null field - userID
userName: String
userEmail: String
}
Let's set up the server, save the file as server.js and we will implement resolvers for the users list.
Run your GraphQL server.
node server.js
Output:
{
users {
userID
userName
userEmail
}
}
Output:
We will receive a response with the list of users
If we pass null value for users list like the below code we will get an error like "Cannot return null for non-nullable field Query.users"
Output:
When a non-null field userID is not passed we will get an error like "Cannot return null for non-nullable field User.userID."
Output:
The use of non-null fields in GraphQL is essential for a few reasons:
Syntax:
fieldName : dataType! //Non-null Field
In this example, we will create a GraphQL schema to define a list of persons with name of the person as null.
Let's create a schema and save the file as schema.graphql.
type Query {
person: [Person]
}
type Person {
name: String! # Non-Null field
age: Int
}
In this example, name is a non-null field. This implies that every person must have name when the query is executed.
Let's set up the server, save the file as server.js and we will implement resolvers for the name field.
Run your GraphQL server.
node server.js
query {
person {
name
age
}
}
Output:
So we will receive a response with the list of persons with the below error because we have passed the name of the person as null.
Here person name is a non-null field so it will not allow null value.
Output:
Lists and non-null constraints are crucial elements in GraphQL schema definition. Lists represent arrays or collections of values, and they can be either nullable or non-nullable. Non-null constraints ensure that certain fields always have a value, contributing to data integrity and making the API more predictable for users.