Example of GraphQL querying a item with a relationship

Hi

I’ve looked everywhere but I can’t find an example on how to query a list of items, where each item has a field with a relation to another item using GraphQL. I am currently using the GraphiQL tool.

I want to see the relation item’s data nested. I see you’ve mentioned it can be done using GraphQL, but I can’t find an example.

Please help.

Thanks
Rudolph

Lets say you have two schemas:

  1. Category
    • name
    • posts (Reference to Posts)
  2. Posts
    • title

Then it would like this

{
   queryCategoryContents {
       id,
       data {
          name {
              iv
          },
          posts {
              id,
              data {
                title {
                  iv
               }
            }
        }
    }
}

Thanks, I’ve figured it out but for me it looked as follow:

{
   queryCategoryContents {
       id,
       data {
          name {
              iv
          },
          posts {
              iv {
                id,
                data {
                  title {
                      iv
                  }
                }
              }
        }
    }
}

yes, you are correct. Not easy to write these without intellisense.

Is this still working in 3.4.0? I cannot seem to find my relationship through the graphql api, the relationship property is simply not available in the graphql-api and the Dto’s, but it works through REST-api. Any ideas?

Sure, it has not been removed. Can you tell me more about your schema and the query you do?

Hi sorry for the late reply, here are the essential bits of my schema and the query:

Model:

{
    "fields": [
        {
            "name": "fullName",
            "properties": {
                "isListField": true,
                "isReferenceField": true,
                "isRequired": true,
                "fieldType": "String",
                "editor": "Input",
                "isUnique": true,
                "label": "Full name"
            },
            "partitioning": "invariant"
        },
        {
            "name": "name",
            "properties": {
                "fieldType": "String",
                "editor": "Input",
                "label": "Model name"
            },
            "partitioning": "invariant"
        }
        (...)
    ]
}

Make:

{
    "fields": [
        {
            "name": "name",
            "properties": {
                "fieldType": "String",
                "editor": "Input",
                "label": "Name"
            },
            "partitioning": "invariant"
        },
        {
            "name": "modelIds",
            "properties": {
                "fieldType": "References",
                "editor": "Dropdown",
                "schemaId": "7d1550af-3526-4c0f-a754-7ae61000380b", <-- Reference to "Model" schema
                "label": "Models"
            },
            "partitioning": "invariant"
        },
        (...)
    ]
}

GraphQL Query:

{
    queryMakeContents {
        data {
            name {
                iv
            },
            modelIds {  <-- This is marked with squiggles and cannot be found in intellisense
                data {
                    name {
                        iv
                    }
                }
            }
        }
    }
}

Error message:

{
    "errors": [
        {
            "message": "Cannot query field \"modelIds\" on type \"MakeDataDto\".",
            "locations": [
                {
                "line": 38,
                "column": 7
                }
            ]
        }
    ]
}

MakeDataDto in explorer, can’t se reference to Model here either:

This looks almost correct.

It should be

{
    queryMakeContents {
        data {
            name {
                iv
            },
            modelIds {  <-- This is marked with squiggles and cannot be found in intellisense
                iv { <-- DIFFERENCE
                  data {
                     name {
                        iv
                    }
                } 
              }
            }
        }
    }
}

But this does not solve your problem. I don’t know why it does not show up. Usually the model is cached for a few minutes, just fyi.

Can you either invite me to your app in Squidex Cloud or invite me (sebastian@squidex.io) to your app in your hosted instance and then send me a backup of your database?

I’m currently only running it on my local machine, but i can try to recreate it on Squidex Cloud to see if it occurs there as well!

Thanks for correcting my query as well :slight_smile:

Edit:

Ok, so it did work in the cloud. That made me check my schema validation for the “make” schema. It turns out the reference types schema field was empty, maybe it got lost when I edited the referenced schema or something. :thinking:

After setting the validation field, the relations between the makes and the models was lost, but the graphql-api started working. I just made the connections between the makes and models once more and things are flying again!

Thanks for helping out, amazing support Sebastian! :heart:

1 Like