GraphQL query: how to map a list of ids to the corresponding entities?

154 views Asked by At

I am using Contentful's "GraphQL Playground".

I have a field that contains a list of id's:

GraphQL example 1

I'd like to retrieve the entities corresponding to those id's. I can do it with a second query:

GraphQL example 2

But I'd like to do it on the fly, in the first query.

The "GraphQL Playground" knows all the types of data that I could wish to query and I presume it builds the descriptions for me. The "Provision" is a content type with some editorial information inside (name, title, subtitle PLUS a "provisionId" field); the provisionCollection type is a list of provisions and the type is created/defined automatically by the system.

THE CONTEXT

Contentful allows to query the content of lists, provided they are specified with its own "sys.id" identifier.

Suppose I have a custom field "innerList" inside of a type of my liking, where provisions are specified with the "sys.id" value: in that case the GraphQL syntax will be:

myOwnContentType(id: "whatever") {
  innerListCollection {
    total
    items {
      name
      title
      subtitle
    }
  }
}

There is a lot of hidden reasoning here:

  • the system knows that innerList is a collection and provides me automatically an "innerListCollection" type to query
  • the system knows that innerList contains provisions, so that innerListCollection will feature the same
  • the "total" subquery calculates innerList's length
  • the "items" subquery (possibly tweaked with "limit" and "skip" params) returns all innerList elements and allows interrogation of their values on the fly.

THE PROBLEM

But in the case of this whole post, I am specifying a field "provisionList" by using the custom field "provisionId" that I created: you can see it on the right side of the first image. This is the source of all difficulties.

It would be necessary to explain the system that in this case the collection is determined by the provisionId's of the items, and not their "sys.id". It all comes down to the definition of a list by its pointers in GraphQL, which is a matter way too complex for my limited GraphQL skills.

0

There are 0 answers