1

I am fairly new to Craft; I have an entry type with a custom field that is a Categories field. I am trying to use GraphQL to return the fields on my entry, including the category field value. Ex. my Blog Post entries have a custom category field of blogType. Ideally, I want my data to be like this

{
"data": {
    "entries": [
        {
            "title": "Blog 1",
            "body": "<STRING>"
            "blogType": "Announcement"
        },
        {
            "title": "Blog 2",
            "body": "<STRING>"
            "blogType": "Marketing"
        },
        {
            "title": "Blog 3",
            "body": "<STRING>"
            "blogType": "News"
        }

I have tried a lot of variations of my query but cannot seem to return the field value for each entry. Here are some of the queries I have tried

{
entries (section: "blogPosts"){ 
title 
body
blogType
} 

and the below query trying to use label, value, title, group and probably a lot more.

{
entries (section: "blogPosts"){ 
title 
body
blogType { label }
}

1 Answer 1

1

To get the field value from you entries you need do some inline fragment on the entries type interface you can refer to https://craftcms.com/docs/4.x/graphql.html#interface-implementation

So, it should look like this

{
  entries (section: "blogPosts"){ 
  title 
  ... on blog_posts_Entry {
    body
    fieldA
    fieldB 
 }
} 

the blog_posts_Entry is called interface, you can found the correct value in your schema.

3
  • Thanks, @aodihis. I've tried this but my call still isnt returning anything and doesnt even know the fieldA or fieldB as available in the schema that was fetched. Is there something specific that needs to be checked off in the schema in addition to the entries section? e.g. the "View Category Group" in the schema definition?
    – Adrienne W
    Commented Dec 8, 2022 at 16:42
  • @AdrienneW for schema, do you use full schema or you create new one? if you created new one, do you have set your entry type to given schema? if it is possible can you give us screenshot about your field and schema.
    – aodihis
    Commented Dec 9, 2022 at 7:27
  • I created a new schema. I have included all of the Entries sections that I am looking to query (including everything for my Blog entry type). But I haven't included any of the "View Category group" under the Categories element.
    – Adrienne W
    Commented Dec 14, 2022 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.