@@ -383,6 +383,46 @@ IsOutputType(type):
383
383
- Return {true }.
384
384
- Return {false }.
385
385
386
+ ### Input Coercion
387
+
388
+ CoerceInputValue (type, value, variableValues):
389
+
390
+ - Assert : {IsInputType (type)}.
391
+ - If {type } is a Non -Null type :
392
+ - If {value } is {null }, raise a _coercion failure_ .
393
+ - Let {innerType } be the _inner type_ of {type }.
394
+ - Return the result of calling {CoerceInputValue (innerType, value,
395
+ variableValues)}.
396
+ - If {value } is {null }, return {null }.
397
+ - If {type } is a List type , return the result of {CoerceInputListValue (type,
398
+ value, variableValues)}.
399
+ - If {type } is an Input Object type , return the result of
400
+ {CoerceInputObjectValue (type, value, variableValues)}.
401
+ - If {type } is an Enum type , return the result of {CoerceInputEnumValue (type,
402
+ value)}.
403
+ - Otherwise , return the result of {CoerceScalarValue (type, value)}.
404
+
405
+ Input coercion defines the requirements for how an value is transformed into a
406
+ canonical form expected by the internal GraphQL service , including the
407
+ resolution of any variables (see {ResolveVariable ()}). This allows GraphQL to
408
+ provide consistent guarantees about inputs to field resolution atop any
409
+ service 's internal runtime or network serialization .
410
+
411
+ :: Failure to meet these requirements results in _coercion failure_ , which may
412
+ occur during different phases of a GraphQL service , each of which results in a
413
+ different type of error . As a result , each phase specifically describes how to
414
+ handle a coercion failure :
415
+
416
+ - During schema construction (i.e. a default value for an argument definition),
417
+ failure results in an invalid schema.
418
+ - During document validation (i.e. a value literal directly within an
419
+ operation), failure results in document invalidation, which raises a _request
420
+ error_.
421
+ - During coercion of values provided for variables as part of a request, failure
422
+ results in a _request error_.
423
+ - During coercion of field arguments, failure results in an _execution error_,
424
+ however validation and variable value coercion should make this uncommon.
425
+
386
426
### Type Extensions
387
427
388
428
TypeExtension :
@@ -502,10 +542,17 @@ information on the serialization of scalars in common JSON and other formats.
502
542
503
543
**Input Coercion**
504
544
545
+ CoerceScalarValue (scalarType , value):
546
+
547
+ - Assert : value is not {null }, which is already handled in {CoerceInputValue ()}.
548
+ - Return the result of calling the internal method provided by the type system
549
+ for determining the “input coercion ” of {scalarType } given the value {value }.
550
+ This internal method must return a valid value for the type and not {null }.
551
+ Otherwise raise a _coercion failure_ .
552
+
505
553
If a GraphQL service expects a scalar type as input to an argument , coercion is
506
554
observable and the rules must be well defined . If an input value does not match
507
- a coercion rule, a _request error_ must be raised (input values are validated
508
- before execution begins ).
555
+ a coercion rule , it results in _coercion failure_ .
509
556
510
557
GraphQL has different constant literals to represent integer and floating -point
511
558
input values , and coercion rules may apply differently depending on which type
@@ -516,8 +563,9 @@ floating-point values, they are interpreted as an integer input value if they
516
563
have an empty fractional part (ex. `1.0`) and otherwise as floating -point input
517
564
value .
518
565
519
- For all types below , with the exception of Non -Null , if the explicit value
520
- {null } is provided , then the result of input coercion is {null }.
566
+ Note : {null } is a valid input value for all scalar types except those wrapped by
567
+ a _Non -Null type_ , however {null } value coercion is handled in
568
+ {CoerceInputValue ()} before {CoerceScalarValue ()} is called .
521
569
522
570
### Int
523
571
@@ -544,10 +592,9 @@ greater than or equal to 2<sup>31</sup>, an _execution error_ should be raised.
544
592
**Input Coercion **
545
593
546
594
When expected as an input type , only integer input values are accepted . All
547
- other input values , including strings with numeric content , must raise a request
548
- error indicating an incorrect type . If the integer input value represents a
549
- value less than -2<sup >31</sup > or greater than or equal to 2<sup >31</sup >, a
550
- _request error_ should be raised .
595
+ other input values , including strings with numeric content result in _coercion
596
+ failure_ indicating an incorrect type . Input values less than -2<sup >31</sup > or
597
+ greater than or equal to 2<sup >31</sup > result in _coercion failure_ .
551
598
552
599
Note : Numeric integer values larger than 32-bit should either use String or a
553
600
custom -defined Scalar type , as not all platforms and transports support encoding
@@ -578,10 +625,10 @@ coerced to {Float} and must raise an _execution error_.
578
625
When expected as an input type , both integer and float input values are
579
626
accepted . Integer input values are coerced to Float by adding an empty
580
627
fractional part , for example `1.0` for the integer input value `1`. All other
581
- input values , including strings with numeric content , must raise a _request
582
- error_ indicating an incorrect type . If the input value otherwise represents a
583
- value not representable by finite IEEE 754 (e.g. {NaN}, {Infinity}, or a value
584
- outside the available precision), a _request error_ must be raised .
628
+ input values , including strings with numeric content , result in _coercion
629
+ failure_ indicating an incorrect type . Input values not representable by finite
630
+ IEEE 754 (e.g. {NaN}, {Infinity}, or a value outside the available precision)
631
+ result in _coercion failure_ .
585
632
586
633
### String
587
634
@@ -604,8 +651,7 @@ value, or the string `"1"` for the integer `1`.
604
651
**Input Coercion **
605
652
606
653
When expected as an input type , only valid Unicode string input values are
607
- accepted . All other input values must raise a _request error_ indicating an
608
- incorrect type .
654
+ accepted . All other input values result in _coercion failure_ .
609
655
610
656
### Boolean
611
657
@@ -624,7 +670,7 @@ Examples of this may include returning `true` for non-zero numbers.
624
670
**Input Coercion **
625
671
626
672
When expected as an input type , only boolean input values are accepted . All
627
- other input values must raise a _request error_ indicating an incorrect type .
673
+ other input values result in _coercion failure_ .
628
674
629
675
### ID
630
676
@@ -647,9 +693,8 @@ When coercion is not possible they must raise an _execution error_.
647
693
648
694
When expected as an input type , any string (such as `"4" `) or integer (such as
649
695
`4` or `-4`) input value should be coerced to ID as appropriate for the ID
650
- formats a given GraphQL service expects . Any other input value , including float
651
- input values (such as `4.0`), must raise a _request error_ indicating an
652
- incorrect type .
696
+ formats a given GraphQL service expects . All other input values , including float
697
+ input values (such as `4.0`), result in _coercion failure_ .
653
698
654
699
### Scalar Extensions
655
700
@@ -914,7 +959,7 @@ executor, see [Value Completion](#sec-Value-Completion).
914
959
915
960
**Input Coercion**
916
961
917
- Objects are never valid inputs.
962
+ Object types are never valid inputs. See [Input Objects]( #sec-Input-Objects) .
918
963
919
964
**Type Validation**
920
965
@@ -938,8 +983,8 @@ of rules must be adhered to by every Object type in a GraphQL schema.
938
983
returns {true }.
939
984
4. If argument type is Non -Null and a default value is not defined :
940
985
1. The `@deprecated ` directive must not be applied to this argument .
941
- 5. If the argument has a default value it must be compatible with
942
- { argumentType } as per the coercion rules for that type .
986
+ 5. If the argument has a default value , { CoerceInputValue (argumentType,
987
+ defaultValue)} must not result in _coercion failure_ .
943
988
3. An object type may declare that it implements one or more unique interfaces .
944
989
4. An object type must be a super -set of all interfaces it implements :
945
990
1. Let this object type be {objectType }.
@@ -1044,6 +1089,26 @@ May return the result:
1044
1089
The type of an object field argument must be an input type (any type except an
1045
1090
Object, Interface, or Union type).
1046
1091
1092
+ **Default Values **
1093
+
1094
+ GetDefaultValue (inputValueDefinition):
1095
+
1096
+ - Assert : {inputValueDefinition } has a {DefaultValue }, as this is only called
1097
+ when true .
1098
+ - Let {inputType } be the type of {inputValueDefinition }.
1099
+ - Let {defaultValue } be the default value of {inputValueDefinition }
1100
+ - Return the result of {CoerceInputValue (inputType, defaultValue, null)}. (Note :
1101
+ default values must not contain variable references )
1102
+
1103
+ A default value may be provided for a field argument , input object field , or
1104
+ variable definitions . If at runtime a value is not provided , the coerced default
1105
+ value is used instead .
1106
+
1107
+ Note : Implementations are encouraged to optimize the coercion of a default value
1108
+ by doing so only once and caching the resulting coerced value . This should not
1109
+ result in request execution _coercion failure_ since all default values should
1110
+ be validated before a request .
1111
+
1047
1112
### Field Deprecation
1048
1113
1049
1114
Fields in an object may be marked as deprecated as deemed necessary by the
@@ -1291,6 +1356,8 @@ Interface types have the potential to be invalid if incorrectly defined.
1291
1356
arguments may share the same name .
1292
1357
3. The argument must accept a type where {IsInputType (argumentType)}
1293
1358
returns {true }.
1359
+ 4. If a argument has a default value , {CoerceInputValue (argumentType,
1360
+ defaultValue)} must not result in _coercion failure_ .
1294
1361
3. An interface type may declare that it implements one or more unique
1295
1362
interfaces , but may not implement itself .
1296
1363
4. An interface type must be a super -set of all interfaces it implements :
@@ -1517,15 +1584,24 @@ reasonable coercion is not possible they must raise an _execution error_.
1517
1584
1518
1585
**Input Coercion **
1519
1586
1520
- GraphQL has a constant literal to represent enum input values . GraphQL string
1521
- literals must not be accepted as an enum input and instead raise a request
1522
- error .
1587
+ CoerceInputEnumValue (type, value):
1588
+
1589
+ - Assert : value is not {null }, which is already handled in {CoerceInputValue ()}.
1590
+ - If {value } represents a name (such as {EnumValue}), let {name } be the name of
1591
+ {value }:
1592
+ - If Enum {type } has a member named {name }, let it be {member }:
1593
+ - Return the internal value representing {member }.
1594
+ - Otherwise raise a _coercion failure_ .
1595
+
1596
+ GraphQL has a constant literal , {EnumValue }, used to represent enum input
1597
+ values . GraphQL {StringValue } literals must not be accepted as an enum input and
1598
+ instead result in a _coercion failure_ .
1523
1599
1524
1600
Variable transport serializations which have a different representation for
1525
1601
non -string symbolic values (for example,
1526
1602
[EDN](https ://github .com /edn -format /edn )) should only allow such values as enum
1527
1603
input values . Otherwise , for most transport serializations that do not , strings
1528
- may be interpreted as the enum input value with the same name .
1604
+ may be interpreted as the enum member of the same name .
1529
1605
1530
1606
**Type Validation **
1531
1607
@@ -1649,11 +1725,47 @@ type of an Object or Interface field.
1649
1725
1650
1726
**Input Coercion **
1651
1727
1728
+ CoerceInputObjectValue (type, value, variableValues):
1729
+
1730
+ - If {value } is not an {ObjectValue } or map of values , raise a _coercion
1731
+ failure_ .
1732
+ - Let {coercedValues } be an empty map .
1733
+ - Let {inputFieldDefinitions } be the input fields defined by {type }:
1734
+ - For each {inputFieldDefinition } in {inputFieldDefinitions }:
1735
+ - Let {inputFieldName } be the name of {inputFieldDefinition }.
1736
+ - Let {inputFieldType } be the expected type of {inputFieldDefinition }.
1737
+ - Assert : {IsInputType (inputFieldType)}, because of
1738
+ [type validation ](#sec-Input-Objects.Type-Validation).
1739
+ - If {value} has an entry with name {inputFieldName}, let {inputFieldValue} be
1740
+ its value :
1741
+ - If {inputFieldValue } is a {Variable }, let {variableName} be its name :
1742
+ - Let {isProvided }, {variableValue} be the result of
1743
+ {ResolveVariable(type, variableName, variableValues)}.
1744
+ - If {isProvided } is {true }, add an entry to {coercedValues } named
1745
+ {inputFieldName } with the value {variableValue }.
1746
+ - Otherwise if {inputFieldDefinition } has a default value , add an entry to
1747
+ {coercedValues } named {inputFieldName } with the value
1748
+ {GetDefaultValue (inputFieldDefinition)}.
1749
+ - Otherwise :
1750
+ - Let {coercedValue } be the result of {CoerceInputValue (inputFieldType,
1751
+ inputFieldValue, variableValues)}.
1752
+ - Add an entry to {coercedValues } named {inputFieldName } with the value
1753
+ {coercedValue }.
1754
+ - Otherwise if {inputFieldDefinition } has a default value , add an entry to
1755
+ {coercedValues } named {inputFieldName } with the value
1756
+ {GetDefaultValue (inputFieldDefinition)}.
1757
+ - Otherwise if {inputFieldType } is a Non -Null type , raise an _coercion
1758
+ failure_ .
1759
+ - Return {coercedValues }.
1760
+
1761
+ Note : This algorithm is very similar to {CoerceArgumentValues ()}, as both are
1762
+ defined with {InputValueDefinition }.
1763
+
1652
1764
The value for an input object should be an input object literal or an unordered
1653
- map supplied by a variable , otherwise a _request error_ must be raised . In
1765
+ map supplied by a variable , otherwise it results in _coercion failure_ . In
1654
1766
either case , the input object literal or unordered map must not contain any
1655
- entries with names not defined by a field of this input object type , otherwise a
1656
- request error must be raised .
1767
+ entries with names not defined by a field of this input object type , otherwise
1768
+ it results in _coercion failure_ .
1657
1769
1658
1770
The result of coercion is an unordered map with an entry for each field both
1659
1771
defined by the input object type and for which a value exists . The resulting map
@@ -1723,6 +1835,9 @@ input ExampleInputObject {
1723
1835
returns {true }.
1724
1836
4. If input field type is Non -Null and a default value is not defined :
1725
1837
1. The `@deprecated ` directive must not be applied to this input field .
1838
+ 5. If the input object field has a default value ,
1839
+ {CoerceInputValue (inputObjectType, defaultValue)} must not result in
1840
+ _coercion failure_ .
1726
1841
3. If an Input Object references itself either directly or through referenced
1727
1842
Input Objects , at least one of the fields in the chain of references must be
1728
1843
either a nullable or a List type .
@@ -1819,19 +1934,44 @@ about this behavior.
1819
1934
1820
1935
**Input Coercion**
1821
1936
1937
+ CoerceInputListValue(type, value, variableValues):
1938
+
1939
+ - Assert : value is not {null }, which is already handled in {CoerceInputValue ()}.
1940
+ - Let {coercedValues } be an empty list .
1941
+ - Let {itemType } be the _inner type_ of {type }.
1942
+ - If {value } is a list :
1943
+ - For each {itemValue } in {value }:
1944
+ - If {itemValue } is a {Variable }, let {variableName } be its name :
1945
+ - Let {isProvided }, {variableValue } be the result of
1946
+ {ResolveVariable (type, variable, variableValues)}.
1947
+ - Append {variableValue } to {coercedValues }. (Note : Variables without
1948
+ provided values are replaced with {null })
1949
+ - Otherwise :
1950
+ - Let {coercedValue } be the result of {CoerceInputValue (itemType,
1951
+ itemValue, variableValues)}.
1952
+ - Append {coercedValue } to {coercedValues }.
1953
+ - Otherwise :
1954
+ - Let {coercedValue } be the result of {CoerceInputValue (itemType, value,
1955
+ variableValues)}.
1956
+ - Append {coercedValue } to {coercedValues }.
1957
+ - Return {coercedValues }.
1958
+
1822
1959
When expected as an input , list values are accepted only when each item in the
1823
1960
list can be accepted by the list 's item type .
1824
1961
1825
- If the value passed as an input to a list type is _not_ a list and not the
1826
- {null} value, then the result of input coercion is a list of size one, where the
1827
- single item value is the result of input coercion for the list's item type on
1828
- the provided value (note this may apply recursively for nested lists).
1962
+ If the value passed as an input to a list type is _not_ a list ( and not the
1963
+ {null} value) , then the result of input coercion is a list of size one , where
1964
+ the single item value is the result of input coercion for the list 's item type
1965
+ on the provided value (which may apply recursively for nested lists).
1829
1966
1830
1967
This allows inputs which accept one or many arguments (sometimes referred to as
1831
1968
"var args" ) to declare their input type as a list while for the common case of a
1832
1969
single value , a client can just pass that value directly rather than
1833
1970
constructing the list .
1834
1971
1972
+ Variables provided as items within a list are resolved to their coerced runtime
1973
+ value if provided , otherwise {null } is used in place of an unprovided value .
1974
+
1835
1975
Following are examples of input coercion with various list types and values :
1836
1976
1837
1977
| Expected Type | Provided Value | Coerced Value |
@@ -1884,14 +2024,14 @@ within the Execution section.
1884
2024
1885
2025
**Input Coercion**
1886
2026
1887
- If an argument or input-object field of a Non-Null type is not provided, is
1888
- provided with the literal value {null}, or is provided with a variable that was
1889
- either not provided a value at runtime, or was provided the value {null}, then a
1890
- _request error_ must be raised .
2027
+ Non-null inputs are _required_, if an argument, variable, or input-object field
2028
+ of a Non-Null type is not provided, is provided with the literal value {null},
2029
+ or is provided with a variable that was either not provided a value at runtime,
2030
+ or was provided the value {null}, it results in a _coercion failure_ .
1891
2031
1892
2032
If the value provided to the Non-Null type is provided with a literal value
1893
2033
other than {null}, or a Non-Null variable value, it is coerced using the input
1894
- coercion for the wrapped type.
2034
+ coercion for the wrapped type (see {CoerceInputValue()}) .
1895
2035
1896
2036
A non -null argument cannot be omitted :
1897
2037
0 commit comments