GraphQL icon indicating copy to clipboard operation
GraphQL copied to clipboard

Comparing two GraphQLResult

Open zamderax opened this issue 10 months ago • 3 comments

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()

zamderax avatar Dec 13 '24 20:12 zamderax