An argument or input field of a generated scalar can require things of the types its value names. Each @assertType names one component of the value and a condition its type must meet; repeat the directive and every one must hold. The requirement is a statement about the argument, not the scalar, so one scalar serves arguments with different requirements.
SDL signature
directive @assertType(component: String!, type: TypeCondition!) repeatable
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
input TypeCondition @oneOf {
is: String
implements: String
anyOf: [TypeCondition!]
allOf: [TypeCondition!]
not: TypeCondition
}
Parameters
| Name | Type | Description |
|---|---|---|
|
|
The component of the value whose type the condition judges, one of its kind’s: |
|
|
The condition the component’s type must meet. |
A condition sets exactly one field:
| Field | Holds when the type |
|---|---|
|
is the named type (object, interface or union) |
|
implements the named interface, directly or through another interface |
|
meets at least one of the conditions |
|
meets every one of the conditions |
|
does not meet the condition |
Canonical example
mottakersti: Path!
@assertType(component: "startType", type: {implements: "Node"})
@assertType(component: "endType", type: {is: "Soker"})
kontekststi: Path!
@assertType(component: "startType", type: {implements: "Hendelse"})
@assertType(component: "endType", type: {anyOf: [{is: "Sak"}, {is: "Soknad"}]})
mottaker: Path! @assertType(component: "endType", type: {implements: "Utboksmottaker"})
When it runs
A requirement runs where the call that takes the value decodes it, right after the decode, against the schema the service serves. A value that fails one is answered like any wrong value, a client error at the argument naming the requirement, and nothing is called:
input.mottakersti: the path's endType `Saksbehandler` is not `Soker`
input.kontekststi: the path's endType `Sak` does not meet {anyOf: [{is: "Soknad"}, {is: "SoknadLevert"}]}
A null value is not checked. Rules relating two arguments, a context path ending where a recipient path starts, stay in the function or method, written over the type names the components hand over.
Constraints
Graphitron checks when it generates:
-
the element is of a generated scalar, else
ASSERT_TYPE_ELEMENT_NOT_GENERATED; -
componentis one of the value’s components, elseASSERT_TYPE_COMPONENT_UNKNOWN; -
isnames an object, interface or union in this subgraph, elseASSERT_TYPE_IS_UNKNOWN; -
implementsnames an interface in it, elseASSERT_TYPE_IMPLEMENTS_UNKNOWN; -
some call decodes the element, else
GENERATED_SCALAR_UNDECODED, so no requirement is silently skipped.
A condition setting no field, or two, is refused by graphql-java when the schema is assembled.