Docs
Launch GraphOS Studio

Operation Arguments


can define as part of their definitions, the values of these arguments can be passed to arguments or arguments (eg. @skip and @include).

generates type-safe initializers for your that accept your operation's arguments. These arguments can be

,
custom scalars
,
enums
, or
input objects
defined in your schema.

Let's say we define a GraphQL named HeroName with an episode which is a custom Episode enum defined in our schema:

HeroNameQuery.graphql
query HeroName($episode: Episode!) {
hero(episode: $episode) {
name
}
}

Apollo iOS will generate a HeroNameQuery class with :

HeroNameQuery.graphql.swift
class HeroNameQuery: GraphQLQuery {
...
var episode: Episode
init(episode: Episode) {
self.episode = episode
}
}

Your HeroNameQuery will have a property for the episode , which will be passed to any field or directive arguments that use the $episode variable in the operation definition.

This query object can be initialized and passed to ApolloClient.fetch(query:):

apollo.fetch(query: HeroNameQuery(episode: .empire)) { result in
guard let data = try? result.get().data else { return }
print(data.hero.name) // Luke Skywalker
}

Working with nullable arguments

When defining an operation argument with a nullable value, Apollo iOS will wrap the generated argument's type in a generic

wrapper enum.

According to

, explicitly providing a null value for an input value to a field argument is semantically different from not providing a value at all (nil). This enum allows you to distinguish your input values between null and nil.

If the HeroName query is defined with a nullable episode argument, the generated HeroNameQuery will have an episode field with the type GraphQLNullable<Episode>:

HeroNameQuery.graphql.swift
class HeroNameQuery: GraphQLQuery {
...
var episode: GraphQLNullable<Episode>
init(episode: GraphQLNullable<Episode>) {
self.episode = episode
}
}

The HeroNameQuery can be initialized with a GraphQLNullable value:

Null value
.init(episode: .null)
No value
.init(episode: .none)
.some case
.init(episode: .some(.empire))

Or with an optional value using the

to provide a fallback.

.some case
let optionalEpisode: Episode?
.init(episode: optionalEpisode ?? .none)

For more usage information see the

.

Default values

When defining your operation's arguments, you may provide default values for the arguments. These default arguments will be included in your generated operation's initializer:

HeroNameQuery.graphql
query HeroName(
$episode: Episode! = .EMPIRE
) {
hero(episode: $episode) {
name
}
}
HeroNameQuery.graphql.swift
class HeroNameQuery: GraphQLQuery {
...
var episode: Episode
init(episode: Episode = .empire) {
self.episode = episode
}
}

Note: This only applies for operation arguments defined by the client.

Default values for on

are defined by the schema, and not generated.

Previous
Fragments
Next
Error Handling
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company