How do you query by a content relationship in a group, but in graphql?

In the REST API, you can filter data by a value in a content relationship in a group, but I can't seem to find how to do so in graphql.

For example, the documentation in REST:

But in graphql, the documentation doesn't mention groups:

I need to be able to filter by service, and I'm not seeing it as an option here:

Please help!

1 Like

Hey again!!

Queries by Content Relationship / Link fields must be top-level fields

When using the Content Relationship or Link field as arguments in GraphQL, they have to be at the top level of a Custom Type. It's not possible to use them as arguments if the field is inside a Group field or a Slice.

Similarly to the REST queries, you need to pass the ID of the doc you're looking for using the where argument along with the API ID of the Content Rel. field. So if that field is called "connection", a query would look something like this:

query {
  allPosts(where : {connection: "XECf-BAAACEACaD9"}) {
    edges {
      node {
        connection {
          ... on Author {
            _meta {
              id
            }
          }
        }
      }
    }
  }
}

We updated the GraphQL docs to make this clearer, thank you very much.

If you need anything else let us know!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Does this make this document invalid then?

As this document states clearly that you can "5. Query all the documents linked to any given tag", but in graphql you are saying that is impossible if you use a group field, which earlier in this document you recommend as best practice. Confused!

Is the only way to search articles based on multiple tags to use the inbuilt tag feature?

1 Like

I came here with the same question.

I too implemented a “Custom Tagging System” according to the documentation, but if I can’t query data by a tag in a group, it is worthless to me. The documentation does say, “If you put this [Content Relationship] field in a Group field, they [content authors] will be able to select as many as they need. […] You will also be able to query any documents linked to a certain tag in your system.”

The built in tagging system is very flawed since there is no way to restrict the tags to a particular domain. My current workaround is to make several successive GraphQL requests to load all of the data in, then filter it client side. I was hoping to find a workaround or, perhaps, hoping Prismic had addressed this flaw.

1 Like

Yes exactly! This is an extremely common use case.

I wonder how many other people have gone down the 'custom tagging system' garden path only to realise half a day later that it's not possible with grouped fields. Then spent another half a day trawling the forums in vain only to realise there isn't a solution.

Basically it boils down to the fact that the inbuilt tags are not enough. Not flexible enough (can't edit, delete, bulk edit, bulk delete, bulk anything, view, manage etc), and just not enough of them.

Most modern websites will require more than 1 custom taxonomy, e.g. tags or categories.
I'd suggest that most sites would need 1 or more taxonomies for each post type. This makes loading, filtering, sorting more efficient, the queries more optimised, and the CMS cleaner to use.

Without the use of a grouped field in the 'best practice approach', its clearly not a substitute for a taxonomy / tagging system, its just a single post reference : (

Surely by now there must be a more robust solution in the pipeline for what is arguably a very common feature in a CMS?

1 Like

Hello @tim and @alex.martens

This is indeed a limitation in the Prismic GraphQL schema. It is only possible when using REST or GraphQL in Gatsby. I have updated the article you found in the user guides you make this clear.

For the moment, we rely on Gatsby as our main GraphQL based framework; it allows you to have more flexibility when using the arguments for ordering or to filter out the response.

Something that we have been actively working on is Bulk Actions for the writing room. This will give you more control to bulk delete, program releases, publish unpublish, etc. You can follow the status of this feature on our progress page.

Hey @Pau I'm curious to know if you plan to extend the GraphQL schema to allow filtering based on values in groups.

I'm unable to query a document based on a value in a group, not just content relationship ones.

Hello everyone,

@jason, @tim, @alex.martens and @prismic5

I'm happy to announce that we're now working on releasing a canary version of this change in the GraphQL schema: being able to use Group fields as filtering options.

We'll be doing a progressive release of this feature for every single repository. It's not possible to have this activated on your repository by request right now, we'll update you here once this changes.

Thanks.

4 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

This feature should now be available almost everywhere, we have updated our documentation to show you how to use this new syntax.

If you can not find your group field when trying to make your query, you might be one of the few repositories without this activated. Contact the Activations Team as described here and we can activate this for you. Thanks.

Hi everyone,

I spent almost a day trying to filter a GraphQL query by multiple group fields ids, in vain.

Submitted a ticket and support team told me it does't work and they are working on this.
In the meantime, did anyone faced that need and finally find a workaround?

This request returns an error...

{
  allContents(lang: "en-us", where: {countries: {country: {id_in: ["ZEFqZBEAACQA1E8i", "WCFiAEABBCQA1E9q"]}}}) {
    edges {
      node {
        countries {
          country {
              _meta {
                id
              }
            }
          }
        }
      }
    }
  }
}

...while filtering a GraphQL query by *a single group fields id works.

{
  allContents(lang: "en-us", where: {countries: {country: "ZEFqZBEAACQA1E8i"}}) {
    edges {
      node {
        countries {
          country {
            ... on Country {
              _meta {
                id
              }
            }
          }
        }
      }
    }
  }
}

Thanks for you help!