- Docs
- API reference
- Endpoints and schemas
- GraphQL
- Directives
@coalesce
Returns the first non-null value from the referenced fields.
Directives. This directive runs after the query has its answer. It changes what you see, not what the query reads.
Returns the first non-null value from the referenced fields. Example: _fn @coalesce(refs: [“a.aa”, “b.bb”]) returns the referenced value of ‘a’ if non-null, otherwise the value referenced by ‘b.bb’.
Arguments
Section titled “Arguments”| Argument | Type | Description |
|---|---|---|
ref |
String |
A dot path to a list in the response. The directive returns the first item of that list that is not null. If the value is not a list, the result is null. |
refs |
[String!] |
A list of dot paths, written as for ref. The directive takes the list of their values as its input. In a chain of directives, only the first directive reads ref or refs, and each later directive takes the output of the one before it. |
Where it goes
Section titled “Where it goes”FIELD.
Example
Section titled “Example”query { search(searchInput: { entityType: BRAND name: "Safeway" conditions: { limit: 1 } }) { ... on Brand { id label: _fn @coalesce(refs: ["websites.edges.0.node.website", "names.edges.0.node.name"]) websites(first: 1) { edges { node { website } } } names(first: 1) { edges { node { name } } } } }}Response, as returned:
{ "data": { "search": [ { "id": "0f26fbef-3436-4f85-8bcd-31fe12a4f99a", "label": "https://www.safeway.com", "websites": { "edges": [ { "node": { "website": "https://www.safeway.com" } } ] }, "names": { "edges": [ { "node": { "name": "SAFEWAY" } } ] } } ] }}