GraphQL tips and patterns
Practical GraphQL patterns for cleaner requests: variables, operation names, fragments, aliases, and directives.
This article covers patterns that make your GraphQL code cleaner, more maintainable, and easier to debug. If you're new to GraphQL, start with GraphQL basics first.
Variables
When you're testing queries, it's easy to write values directly into the query string. But real applications need to pass different values each time: a user might click on another device, select a different filter, or navigate to the next page.
Variables solve this by separating what you're asking for (the query) from the specific values (the variables). Think of it like a function: the query is the function definition, and variables are the arguments you pass in.
Here's a query without variables:
query GetDevice {
bdr {
device(id: "550e8400-e29b-41d4-a716-446655440001") {
title
}
}
}And here's the same query with a variable:
query GetDevice($deviceId: ID!) {
bdr {
device(id: $deviceId) {
title
}
}
}The variable $deviceId is declared in parentheses after the operation name. The ID! part specifies the type (must match what the schema expects). Then you use $deviceId wherever you need that value in the query.
The actual value is passed separately as JSON:
When you send the request, include both the query and the variables. GraphQL tools and client libraries send both for you, and you provide only the variables object.
Multiple variables
You can declare as many variables as you need:
In this example:
$statusIds: [ID!]!is required (the!after the brackets means the list itself cannot be null)Each item in the list is a non-null
ID(the!inside the brackets)$limit: Int = 20is optional with a default value of 20
Variables in JSON:
Operation names
You can give your operations custom names to make them easier to identify in logs, debugging tools, and network traces.
The name GetDevice appears after the operation type (query, mutation, or subscription). It doesn't affect execution, as the API ignores it, but helps in several ways:
Debugging: Error messages and server logs reference the operation name
Tooling: GraphQL clients use names for caching and request deduplication
Analytics: Track which operations are called most frequently
Readability: Self-documenting code, especially with multiple operations
Mutations benefit from naming too:
Common naming conventions use PascalCase with a verb prefix: GetDevice, ListDevices, UpdateAsset, CreateGeoObject, DeleteSchedule.
Fragments
Fragments let you define reusable sets of fields. They're useful when you need the same fields in multiple queries.
Define a fragment on a specific type:
Use it in queries with the spread operator (...):
Both queries now share the same field selection for core device data.
Inline fragments for interfaces
When working with interfaces like Node, use inline fragments to request type-specific fields:
The node query returns any entity by ID. The inline fragments (... on Type) tell GraphQL what fields to return based on the actual type.
Aliases
Aliases let you rename fields in the response or fetch the same field multiple times with different arguments.
Renaming fields
Response:
Comparing entities
Fetch the same entity at different points or with different parameters:
Batch mutations
Aliases enable batch operations in a single request. In a mutation, you can select only one field inside a single bdr block, so alias the bdr namespace itself, one block per call:
Selecting two mutation fields inside one bdr block returns a validation error instead. This rule applies to mutations only: a query can select as many fields inside one bdr block as it needs, like the examples above.
The aliased blocks execute one after another, in the order they appear, and each mutation runs independently. If one fails, the others can still succeed, so check the errors array. See Partial failure.
There are no dedicated bulk mutations, so aliased blocks are also how you update many entities in one request. For large programmatic batches, send the version you last read for each entity. A concurrent change then fails only that block with CONFLICT, and you re-read and retry that entity alone. See Optimistic locking.
Directives
Directives modify how fields are executed. The API supports the standard GraphQL directives plus one custom directive; see Directives for the full list. The two you use directly in queries are @include and @skip, which turn a field on or off based on a variable.
Directives can be used to:
Hide fields the user can't access
Skip expensive fields when not needed
Fetch fewer fields on mobile devices
See also
GraphQL basics: Learn GraphQL fundamentals, from queries and mutations to the type system
Directives: Complete reference for the standard and custom directives
Common resources: Complete reference for shared scalars, interfaces, and types
Error handling: Understand error structure, codes, and common error scenarios
Last updated
Was this helpful?