I have a question with graphQL (actually I have a lot because I just started using it, but this one is punctual)
Let's imagine that I declare a type
, for example Usuarios
.
type Usuario {
username: String!
password: String!
}
And then I have the queryUsuarios
Querys {
getUsuarios: [Usuario]!
}
If I define this query that instead of returning {Users} , I destruct it and hide the password in this way
{ ...Users, password: null }
That's when all the questions come in.
The fact that I Usuario
have defined in the type password: String!
makes the backend, when it wants to return the user created with the password to null, throw me an error saying that it cannot return null from a non-nullable property.
So, what is the correct way to say that a variable must be of a strict type, but at the same time be able to ''nullify'' it in case it is sensitive data?
On the other hand I don't quite understand the difference between returning [User], [User!], [User]! or [User!]!
From already thank you very much
I am also studying
graphQL
, I share my notes.You want to query a list of users with
graphQL query language
:The
resolver Query
expects a list type [User] with zero or more values , defined in theObject type Usuario
.List Object Type with exclamation mark
With the exclamation mark "!", it is specified that a list cannot receive a value of type
null
. If the list received this value,graphQL
it would return the following message:Here I want to make a point very clear:
List Object type without exclamation mark
If the Query were defined without "!" in the list
[Usuario]
:When doing the query, assuming that the list
[Usuario]
for some reason comes as a type valuenull
, graphQL would return the following message:Object type with exclamation mark
If the Query were defined with double "!", one for the list
[Usuario]
and one for the typeUsuario
:It is specifying, for the type
Usuario
, that all the fields of each element of the listUsuario
must be included.For example, if the list
Usuarios
had only one elementUsuario
where the field did not existpassword
, graphQL would return the following message:And more importantly, the other elements of the list would be discarded , even if these had the field
password
, graphQL would return the following message:Object type without exclamation mark
If the Query were defined without "!" in the type
Usuario
:It is specifying that not all the fields of each element of the list
Usuario
must be included.For example, if the list
Usuarios
had only one elementUsuario
where the field did not existpassword
, graphQL would return the following message:However, graphQL would return list items that have the field
password
.