Repository navigation
Open
Description
Problem
Currently can't compare two GraphQLResult even though the data is the same.
I'm currently using swift-testing with Swift 6.0.2
let queryOrganizationBySlug: String = """
query FindSingleOrganization($slug: String!) {
findSingleOrganization(slug: $slug) {
slug
displayName
details
domain
}
}
"""
let variables: [String: Map] = ["slug": "acme-corp"]
let graphQLRequest = GraphQLRequest(query: query, variables: variables)
let encoder = GraphQLJSONEncoder()
let buffer = ByteBuffer(data: try encoder.encode(graphQLRequest))
let response: GraphQLResult = try await client.runGraphQLQuery(
url: "/graphql",
buffer: buffer
)
let expected: GraphQLResult = GraphQLResult(
data: [
"findSingleOrganization": [
"slug": "acme-corp",
"displayName": "Acme Corporation",
"details": "A leading technology company",
"domain": "acme.com"
]
]
)
#expect(response == expected)However I'm getting an issue on comparison even though GraphQLResult is Equatable. The error is as follows:
✘ Expectation failed: (response → {"data":{"findSingleOrganization":{"displayName":"Acme Corporation","details":"A leading technology company","domain":"acme.com","slug":"acme-corp"}}}) == (expected → {"data":{"findSingleOrganization":{"slug":"acme-corp","displayName":"Acme Corporation","details":"A leading technology company","domain":"acme.com"}}})Which we can see that the data is the same, the only thing is that the data is in differing orders:
[
{
"data": {
"findSingleOrganization": {
"details": "A leading technology company",
"domain": "acme.com",
"slug": "acme-corp",
"displayName": "Acme Corporation"
}
}
},
{
"data": {
"findSingleOrganization": {
"slug": "acme-corp",
"displayName": "Acme Corporation",
"details": "A leading technology company",
"domain": "acme.com"
}
}
}
]What's the proper way to compare the results?
In addition, I used
let graphQLRequest = GraphQLRequest(query: query, variables: variables)
let encoder = GraphQLJSONEncoder()Metadata
Metadata
Assignees
Labels
No labels
Activity
NeedleInAJayStack commentedon Dec 20, 2024
It looks like you were doing the right thing using
GraphQLJSONEncoder. Do you happen to know what Decoderclient.runGraphQLQueryis using under the hood to convert to aGraphQLResult?For background, JSON doesn't care about ordering, but GraphQL does, so we've had to do quite a bit of work to get GraphQL to play nice, and may have missed a spot on decoding.
As an alternative, you could check each field (this is psudocode - you might have to fix it up):
It's not pretty, but it should do the trick.
zamderax commentedon Dec 25, 2024
Apologies for the late reply, was traveling for the holidays.
The following is the swift extension that I wrote for my Integration testing framework.
NeedleInAJayStack commentedon Dec 28, 2024
Oh okay, that makes sense.
For background,
GraphQLResult.datauses theMapstructure, which itself uses anOrderedDictionaryof fields. This is important in order to deliver the correct ordering of results in the JSON response, which is required by the graphQL spec. However, it means thatMapequality requires consistent ordering of the fields.The issue here is that
JSONDecoderinternally uses an ordinaryDictionary, which does not preserve ordering, so your decodedGraphQLResulthas non-deterministic ordering of its fields, which may not match your declared expectedGraphQLResultI'd suggest that you instead model the expected response as a standard decodable type and don't use
GraphQLResultwhich will avoid any ordering considerations. For example:Just be aware: I didn't test the code above, so you might have to fiddle with it a bit.