@@ -13,42 +13,36 @@ the case that a field error occurred on a field which was replaced with null.
13
13
14
14
GraphQL does not require a specific serialization format. However, clients
15
15
should use a serialization format that supports the major primitives in the
16
- GraphQL response. In particular, the serialization format must support
16
+ GraphQL response. In particular, the serialization format must at least support
17
17
representations of the following four primitives:
18
18
19
19
* Map
20
20
* List
21
21
* String
22
22
* Null
23
23
24
- Serialization formats which can represent an ordered map should preserve the
25
- order of requested fields as defined by {CollectFields()} in the Execution
26
- section. Serialization formats which can only represent unordered maps
27
- (such as JSON) should retain this order textually. That is, if two fields
28
- ` {foo, bar} ` were queried in that order, the resulting JSON serialization
29
- should contain ` {"foo": "...", "bar": "..."} ` in the same order.
30
-
31
- Producing a response where fields are represented in the same order in which
32
- they appear in the request improves human readability during debugging and
33
- enables more efficient parsing of responses if the order of properties can
34
- be anticipated.
35
-
36
- A serialization format may support the following primitives, however, strings
37
- may be used as a substitute for those primitives.
24
+ A serialization format should also support the following primitives, each
25
+ representing one of the common GraphQL scalar types, however a string or simpler
26
+ primitive may be used as a substitute if any are not directly supported:
38
27
39
28
* Boolean
40
29
* Int
41
30
* Float
42
31
* Enum Value
43
32
33
+ This is not meant to be an exhaustive list of what a serialization format may
34
+ encode. For example custom scalars representing a Date, Time, URI, or number
35
+ with a different precision may be represented in whichever relevant format a
36
+ given serialization format may support.
37
+
44
38
45
39
### JSON Serialization
46
40
47
- JSON is the preferred serialization format for GraphQL, though as noted above,
48
- GraphQL does not require a specific serialization format. For consistency and
49
- ease of notation, examples of the response are given in JSON throughout the
50
- spec. In particular, in our JSON examples, we will represent primitives using
51
- the following JSON concepts :
41
+ JSON is the most common serialization format for GraphQL. Though as mentioned
42
+ above, GraphQL does not require a specific serialization format.
43
+
44
+ When using JSON as a serialization of GraphQL responses, the following JSON
45
+ values should be used to encode the related GraphQL values :
52
46
53
47
| GraphQL Value | JSON Value |
54
48
| ------------- | ----------------- |
@@ -61,23 +55,37 @@ the following JSON concepts:
61
55
| Float | Number |
62
56
| Enum Value | String |
63
57
64
- ** Object Property Ordering**
58
+ Note: For consistency and ease of notation, examples of responses are given in
59
+ JSON format throughout this document.
60
+
61
+
62
+ ### Serialized Map Ordering
63
+
64
+ Since the result of evaluating a selection set is ordered, the serialized Map of
65
+ results should preserve this order by writing the map entries in the same order
66
+ as those fields were requested as defined by query execution. Producing a
67
+ serialized response where fields are represented in the same order in which
68
+ they appear in the request improves human readability during debugging and
69
+ enables more efficient parsing of responses if the order of properties can
70
+ be anticipated.
71
+
72
+ Serialization formats which represent an ordered map should preserve the
73
+ order of requested fields as defined by {CollectFields()} in the Execution
74
+ section. Serialization formats which only represent unordered maps but where
75
+ order is still implicit in the serialization's textual order (such as JSON)
76
+ should preserve the order of requested fields textually.
77
+
78
+ For example, if the request was ` { name, age } ` , a GraphQL service responding in
79
+ JSON should respond with ` { "name": "Mark", "age": 30 } ` and should not respond
80
+ with ` { "age": 30, "name": "Mark" } ` .
65
81
66
82
While JSON Objects are specified as an
67
83
[ unordered collection of key-value pairs] ( https://tools.ietf.org/html/rfc7159#section-4 )
68
84
the pairs are represented in an ordered manner. In other words, while the JSON
69
85
strings ` { "name": "Mark", "age": 30 } ` and ` { "age": 30, "name": "Mark" } `
70
86
encode the same value, they also have observably different property orderings.
71
87
72
- Since the result of evaluating a selection set is ordered, the JSON object
73
- serialized should preserve this order by writing the object properties in the
74
- same order as those fields were requested as defined by query execution.
75
-
76
- For example, if the query was ` { name, age } ` , a GraphQL server responding in
77
- JSON should respond with ` { "name": "Mark", "age": 30 } ` and should not respond
78
- with ` { "age": 30, "name": "Mark" } ` .
79
-
80
- NOTE: This does not violate the JSON spec, as clients may still interpret
88
+ Note: This does not violate the JSON spec, as clients may still interpret
81
89
objects in the response as unordered Maps and arrive at a valid value.
82
90
83
91
0 commit comments