Contentful logo

Contentful Community

Query on parent reference

Hello :slight_smile:

is it possible to query on refenrences with where?

eg. find collection β†’ where property (parentLink) id-> is equal to (id)

can this be done in graphql?

right now i can ONLY see the parentLink_exists eg: `

contentPageCollection(where: {parentLink_exists: true})
    items {
      sys {
        id
      }
    }
    
  }

Hi,

I don’t think so, what I have had to do is build one query to get a collections of pages and then one to get the actual page.
When getting the page, I use fragments to get the content items. For example, here is my listPages:

query pageCollection {
  pageCollection {
    items {
      sys {
        id
      }
      title
      image
      description
      path
      contentCollection {
        items {
          __typename
        }
      }
    }
  }
}

Once I have that, I then check the route and try to find the page id by the path. If found I then do a query like this:

query getPage($id: String!) {
  page(id: $id) {
    title
    breadcrumbEnabled
    header {
      __typename
      ...SectionAbout
      ...BrandSection
      ...SectionCallToAction
      ...SectionFindUs
      ...SectionGallery
      ...SectionImageColumn
      ...SectionLive
      ...SectionMultipleRows
      ...SectionPartners
      ...SectionProducts
      ...SectionTheatres
    }
    contentCollection(limit: 8) {
      items {
        __typename
        ...SectionAbout
        ...BrandSection
        ...SectionCallToAction
        ...SectionFindUs
        ...SectionGallery
        ...SectionImageColumn
        ...SectionLive
        ...SectionMultipleRows
        ...SectionPartners
        ...SectionProducts
        ...SectionTheatres
      }
    }
  }
}

A fragment might look like this:

fragment SectionProducts on SectionProducts {
  __typename
  title
  description
  contentfulMetadata {
    tags {
      id
    }
  }
}

HTH

It is not possible to search by a value in a multi-reference field. However, you could search on the childCollection, and then find the parentCollection with linkedFrom

query{
  childCollection(where:{xxx_contains:"xxx"}){
    items{
      linkedFrom{
        parentCollection{
          items{
            xxx
          }
        }
      }
    }
  }
}

Full doc can be found here.