The following is a query that retrieves menu items whose names match pizza
by using a document literal for the matching argument:
{menuItems(matching: "pizza"){name}}
The literal for the string argument is in double quotation marks (""
), and the argument values are provided after the argument name followed by a colon (:
).
Variables in GraphQL act as type placeholders for values that are sent with the request. This is a familiar concept to us if we’ve used parameterized SQL queries for value insertion and sanitization. Before they’re utilized with the operation type, GraphQL variables are specified with their types.
query ($term: String) {menuItems(matching: $term) {name}}
In the code above, we use the String
type for our $term
variable since that’s exactly the type of argument value that we defined for the matching argument in our schema.
query ($term: String) {menuItems(matching: $term) {name}}# Query variable{"term":"pizza"}
We get the following result:
{"data":{"menuItems": [{"name": "Hot pizza"}]}}
Unlock your potential: GraphQL series, all in one place!
To continue your exploration of GraphQL, check out our series of Answers below:
What is GraphQL?
Get an overview of GraphQL, its core principles, and how it enhances API interactions.
What are abstract types in GraphQL?
Understand abstract types like interfaces and unions, and how they provide flexibility in your GraphQL queries.
What are non-null constraints in GraphQL?
Discover the role of non-null constraints in ensuring data integrity within GraphQL schemas.
What are different ways to pass an argument in GraphQL?
Explore various methods for passing arguments in GraphQL to customize queries and mutations for specific needs.
GraphQL vs. REST
Compare GraphQL with REST, understanding the strengths and differences between the two approaches.
How to set up GraphQL in a React app
Learn how to integrate GraphQL into React apps to create dynamic, data-driven user interfaces.
Free Resources