How to pass specific key from `$data` into the nested graphql query?

How to pass specific key from `$data` into the nested graphql query?


0

There are publications and tags with many-to-many relations. Here’s schema.gql:

input GetPublicationsDto {
  cursor: Int
  take: Int
  tagIds: [Int!]
  tagId: Int
  ...
}

input GetTagsForDto {
  tagIds: [Int!]
  tagId: Int
}

type Query {
  publications(data: GetPublicationsDto!): [Publication!]!
  tagsFor(data: GetTagsForDto!): [Tag!]!
}

We see that GetPublicationsDto extends GetTagsForDto.
Suppose we want to get publications by some tagId and this tag data in a single request. Here’s the query:

query PublicationsWithTags($data: GetPublicationsDto!) {
  publications(data: $data) {
    ...PublicationEntry
  }
  tagsFor(data: $data) {
    ...TagEntry
  }
}

This code fails to compile. The error is Variable "$data" of type "GetPublicationsDto!" used in position expecting type "GetTagsForDto!".

IMHO this code has to work, but the error is clear. It wants different data to be provided to tagsFor query. But how to extract specific key from $data?

I tried

query PublicationsListWithTags($data: GetPublicationsDto!) {
  publications(data: $data) {
    ...PublicationListEntry
  }
  tagsFor(data: { tagId: $data.tagId }) {
    id
    name
  }
}

It does not compile either, Cannot parse the unexpected character ".". What is the right way to extract key from $data and pass it into the tagsFor query?


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *