Querying Content Links and Slices

Hi there -

I’d like to query a content relationship that’s part of a slice and cannot figure out how to do it. I have two objects in my repo - Publications and Subjects. I am trying to get all publications that have a particular Subject, but can’t. Here is the query:
query MyQuery {
allSubjects(uid: “education”) {
edges {
node {
_meta {
uid
}
}
}
}
allPublications{
edges {
node {
title
body {
…on PublicationBodyPublication_subjects {
fields {
subject {
…on Subject {
_meta {
uid
}
}
}
}
}
}
}
}
}
}

I want to be able to pass the UID of the Subject down to the allPublications query and filter where the uid of the Subject in the slice is equal to the uid of the subject I have chose (here, I’ve hard-coded ‘education’ as an example, but I would use a variable in the future).

I have tried restructuring the data model to have a group in which folks choose a content relationship, instead of a slice, and I can’t get that to work either.

Our users want/need a controlled vocabulary for the Subject object, so we cannot use tags here. They need to be able to add/edit the subjects, but also assign Subjects to a Publication.

I am using Gatsby with the gatsby-source-prismic-graphql plugin to generate pages. I can generate a page for each Publication and a page for each Subject just fine, but I want to list all Publications of a particular subject on the Subject page and cannot figure out a way to do this.

Any help is greatly appreciated.

Danielle

Hi Danielle, unfortunately it isn’t possible to query/filter by a Content Relationship field which is inside of a Group or a Slice. In order for this to work, the Content Relationship field needs to be at the top level of the Custom Type.

The other thing is that you can’t use the UID value for this. It only works with the document ID of the linked document (the linked subject in your case).

{
  prismic {
    allPublications(where: {subject: "XmZAAhMAACAARcwc"}) {
      edges {
        node {
          subject {
            _linkType
            ... on PRISMIC_Subject {
              _meta {
                id
              }
            }
          }
        }
      }
    }
  }
}

I hope this helps you get this sorted out. Let me know if you have any more questions about this.

Thanks Levi!

I have found a workaround. Is this something that is on the radar as a future feature? Or is it more of a limit to graphql?

@features-team Perhaps you can say more on this.

I just wanted to chime in and mention that I ran into this same issue/limitation and would love to be able to query relationships within groups/slices via graphQL

is this limitation just om the graphql API or any client? I’m also trying to query a blog post by some slice data.

If that’s not possible, how would model a blog post to have multiple categories and be able to query all posts by category id?

Hi @levijesica, I answered this question in the thread you opened here.