Search code examples
amazon-web-servicesaws-lambdaaws-appsync

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?


Solution

  • 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' }