GraphQL allows four types of variables to be used in a query or mutation:
- Scalar Variables: These variables are used to represent simple values such as integers, floats, booleans, strings, and enums. For example, you can declare a variable called userId of type ID to represent a user's unique identifier.
- Input Object Variables: These variables are used to represent complex objects, similar to JSON. You can define an input object type in your schema and use it as a variable. For instance, you may have an input type called UserInput with fields such as name, email, and password, which can be used as a variable in a mutation.
- List Variables: These variables are used to represent an array of values. You can declare a variable as a list of any other variable type. For example, if you have a variable userIds of type [ID], it represents an array of user identifiers.
- Non-Null Variables: By default, variables in GraphQL are nullable, meaning they can have a null value. However, you can also define a variable as non-null by adding an exclamation point after its type. For instance, name: String! denotes that the name variable must have a non-null string value.
These variable types provide flexibility in passing different kinds of data to your GraphQL queries and mutations, enabling a dynamic and interactive API development experience.
What is the scope of GraphQL variables?
In GraphQL, variables are used to pass dynamic values to a query or mutation. The scope of variables is limited to the specific operation in which they are defined.
More specifically, the scope of a variable is the operation definition (query or mutation) in which it is declared, and any nested selections within that operation. Variables cannot be accessed outside of the operation in which they are defined.
This means that variables cannot be used in different operations or in different fields within the same operation. Each operation can only access the variables defined within its own scope.
For example, if you have a query with a variable $userId
defined in its scope, you cannot access $userId
in any mutations or other queries. Similarly, a variable defined in one field selection cannot be used in another field selection within the same query or mutation.
The scope of variables ensures that they are isolated and can only be used where they are intended, providing a mechanism for passing dynamic values to specific parts of a GraphQL operation.
What are the limitations of GraphQL variables?
There are a few limitations of GraphQL variables:
- Scalar Types: GraphQL variables can only accept scalar types (such as String, Int, Float, Boolean). They cannot accept input types like objects, interfaces, or unions.
- Single Operation: GraphQL variables can only be used within a single operation. They cannot be shared between multiple operations in the same request.
- Variable Definitions: Variable definitions are required for every variable used in a GraphQL request. This means that you need to define a variable and assign a type to it before using it, which can lead to longer and more complex requests.
- No Variable Default Values: GraphQL does not support default values for variables. If a variable is not provided a value in the request, it will throw an error and the request will fail.
- Variable Names: Variable names must be valid GraphQL names and should not start with a reserved keyword like "true", "false", or "null".
- Variable Types: GraphQL variables must have an explicit type, and the provided variable value must match that type. There is no type coercion or automatic type conversion.
- Variable Limitations: Depending on the implementation and server configuration, there might be limitations on the number or size of variables that can be used in a single GraphQL request.
How to define an enum variable in GraphQL?
In GraphQL, an enum type is a special kind of scalar that is restricted to a particular set of allowed values. Enum values are defined using the enum
keyword. Here's an example of how to define an enum variable in GraphQL:
1 2 3 4 5 6 7 8 9 10 11 |
enum Color { RED GREEN BLUE } type User { id: ID! name: String! status: Color! } |
In the above example, we defined an enum type Color
that represents three possible values: RED
, GREEN
, and BLUE
. The status
field in the User
type is of type Color
, meaning it can only have one of the defined enum values.
When querying or mutating data, you can specify the enum value as a string:
1 2 3 4 5 6 7 |
query { user(id: "123") { id name status } } |
In the above query, we are requesting the id
, name
, and status
fields of a user. The status
field is an enum value, and it will be returned as a string representation of the selected enum value (RED
, GREEN
, or BLUE
).
Similarly, when providing input variables in a mutation, you can pass an enum value as a string:
1 2 3 4 5 6 7 |
mutation { updateUserStatus(id: "123", status: "GREEN") { id name status } } |
In the above mutation, we are updating the status
of a user to GREEN
. Again, the enum value is specified as a string.