What is an exclamation point in GraphQL?
What is an exclamation point in GraphQL?
In GraphQL, an exclamation point (!
) is used to denote that a field is non-nullable. This means that the field must always return a value and cannot be null
. The exclamation point can be applied to both scalar types and custom object types within the GraphQL schema.
By default, scalar types in GraphQL, such as String
, Int
, Float
, Boolean
, and ID
, are nullable, meaning they can return a null
value. However, by adding an exclamation point to the type definition, you can specify that the field must always return a value and cannot be null
. For example:
type ExampleType {
someField: String!
}
In this example, someField
is a non-nullable String
field, meaning it must always return a string value and cannot be null
[2][4][6].
Similarly, the exclamation point can be used to denote non-nullable fields in custom object types. For instance:
type ExampleType {
someObject: AnotherType!
}
Here, someObject
is a non-nullable field of type AnotherType
, indicating that it must always return an instance of AnotherType
and cannot be null
[2][4][6].
The exclamation point can also be used with lists to ensure that the list itself or the items within the list are non-nullable. For example:
type Query {
listOfStrings: [String!]!
}
In this case, listOfStrings
is a non-nullable list of non-nullable strings, meaning the list itself cannot be null
, and none of its items can be null
[5][7][10].
Wh...
entry
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào