type Hotel {hotelName: String!rating: Int!hotelLocation: Location!}type Location {address: String!}type Query {hotel(query: String): [Hotel]}
Non-null
fields are those that have an exclamation mark (!
) next to their type. When we query these fields, they will not return a null value. Here’s a query:
hotel {hotelNamehotelLocation {address}}
We can get the following outcomes:
// no restaurants exist{ hotels: null }// all data present{hotels: [{hotelName: "pearl continental",hotelLocation: {address: "Street 85, San Francisco"}}]}
// hotelName or hotelLocation not be null// this result would be incorrect{hotel: [{hotelName: null,hotelLocation: null}]}
To ensure the structure of the response, GraphQL may also ensure the presence of specific fields when queried.
Unlock your potential: GraphQL series, all in one place!
To continue your exploration of GraphQL, check out our series of Answers below:
What is GraphQL?
Get an overview of GraphQL, its core principles, and how it enhances API interactions.
What are abstract types in GraphQL?
Understand abstract types like interfaces and unions, and how they provide flexibility in your GraphQL queries.
What are non-null constraints in GraphQL?
Discover the role of non-null constraints in ensuring data integrity within GraphQL schemas.
What are different ways to pass an argument in GraphQL?
Explore various methods for passing arguments in GraphQL to customize queries and mutations for specific needs.
GraphQL vs. REST
Compare GraphQL with REST, understanding the strengths and differences between the two approaches.
How to set up GraphQL in a React app
Learn how to integrate GraphQL into React apps to create dynamic, data-driven user interfaces.
Free Resources