Skip to content

Commit 151c2f1

Browse files
Merge pull request #94 from NeedleInAJayStack/fix/JSONResultOrdering
Fixes Encoded JSON Result Ordering
2 parents 588b7b4 + d71909f commit 151c2f1

File tree

6 files changed

+1353
-7
lines changed

6 files changed

+1353
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Looking for help? Find resources [from the community](http://graphql.org/communi
1414

1515
This repo only contains the core GraphQL implementation. For a better experience when creating your GraphQL schema use [Graphiti](https://github.com/GraphQLSwift/Graphiti).
1616

17+
## Encoding Results
18+
19+
If you encode a `GraphQLResult` with an ordinary `JSONEncoder`, there are no guarantees that the field order will match the query,
20+
violating the [GraphQL spec](https://spec.graphql.org/June2018/#sec-Serialized-Map-Ordering). To preserve this order, `GraphQLResult`
21+
should be encoded using the `GraphQLJSONEncoder` provided by this package.
22+
1723
## Contributing
1824

1925
Most of this repo mirrors the structure of the canonical GraphQL implementation written in Javascript/Typescript. If there is any feature missing, looking at the original code and "translating" it to Swift works, most of the time. For example:

Sources/GraphQL/GraphQL.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Foundation
21
import NIO
32

43
public struct GraphQLResult : Equatable, Codable, CustomStringConvertible {
@@ -34,7 +33,7 @@ public struct GraphQLResult : Equatable, Codable, CustomStringConvertible {
3433
}
3534

3635
public var description: String {
37-
let data = try! JSONEncoder().encode(self)
36+
let data = try! GraphQLJSONEncoder().encode(self)
3837
return String(data: data, encoding: .utf8)!
3938
}
4039
}

0 commit comments

Comments
 (0)