What Types Of GraphQL Variables Are Allowed?

9 minutes read

GraphQL allows four types of variables to be used in a query or mutation:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Top Rated JavaScript Books to Read in January 2024

1
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

Rating is 5 out of 5

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

2
Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

Rating is 4.9 out of 5

Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

3
Web Design with HTML, CSS, JavaScript and jQuery Set

Rating is 4.8 out of 5

Web Design with HTML, CSS, JavaScript and jQuery Set

4
HTML, CSS, and JavaScript All in One: Covering HTML5, CSS3, and ES6, Sams Teach Yourself

Rating is 4.6 out of 5

HTML, CSS, and JavaScript All in One: Covering HTML5, CSS3, and ES6, Sams Teach Yourself

5
Head First JavaScript Programming: A Brain-Friendly Guide

Rating is 4.4 out of 5

Head First JavaScript Programming: A Brain-Friendly Guide

6
Mastering JavaScript Functional Programming: Write clean, robust, and maintainable web and server code using functional JavaScript, 2nd Edition

Rating is 4.3 out of 5

Mastering JavaScript Functional Programming: Write clean, robust, and maintainable web and server code using functional JavaScript, 2nd Edition

7
Murach's JavaScript and jQuery (4th Edition)

Rating is 4 out of 5

Murach's JavaScript and jQuery (4th Edition)


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:

  1. 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.
  2. Single Operation: GraphQL variables can only be used within a single operation. They cannot be shared between multiple operations in the same request.
  3. 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.
  4. 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.
  5. Variable Names: Variable names must be valid GraphQL names and should not start with a reserved keyword like "true", "false", or "null".
  6. 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.
  7. 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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

When working with GraphQL, nested types refer to the situation where a type is defined within another type. Resolving nested types correctly is crucial for querying and returning the desired data in a GraphQL API.To resolve nested types in GraphQL, you need to...
In GraphQL, scalar types like String, Int, Float, Boolean, and ID are used to represent simple data types. However, sometimes you may need to work with custom or non-native data types that are not included by default in GraphQL. In such cases, you can implemen...
To fetch API data for the GraphQL schema, you need to follow these steps:Set up a GraphQL client: Choose a suitable client library for your preferred programming language. Popular options include Apollo Client, Relay, and GraphQL.js. Define your GraphQL schema...