hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

Can you please add an example for querying fields that are arrays #150

Closed piccione99 closed 1 month ago

piccione99 commented 1 month ago

I am missing a piece of information.

I'm trying a query like below, but it fails graphql validation. Normally, in the playground, you wouldn't specify anything about an array in the query, just the fields. But it seems like the client puts the response data into the same query structure, so an array is needed for the response.

type DeviceEventsQuery struct {
    CpeInsight struct {
        DeviceID string  `graphql:"deviceId"`
        Events   []struct {
            EventTimestamp time.Time `graphql:"eventTimestamp"`
            Dest           *string   `graphql:"dest,omitempty"`
            Source         *string   `graphql:"source,omitempty"`
            Metadata       []struct {
                Key   string `graphql:"key"`
                Value string `graphql:"value"`
            }    `graphql:"metadata,omitempty"`
            SessionId      string    `graphql:"sessionID"`
        } `graphql:"events"`
    } `graphql:"cpeInsight(deviceId: $deviceId)"`
}
2024-08-09T20:27:14-04:00   ERROR   codex-adapter/handler.go:114    error getting device events {"deviceId": "cf:f3:2c:fc:0d:fb", "error": "Message: 422 Unprocessable Entity; body: \"{\\\"errors\\\":[{\\\"message\\\":\\\"Cannot query field \\\\\\\"omitempty\\\\\\\" on type \\\\\\\"Event\\\\\\\".\\\",\\\"locations\\\":[{\\\"line\\\":1,\\\"column\\\":95}],\\\"extensions\\\":{\\\"code\\\":\\\"GRAPHQL_VALIDATION_FAILED\\\"}},{\\\"message\\\":\\\"Cannot query field \\\\\\\"omitempty\\\\\\\" on type \\\\\\\"Event\\\\\\\".\\\",\\\"locations\\\":[{\\\"line\\\":1,\\\"column\\\":112}],\\\"extensions\\\":{\\\"code\\\":\\\"GRAPHQL_VALIDATION_FAILED\\\"}},{\\\"message\\\":\\\"Field \\\\\\\"metadata\\\\\\\" of type \\\\\\\"[Pair]\\\\\\\" must have a selection of subfields. Did you mean \\\\\\\"metadata { ... }\\\\\\\"?\\\",\\\"locations\\\":[{\\\"line\\\":1,\\\"column\\\":122}],\\\"extensions\\\":{\\\"code\\\":\\\"GRAPHQL_VALIDATION_FAILED\\\"}},{\\\"message\\\":\\\"Cannot query field \\\\\\\"omitempty\\\\\\\" on type \\\\\\\"Event\\\\\\\".\\\",\\\"locations\\\":[{\\\"line\\\":1,\\\"column\\\":131}],\\\"extensions\\\":{\\\"code\\\":\\\"GRAPHQL_VALIDATION_FAILED\\\"}}],\\\"data\\\":null,\\\"extensions\\\":{\\\"tracing\\\":{\\\"version\\\":1,\\\"startTime\\\":\\\"2024-08-10T00:27:14.447130557Z\\\",\\\"endTime\\\":\\\"2024-08-10T00:27:14.44726538Z\\\",\\\"duration\\\":134833,\\\"parsing\\\":{\\\"startOffset\\\":58384,\\\"duration\\\":21013},\\\"validation\\\":{\\\"startOffset\\\":79471,\\\"duration\\\":-9223372036854775808},\\\"execution\\\":{\\\"resolvers\\\":null}}}}\", Locations: [], Extensions: map[code:request_error], Path: []"}
hgiasac commented 1 month ago

As you can see in the error detail:

{
  "errors": [
    {
      "message": "Cannot query field \"omitempty\" on type \"Event\".",
      "locations": [{ "line": 1, "column": 95 }],
      "extensions": { "code": "GRAPHQL_VALIDATION_FAILED" }
    },
    {
      "message": "Cannot query field \"omitempty\" on type \"Event\".",
      "locations": [{ "line": 1, "column": 112 }],
      "extensions": { "code": "GRAPHQL_VALIDATION_FAILED" }
    },
    {
      "message": "Field \"metadata\" of type \"[Pair]\" must have a selection of subfields. Did you mean \"metadata { ... }\"?",
      "locations": [{ "line": 1, "column": 122 }],
      "extensions": { "code": "GRAPHQL_VALIDATION_FAILED" }
    },
    {
      "message": "Cannot query field \"omitempty\" on type \"Event\".",
      "locations": [{ "line": 1, "column": 131 }],
      "extensions": { "code": "GRAPHQL_VALIDATION_FAILED" }
    }
  ],
  "data": null,
  "extensions": {
    "tracing": {
      "version": 1,
      "startTime": "2024-08-10T00:27:14.447130557Z",
      "endTime": "2024-08-10T00:27:14.44726538Z",
      "duration": 134833,
      "parsing": { "startOffset": 58384, "duration": 21013 },
      "validation": { "startOffset": 79471, "duration": -9223372036854775808 },
      "execution": { "resolvers": null }
    }
  }
}

The library doesn't support omitempty tag. You should remove them.

piccione99 commented 1 month ago

Thanks for your response. That's embarrassing. I was only paying attention to the error on the metadata field. It looks like including the array structure in the query is indeed the right thing to do and it works fine after removing the omit tags.