Selection
type to generate a concrete type for your selection (e.g. Selection<String, Objects.Human>
). You can find all generated phantom types by typing
Unions.
Interfaces.
Objects.
Operations.
Selection.
that contains type-alias for unions, interfaces, objects and operations. In that case, you don’t have to specify the type-lock anymore and simply provide the return type (e.g. Selection.Human<String>
).
Selecting Fields
Unions
When fetching a union you should provide selections for each of the union sub-types. Additionally, all of those selections should resolve to the same type.
Interfaces
Interfaces are very similar to unions. The only difference is that you may query for a common field from the intersection.
Transforming Selections
Nullable, List, and Non-Nullable Selection
Selection packs a collection of utility functions that let you select nullable and list fields using your existing selecitons. Each selection comes with three calculated properties that let you do that:list
- to query listsnullable
- to query nullable fieldsnonNullOrFail
- to query nullable fields that should be there
Selection
static functions .list
, .nullable
, and .nonNullOrFail
.
Making selection on the entire type
You might want to write a selection on the entire type from the selection composer itself. This usually happens if you have a distinct identifier reused in many types. Consider the following scenario where we have anid
field in Human
type. There are many cases where we only query id
field from the Human
that’s why we create a human id selection.
selection
helper method that lets you make a selection on the whole TypeLock
from inside the selection.
Human
again.
Mapping Selection
You might want to map the result of your selection to a new type and get a selection for that new type. You can do that by calling amap
function on selection and provide a mapping.
⚠️ Don’t make any nested calls to the API. Use the first half of the initializer to fetch all the data and return the calculated result. Just don’t make nested requests.
Validating Data
Since SwiftGraphQL uses functions to create selections, you may validate recieved data before turning it into a Swift structure. This way, you can use more structure to represent your data and make stricter requirements than those imposed by schema. You can easily terminate resolution by throwing an error inside your selection.NOTE: Always make all selection before throwing errors!
NOTE:age
andisGoodDog
values are nullable in our schema but aren’t nullable in our model.
Operations on selection
Selections are quite useless on their own - they feel a bit like skeletons. Their true (and only) power comes from the two functions they expose -query
and decode
.
query
returns a spec-compliant GraphQL query and a variable set that you should use in your request.decode
accepts the body of the response and returns the decoded result.
query
anddecode
don’t actually communicate with the server. To do that, use one of the official clients or create your own.
Besidesquery
anddecode
selection also contains wide range of utility functions that help you understand the structure of a given query. Those functions won’t be covered here. You should follow their comments to understand what they do and examine official clients to see how they are used.