Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Graphql error when trying to use nested fields in the `where` clausule.

default discord avatar
Deleted User2 years ago
1

Hello!



I am currently building a website for a museum, and for their events page, I would like to implement a filtering system, based on the starting date, the type of event, etc...



I have added all that data in the config and I am now in the process of writing the Graphql queries with the filtering options.


To create a nicer user interface for the editors, I have added some

Tabs

fields. One of those tabs is called

information

and whenever I try to use a value in the

where

clausule, that is inside the tab, it gives me the following error:


The following path cannot be queried: event_type

. I have moved it out of the tabs and then it works, but this makes the UI for the editor very cluttered, as there is a lot of data to input that I'd like to keep organized.



Is there a way to use nested fields in the

where

clausule?



The field structure looks as follows:



{
      type: 'tabs',
      label: false,
      tabs: [
         {
            name: 'information',
            label: 'Information',
            fields: [
              {
                type: 'row',
                fields: [
                  {
                    type: 'number',
                    name: 'start_age',
                    label: 'Youngest recommended age',
                  },
                  {
                    type: 'number',
                    name: 'end_age',
                    label: 'Oldest recommended age',
                  },
                ],
              },
              {
                  type: 'relationship',
                  name: 'event_type',
                  label: 'Event type',
                  relationTo: 'eventType',
                  hasMany: true,
                  required: true,
              }
            ]
         },
      ]
}


And the Graphql query looks as follows:


query Events($page: Int, $locale: LocaleInputType, $eventType: String, ) {
  Events(
    limit: 2
    page: $page
    locale: $locale
    where: { AND: [{ event_type: { equals: $eventType } }, { start_age: { less_than: 10 } }] }
  ) {
    docs {
      information {
        start_age
        end_age
      }
    }
  }
}
  • discord user avatar
    dribbens
    2 years ago

    You have two options:


    1) Remove the name on your tab

    name: 'information',

    this is grouping your data making your query incorrect.


    2) Update your

    where

    to target the nested

    event_type

    . I believe this should work:


    where: { AND: [{ information__event_type: { equals: $eventType } }, { start_age: { less_than: 10 } }] }


    If that isn't right, go into your graphql explorer and find the correct field name for the nested information__event_type.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.