Skip to content

Commit a9a6dab

Browse files
authored
Clarify: serialization formats (#437)
Factors out a section on Map ordering which was duplicated between the previous two subsections. Also clarifies that a different serialization format may use whichever natively supported primitives it likes. Closes #337
1 parent 007a93e commit a9a6dab

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

spec/Section 7 -- Response.md

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,36 @@ the case that a field error occurred on a field which was replaced with null.
1313

1414
GraphQL does not require a specific serialization format. However, clients
1515
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
1717
representations of the following four primitives:
1818

1919
* Map
2020
* List
2121
* String
2222
* Null
2323

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:
3827

3928
* Boolean
4029
* Int
4130
* Float
4231
* Enum Value
4332

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+
4438

4539
### JSON Serialization
4640

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:
5246

5347
| GraphQL Value | JSON Value |
5448
| ------------- | ----------------- |
@@ -61,23 +55,37 @@ the following JSON concepts:
6155
| Float | Number |
6256
| Enum Value | String |
6357

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" }`.
6581

6682
While JSON Objects are specified as an
6783
[unordered collection of key-value pairs](https://tools.ietf.org/html/rfc7159#section-4)
6884
the pairs are represented in an ordered manner. In other words, while the JSON
6985
strings `{ "name": "Mark", "age": 30 }` and `{ "age": 30, "name": "Mark" }`
7086
encode the same value, they also have observably different property orderings.
7187

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
8189
objects in the response as unordered Maps and arrive at a valid value.
8290

8391

0 commit comments

Comments
 (0)