You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
4 Hrs
42 Min
24 Seg

Listas y non-null types

8/24
Resources

How do you handle 'non-null' values in GraphQL?

Welcome to a fascinating discussion on data type handling in GraphQL, where we will focus on non-null value validation and list implementation. In the world of software development, it is crucial to understand how these concepts work to ensure the integrity of your system and proper data handling.

To ensure that a field cannot be null, GrpahQL offers us the use of an exclamation mark (!) after the data type definition. This means that the value is mandatory and cannot be null. For example, if we define a field as String!, we force it to always return a string and not a null value.

What happens if we do not validate null values?

By default, GraphQL allows nulls to be sent even if we have determined a data type. This general behavior allows flexibility, but lacks robustness in certain scenarios. Let's imagine we have a hello query that must return a String. If we do not validate to avoid null, we could receive a null value without errors. This can be inconvenient if you expect certain data in your application.

type Query { hello: String}

To avoid this, we simply add ! to the data type:

type Query { hello: String!}

What role do lists play in GraphQL?

Lists are fundamental elements that allow the grouping of fields into a single data type. They are defined using square brackets around the desired data type. This translates into an array and allows you to handle more complex data when querying multiple instances of a specific type.

Want to return a list of String? Define your type like this:

type Query { getStrings: [String]}

How do you combine 'non-null' and lists?

By combining the concepts of 'non-null' and lists, we find ourselves with a powerful functionality to manage data in GraphQL. Here is an example of the application of these combinations:

  1. Non-null array, with no internal constraints:

    • Usage: [Int]!
    • Declares that the array itself cannot be null, but the internal values can be.
  2. Non-null internal values in arrays:

    • Usage: [Int!]
    • Ensures that all elements inside the array contain non-null values, while the array itself can be null.
  3. Combination of constraints:

    • Usage: [Int!]!
    • Ensures that neither the array nor its internal values can be null.

For example, to handle lists with non-null in a resolver that returns an array of integers:

type Query { getNumbers(numbers: [Int!]!): [Int!]!} }

In this type definition, both the array and each integer within the array are guaranteed to be non-null. This strengthens data integrity across the entire application stack, ensuring that any data interacts in a consistent and predictable manner.

The combination of these features in GraphQL allows you to create robust applications by ensuring that the data manipulated and returned meets the expected requirements. Keep exploring the world of GraphQL development and don't stop learning!

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Nulabilidad y listas

En GraphQL, no define un nuevo tipo cuando intenta devolver una lista de elementos de un campo; simplemente aplica un modificador de lista a ese tipo, as铆:

type ObjectType {
  singleRestaurant: Restaurant
  multipleRestaurants: [Restaurant]
}

Non-null tambi茅n es un modificador, y resulta que puede aplicar modificadores de listas y no nulos de forma anidada arbitrariamente, especialmente porque las listas se pueden anidar:

type ObjectType {
  first: Restaurant
  second: [Restaurant]
  third: [Restaurant!]
  fourth: [Restaurant!]!
  fifth: [[Restaurant!]]!
}

Entonces, 驴qu茅 significa tener el no nulo dentro o fuera de una lista? Bueno, decide si el no nulo se aplica al elemento de la lista frente a la lista en s铆.

Por ejemplo, puede tener una lista de cadenas no nulas:

drinkSizes: [String!]

Esto significa que la lista en s铆 puede ser nula, pero no puede tener ning煤n miembro nulo. Por ejemplo, en JSON:

drinkSizes: nulo // v谩lido
drinkSizes: [] // v谩lido
drinkSizes: ["peque帽o", "grande"] // v谩lido
drinkSizes: ["peque帽o", nulo, "grande"] // error

Ahora, digamos que definimos una lista de cadenas no nulas:

drinkSizes: [String]!

Esto significa que la lista en s铆 no puede ser nula, pero puede contener valores nulos:

drinkSizes: nulo // error
drinkSizes: [] // v谩lido
drinkSizes: ["peque帽o", "grande"] // v谩lido
drinkSizes: ["peque帽o", nulo, "grande"] // v谩lido

Finalmente, podemos combinar los dos:

drinkSizes: [String!]!

Este es el m谩s restrictivo:

drinkSizes: nulo // error
drinkSizes: [] // v谩lido
drinkSizes: ["peque帽o", "grande"] // v谩lido
drinkSizes: ["peque帽o", nulo, "grande"] // error

Una conclusi贸n interesante aqu铆 es que no hay forma de especificar que la lista no puede estar vac铆a: una lista vac铆a [ ] siempre es v谩lida, independientemente de si la lista o los elementos no son nulos.

[Int]!

[1,2,3] // ok
[] // ok 
null // N/A 
[1,2,null] // ok

[Int!]

[1,2,3] // ok
[] // ok 
null // ok 
[1,2,null] // N/A

[Int!]!

[1,2,3] // ok
[] // ok 
null // N/A 
[1,2,null] // N/A