Query linked document data inside a group field with GraphQuery

Hello,
I'm trying to run this query:

const page = await $prismic.api.query([
  $prismic.predicates.at('document.type', 'homepage'),
  $prismic.predicates.at('my.homepage.main_projects.project', homeId),
])

In the homepage, I have a group field (main_projects)
Within the group field, I have a relationship field (API ID: main_project) linking to a custom type (API Id: project)

I'm trying to get data of the custom type document that is added with the relationship field in the group field.

However, the query does not return any results.

If I run this:

const page = await $prismic.api.query([
  $prismic.predicates.at('document.type', 'homepage'),
])

It returns the homepage data but I don't seem to get the data of the custom type added in the relationship in the group

Any ideas?

Thank you!

Hello @fransy, your first query seems correct, but you also have to use FetchLinks to retrieve Document Fields from those linked documents.

Hello Paulina,
Yes, FetchLinks worked.
Thank you!

WOAH! You can get linked items in a group?

Are the docs wrong? I had thought it was “not possible to retrieve…Any field in a Group or Slice


Hi @rjessome,

I think maybe the docs are a bit confusing in that regard.

not possible to retrieve…Any field in a Group or Slice

This means that with FetchLinks you cannot get the content of a group field inside the linked document, for that you would need to use GraphQuery.

What Paulina and Fransy are talking about is querying data from multiple linked documents where the links live in a group field in the main document.

OH! I see.

Yeah - I totally read it differently! Thanks for clarifying, though!

1 Like

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

Just chiming in since I found this page.

My data looks like this:

  • blog-article (custom type)
    • categories (group)
      • category (link to blog-category custom type)

Where blog-category has a name field.

I was able to grab the data with GraphQuery with the following query:

const deepFetchCategoriesQuery = `{
  blog-article {
    ...blog-articleFields
    categories {
      category {
        name
      }
    }
  }
}`;

I tried FetchLinks with values like "categories.category", "categories.name", "categories.category.name", and "category.name" but none of those worked.

I hope this helps someone! This took me a while and quite some digging to get something working.

2 Likes

Hi @eddyvinck95 might this also be my struggle? I have a page custom type for rendering pages. Then I have a slice called featured_pages, which has a "repeatable zone" of actual pages (using said page custom type. When I query any page, I want the data for any/each page added to featured_pages to resolve so I could use them, like image, title, description. How might you write such a query following how you got yours to work using graphQuery? Thanks.

Hi @eddyvinck95 , @kb1
I'm currently trying to troubleshoot this.
Can you please provide your repository name (in a private message if possible) and link to your query in the API browser?

Note that you can access your API browser such as
https://your-repository-name.prismic.io/api/v2

Looking forward to your reply,
Fares

Hi @eddyvinck95

I have replicated the cutom type structure you have and here is a link to a working GraphQuery.

The response looks like:

And here is how the GraphQuery looks like decoded:

graphQuery={
 blog-article{
  categories{
   blog_cat_link{
    ...onblog_category{
      title
    }
   }
  }
 }
}
#format=json

Here are the custom types:

blog-article.json

{
  "Main" : {
    "categories" : {
      "type" : "Group",
      "config" : {
        "fields" : {
          "blog_cat_link" : {
            "type" : "Link",
            "config" : {
              "select" : "document",
              "customtypes" : [ "blog_category" ],
              "label" : "blog cat link"
            }
          }
        },
        "label" : "categories"
      }
    }
  }
}

blog_category.json

{
  "Main" : {
    "title" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "heading1,heading2,heading3,heading4,heading5,heading6",
        "label" : "Title"
      }
    },
    "blog_cat" : {
      "type" : "StructuredText",
      "config" : {
        "single" : "heading1,heading2,heading3,heading4,heading5,heading6",
        "label" : "blog cat"
      }
    }
  }
}

Please let me know if that answers your query.