Need help with graph query and returning fields

I have a document that looks like this:

{
        "id": "YVFtzxEAACMAUusa",
        "uid": "abcde",        
        "first_publication_date": "2021-09-27T07:08:03+0000",
        "last_publication_date": "2022-08-16T15:04:20+0000",
        "data": {            
            "specialisations": [
                {
                    "specialization": {
                        "id": "Ye2kTxMAACMAfe9X",
                        "type": "specialisation",
                        "tags": [],
                        "lang": "ro",
                        "slug": "-",
                        "first_publication_date": "2022-01-23T18:54:13+0000",
                        "last_publication_date": "2024-03-06T13:09:27+0000",
                        "uid": "specialization-one",
                        "link_type": "Document",
                        "isBroken": false
                    }
                }
            ]            
        }
    }    

As you can see, my document called "helper" contains a group field, called "specialisations", and in each group field I have a field called "specialization" (notice the "z") which is a link to another document of type "specialisation.

I am now using graphQuery in order to obtain the fields, and here is how I am using it:

const helpers = await client.getAllByType("helper", {
    graphQuery: `{
      helpers {
        specialisations {
          specialization {
            ...on specialisation {
              ...specialisationFields
            }
          }
        }
      }
    }`,
  });

No matter how I try I don't get the related document fields. This is how the console log looks like:

"specialisations": [
                {
                    "specialization": {
                        "id": "Ye2kTxMAACMAfe9X",
                        "type": "specialisation",
                        "tags": [],
                        "lang": "ro",
                        "slug": "-",
                        "first_publication_date": "2022-01-23T18:54:13+0000",
                        "last_publication_date": "2024-03-06T13:09:27+0000",
                        "uid": "cardiologie-interventionala",
                        "link_type": "Document",
                        "isBroken": false
                    }
                }
            ]

As you can see the "data" property is missing....

Hi there @tudor2004

I believe your close. Here's an example where I grab a content relationship but all I want is the title of the related item. I believe you'll need to replace ...specializationFields with the fields you actually want.

graphQuery: `
    {
      gallery_item {
        related_content {
          ...on portfolio {
            title
          }
        }
        gallery_image
      }
    }
    `
const helpers = await client.getAllByType("helper", {
    graphQuery: `{
      helpers {
        specialisations {
          specialization {
            ...on specialization {
              title
              richtext // or whatever your fields are
            }
          }
        }
      }
    }`,
  });

Give that a shot and let me (us) know how it goes.

Unfortunately it does not work. This is how it looks like right now:

const helpers = await client.getAllByType("helper", {
    graphQuery: `{
      helpers {
        specialisations {
          specialization {
            ...on specialization {
              title
              position
            }
          }
        }
      }
    }`,
  });

And the response looks like this:

[
    {        
        ...
        "data": {
            ...
            "specialisations": [
                {
                    "specialization": {
                        "id": "Ye2kTxMAACMAfe9X",
                        "type": "specialisation",
                        "tags": [],
                        "lang": "ro",
                        "slug": "-",
                        "first_publication_date": "2022-01-23T18:54:13+0000",
                        "last_publication_date": "2024-03-06T13:09:27+0000",
                        "uid": "my-uid,
                        "link_type": "Document",
                        "isBroken": false
                    }
                }
            ]
            ....
        }
     }
]

This is how it looks like when I am using `fetchLinks instead of graphQuery:

"specialisations": [
                {
                    "specialization": {
                        "id": "Ye2kTxMAACMAfe9X",
                        "type": "specialisation",
                        "tags": [],
                        "lang": "ro",
                        "slug": "-",
                        "first_publication_date": "2022-01-23T18:54:13+0000",
                        "last_publication_date": "2024-03-06T13:09:27+0000",
                        "uid": "my-specialisation",
                        "data": {
                            "title": [
                                {
                                    "type": "heading1",
                                    "text": "My Specialisation",
                                    "spans": []
                                }
                            ],
                            "position": 2
                        },
                        "link_type": "Document",
                        "isBroken": false
                    }
                }
            ],

Hi there @tudor2004

I made a spelling change that may have impacted your results. I believe we differ in our UK/US English spelling of specialization. Please try this:

const helpers = await client.getAllByType("helper", {
    graphQuery: `{
      helpers {
        specialisations {
          specialization {
            ...on specialisation {
              title
              position
            }
          }
        }
      }
    }`,
  });

Often, when I make an error in my graphQuery, there's an error output in my development console. Are you seeing anything in there if this doesn't work?

Hi and sorry for my late reply.

I have another example, much more simpler, where the graph query still doesn't work:

const newsItem = await client
    .getByUID("news", uid, {
      graphQuery: `{
        news_item {
          author {
            ...on team_member {
              name
            }
          }
        }
      }`,
    })
    .catch(() => notFound());

console.log("newsItem", newsItem.data.author);

Running the script above is returning me something like this:

newsItem {
  id: 'Zn1LcREAACwAc9ia',
  type: 'team_member',
  tags: [],
  lang: 'ro',
  slug: 'john-doe',
  first_publication_date: '2024-06-27T11:22:28+0000',
  last_publication_date: '2024-07-02T07:33:06+0000',
  uid: 'john-doe',
  link_type: 'Document',
  isBroken: false
}

If I am trying the same using `fetchLinks:

const newsItem = await client
    .getByUID("news", uid, {
      fetchLinks: ["team_member.name"],
    })
    .catch(() => notFound());

...I get the following output:

newsItem {
  id: 'Zn1LcREAACwAc9ia',
  type: 'team_member',
  tags: [],
  lang: 'ro',
  slug: 'john-doe',
  first_publication_date: '2024-06-27T11:22:28+0000',
  last_publication_date: '2024-07-02T07:33:06+0000',
  uid: 'john-doe,
  data: { name: [ [Object] ] },
  link_type: 'Document',
  isBroken: false
}

I really don't understand what I'm doing wrong with such a basic query.

Ah... my custom type is called news not news_item :man_facepalming:

@nf_mastroianni I've read carefully also your last reply and it seems to work now. Thanks very much for your help and patience!

1 Like