@@ -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 : A {null } value is a valid input for all input types except the _Non -Null
567
+ type_ , however this is handled in {CoerceInputValue ()} before
568
+ {CoerceScalarValue ()} is called .
521
569
522
570
### Int
523
571
@@ -545,9 +593,9 @@ greater than or equal to 2<sup>31</sup>, an _execution error_ should be raised.
545
593
546
594
When expected as an input type , only integer input values are accepted . All
547
595
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 .
596
+ error indicating an incorrect type . Integer input values representing a value
597
+ less than -2<sup >31</sup > or greater than or equal to 2<sup >31</sup > result in
598
+ _coercion failure_ .
551
599
552
600
Note : Numeric integer values larger than 32-bit should either use String or a
553
601
custom -defined Scalar type , as not all platforms and transports support encoding
@@ -579,9 +627,9 @@ When expected as an input type, both integer and float input values are
579
627
accepted . Integer input values are coerced to Float by adding an empty
580
628
fractional part , for example `1.0` for the integer input value `1`. All other
581
629
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 .
630
+ error_ indicating an incorrect type . Input values representing a value not
631
+ representable by finite IEEE 754 (e.g. {NaN}, {Infinity}, or a value outside the
632
+ available precision) result in _coercion failure_ .
585
633
586
634
### String
587
635
@@ -604,8 +652,7 @@ value, or the string `"1"` for the integer `1`.
604
652
**Input Coercion **
605
653
606
654
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 .
655
+ accepted . All other input values result in _coercion failure_ .
609
656
610
657
### Boolean
611
658
@@ -624,7 +671,7 @@ Examples of this may include returning `true` for non-zero numbers.
624
671
**Input Coercion **
625
672
626
673
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 .
674
+ other input values result in _coercion failure_ .
628
675
629
676
### ID
630
677
@@ -647,9 +694,8 @@ When coercion is not possible they must raise an _execution error_.
647
694
648
695
When expected as an input type , any string (such as `"4" `) or integer (such as
649
696
`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 .
697
+ formats a given GraphQL service expects . All other input values , including float
698
+ input values (such as `4.0`), result in _coercion failure_ .
653
699
654
700
### Scalar Extensions
655
701
@@ -914,7 +960,7 @@ executor, see [Value Completion](#sec-Value-Completion).
914
960
915
961
**Input Coercion**
916
962
917
- Objects are never valid inputs.
963
+ Object types are never valid inputs. See [Input Objects]( #sec-Input-Objects) .
918
964
919
965
**Type Validation**
920
966
@@ -938,8 +984,8 @@ of rules must be adhered to by every Object type in a GraphQL schema.
938
984
returns {true }.
939
985
4. If argument type is Non -Null and a default value is not defined :
940
986
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 .
987
+ 5. If a default value is defined , { CoerceInputValue (argumentType,
988
+ defaultValue)} must not result in _coercion failure_ .
943
989
3. An object type may declare that it implements one or more unique interfaces .
944
990
4. An object type must be a super -set of all interfaces it implements :
945
991
1. Let this object type be {objectType }.
@@ -1044,6 +1090,26 @@ May return the result:
1044
1090
The type of an object field argument must be an input type (any type except an
1045
1091
Object, Interface, or Union type).
1046
1092
1093
+ **Default Values **
1094
+
1095
+ GetDefaultValue (inputValueDefinition):
1096
+
1097
+ - Assert : {inputValueDefinition } has a {DefaultValue }, as this is only called
1098
+ when true .
1099
+ - Let {inputType } be the type of {inputValueDefinition }.
1100
+ - Let {defaultValue } be the default value of {inputValueDefinition }
1101
+ - Return the result of {CoerceInputValue (inputFieldType, defaultValue, null)}.
1102
+ (Note : default values must not contain variable references )
1103
+
1104
+ A default value may be provided for each field argument , input object field , and
1105
+ variable definitions . If at runtime a value is not provided , the default value
1106
+ is used instead .
1107
+
1108
+ Note : Implementations are encouraged to optimize the coercion of a default value
1109
+ by doing so only once and caching the resulting coerced value . This should not
1110
+ result in runtime _coercion failure_ since default values are validated before
1111
+ this point .
1112
+
1047
1113
### Field Deprecation
1048
1114
1049
1115
Fields in an object may be marked as deprecated as deemed necessary by the
@@ -1291,6 +1357,8 @@ Interface types have the potential to be invalid if incorrectly defined.
1291
1357
arguments may share the same name .
1292
1358
3. The argument must accept a type where {IsInputType (argumentType)}
1293
1359
returns {true }.
1360
+ 4. If a default value is defined , {CoerceInputValue (argumentType,
1361
+ defaultValue)} must not result in _coercion failure_ .
1294
1362
3. An interface type may declare that it implements one or more unique
1295
1363
interfaces , but may not implement itself .
1296
1364
4. An interface type must be a super -set of all interfaces it implements :
@@ -1517,15 +1585,24 @@ reasonable coercion is not possible they must raise an _execution error_.
1517
1585
1518
1586
**Input Coercion **
1519
1587
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 .
1588
+ CoerceInputEnumValue (type, value):
1589
+
1590
+ - Assert : value is not {null }, which is already handled in {CoerceInputValue ()}.
1591
+ - If {value } represents a name (such as {EnumValue}), let {name } be the name of
1592
+ {value }:
1593
+ - If Enum {type } has a member named {name }, let it be {member }:
1594
+ - Return the internal value representing {member }.
1595
+ - Otherwise raise a _coercion failure_ .
1596
+
1597
+ GraphQL has a constant literal , {EnumValue }, used to represent enum input
1598
+ values . GraphQL {StringValue } literals must not be accepted as an enum input and
1599
+ instead result in a _coercion failure_ .
1523
1600
1524
1601
Variable transport serializations which have a different representation for
1525
1602
non -string symbolic values (for example,
1526
1603
[EDN](https ://github .com /edn -format /edn )) should only allow such values as enum
1527
1604
input values . Otherwise , for most transport serializations that do not , strings
1528
- may be interpreted as the enum input value with the same name .
1605
+ may be interpreted as the enum member of the same name .
1529
1606
1530
1607
**Type Validation **
1531
1608
@@ -1649,11 +1726,47 @@ type of an Object or Interface field.
1649
1726
1650
1727
**Input Coercion **
1651
1728
1729
+ CoerceInputObjectValue (type, value, variableValues):
1730
+
1731
+ - If {value } is not an {ObjectValue } or map of values , raise a _coercion
1732
+ failure_ .
1733
+ - Let {coercedValues } be an empty map .
1734
+ - Let {inputFieldDefinitions } be the input fields defined by {type }:
1735
+ - For each {inputFieldDefinition } in {inputFieldDefinitions }:
1736
+ - Let {inputFieldName } be the name of {inputFieldDefinition }.
1737
+ - Let {inputFieldType } be the expected type of {inputFieldDefinition }.
1738
+ - Assert : {IsInputType (inputFieldType)}, because of
1739
+ [type validation ](#sec-Input-Objects.Type-Validation).
1740
+ - If {value} has an entry with name {inputFieldName}, let {inputFieldValue} be
1741
+ its value :
1742
+ - If {inputFieldValue } is a {Variable }, let {variableName} be its name :
1743
+ - Let {isProvided }, {variableValue} be the result of
1744
+ {ResolveVariable(type, variableName, variableValues)}.
1745
+ - If {isProvided } is {true }, add an entry to {coercedValues } named
1746
+ {inputFieldName } with the value {variableValue }.
1747
+ - Otherwise if {inputFieldDefinition } has a default value , add an entry to
1748
+ {coercedValues } named {inputFieldName } with the value
1749
+ {GetDefaultValue (inputFieldDefinition)}.
1750
+ - Otherwise :
1751
+ - Let {coercedValue } be the result of {CoerceInputValue (inputFieldType,
1752
+ inputFieldValue, variableValues)}.
1753
+ - Add an entry to {coercedValues } named {inputFieldName } with the value
1754
+ {coercedValue }.
1755
+ - Otherwise if {inputFieldDefinition } has a default value , add an entry to
1756
+ {coercedValues } named {inputFieldName } with the value
1757
+ {GetDefaultValue (inputFieldDefinition)}.
1758
+ - Otherwise if {inputFieldType } is a Non -Null type , raise an _coercion
1759
+ failure_ .
1760
+ - Return {coercedValues }.
1761
+
1762
+ Note : This algorithm is very similar to {CoerceArgumentValues ()}, as both are
1763
+ defined with {InputValueDefinition }.
1764
+
1652
1765
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
1766
+ map supplied by a variable , otherwise it results in _coercion failure_ . In
1654
1767
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 .
1768
+ entries with names not defined by a field of this input object type , otherwise
1769
+ it results in _coercion failure_ .
1657
1770
1658
1771
The result of coercion is an unordered map with an entry for each field both
1659
1772
defined by the input object type and for which a value exists . The resulting map
@@ -1723,6 +1836,8 @@ input ExampleInputObject {
1723
1836
returns {true }.
1724
1837
4. If input field type is Non -Null and a default value is not defined :
1725
1838
1. The `@deprecated ` directive must not be applied to this input field .
1839
+ 5. If a default value is defined , {CoerceInputValue (inputObjectType,
1840
+ defaultValue)} must not result in _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