Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appsync query union return type throws 400

I have two types Stream and Video and a listResources query that returns a mixed list of streams and videos:

type Stream {
    pk: String!
    starts_at: String!
}

type Video {
    pk: String!
    created_at: String!
}

union SearchResult = Stream | Video

type Query {
    listResources(code: String!): [SearchResult]
}

and below an example of calling the query:

query {
  listResources(code: "234") {
    ... on Stream {
      pk
      starts_at
    }
    ... on Video {
      pk
      created_at
    }
  }
}

For some reason, even though the query should be formed correctly according to the appsync and graphql docs (https://graphql.org/learn/schema/#union-types, https://docs.aws.amazon.com/appsync/latest/devguide/interfaces-and-unions.html), the query throws a 400 error.

  • Already checked the lambda locally and in cloudwatch, the lambda returns data correctly.
  • If I change the return type of listResources query to AWSJSON the data gets returned properly, which confirms proper funcitonality of the lambda. The error must either be the query return type or the schema definition.

Where might the culprit be?

like image 887
furyozo Avatar asked Jan 31 '26 19:01

furyozo


1 Answers

you might have this solved by now but I would guess the problem could be you're not returning the __typename in your code, so AppSync does not know what type within the union it's being returned. It should be returning, for instance: { __typename: 'Stream', pk: 1, starts_at: '2022-01-07' }

like image 139
Jologe Avatar answered Feb 03 '26 08:02

Jologe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!