![]() |
GraphQL is a powerful open-source Query 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-NullLists 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.
Lists in GraphQL Schema
1. Nullable Lists
Syntax : type Query{ 2. Non-Nullable Lists
Syntax : type Query{ Example for ListsIn the below example, create a GraphQL schema to define a list of users. Step 1: Define the SchemaLet’s create a schema and save the file as schema.graphql. type Query { Step 2: Implement ResolversLet’s set up the server, save the file as server.js and we will implement resolvers for the users list. Javascript
Step 3: Test Your List in GraphiQLRun your GraphQL server. node server.js Output: Step 4: Execute the Below Query in the GraphiQL Interface{ Output: We will receive a response with the list of users ![]() List of users Passing Null ValuesIf 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” ![]() Pass users list as null Output: ![]() Error when users list is passed as null When a non-null field userID is not passed we will get an error like “Cannot return null for non-nullable field User.userID.” ![]() UserID is not passed Output: ![]() Error when non-null field userID is not passed Non-Null in GraphQL Schema
Why do We Need Non-Null?The use of non-null fields in GraphQL is essential for a few reasons:
Syntax: fieldName : dataType! //Non-null Field Example for Non-Null FieldIn this example, we will create a GraphQL schema to define a list of persons with name of the person as null. Step 1: Define a Non-Null Field in Your SchemaLet’s create a schema and save the file as schema.graphql. type Query { In this example, name is a non-null field. This implies that every person must have name when the query is executed. Step 2: Implement ResolversLet’s set up the server, save the file as server.js and we will implement resolvers for the name field. Javascript
Step 3: Test Your List in GraphiQLRun your GraphQL server. node server.js Step 4: Execute the Below Query in the GraphiQL Interfacequery { 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. ![]() Error due to null passed in Non-null field Handling the Non-Null Errors
Javascript
![]() Name of the person with non-null value Output: ![]() Output of Person without error ConclusionLists 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. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |