Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Graphql query error - There is a table that has some rows, within each row, there are a number of cells, and within each cell, there are some values.

7 replies
Last updated: Aug 4, 2021

Hi everyone, I am trying a relatively easy query but can't make it work. There is a table that has some rows, within each row there are a number of cells, and within each cell there are some values. The schema is something like this:

{ 
   name: 'table',
   fields: [
      name: 'tableTemplate',
      type: 'object',
      fields: [
         name: 'rows',
         type: 'array',
     ....
    ]
]
The GraphQL query (that works), is this:

query MyQuery {
  lessons: allLesson {
      tables {
        tableTemplate {
          rows {
            cells {
              values
            }
          }
      }
    }
  }
}
The GROQ query I am trying and fails is this:

*[_type == "lesson"]{
    "data": tables[].tableTemplate.rows[].cells[].values
  }
This works when I stop at the
rows[]
, like so:
*[_type == "lesson"]{
    "data": tables[].tableTemplate.rows[]
  }
But when I add
.cells[]
they have none of it.
Anyone any suggestions on what I am doing wrong?
Thanks

Aug 3, 2021, 5:08 PM

I am using

apiVersion: "v2021-03-25"
Sample outcome:

[
    {
      _key: '96ed4941-62fe-4035-9db8-722973d34a10',
      _type: 'row',
      cells: [Array]
    },
    {
      _key: '50239c84-6909-44cb-9adf-a9fe430be551',
      _type: 'row',
      cells: [Array]
    },
    {
      _key: 'fc4f4f04-36e5-4843-a595-6de325d43ada',
      _type: 'row',
      cells: [Array]
    },
    {
      _key: '35851491-ef9c-4243-af33-0d5f3e39c588',
      _type: 'row',
      cells: [Array]
    },
.
.
.
]

Aug 3, 2021, 5:29 PM

Would you be willing to post the schema you’re using, Nikos? I want to make sure I understand you correctly. For example, in your sample schema at the top of your post it looks like table is an object (it has a

fields
property) but in your query it’s an array. Thanks!

Aug 4, 2021, 4:22 AM

oh yea sure, I am sorry --the one I shared is a bit rushed.

// lesson document
{
  name: 'lesson',
  type: 'document',
  title: 'Lesson',
  fields: [
   {
      title: 'Curriculum Benchmark Tables',
      name: 'tables',
      type: 'array',
      of: [
        {
          type: 'curriculumBenchmark',
        },
      ],
      inputComponent: TableLesson,
    },
  ]
}

// curriculumBenchmark document
{
  name: 'curriculumBenchmark',
  type: 'document',
  title: 'Curriculum Benchmarks',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'tableTemplate',
      title: ' ',
      type: 'tableTemplate',
    },
  ],
}

// tableTemplate object
{
  title: 'Table Template',
  name: 'tableTemplate',
  type: 'object',
  fields: [
    {
      title: 'Rows',
      name: 'rows',
      type: 'array',
      of: [
        {
          name: 'row',
          type: 'row',
        },
      ],
    },
  ],
}

// row object
{
  title: 'Row',
  name: 'row',
  type: 'object',
  fields: [
    {
      name: 'cells',
      type: 'array',
      of: [{ name: 'cell', type: 'cell' }],
    },
  ],
}

// cell object
{
  title: 'Cell',
  name: 'cell',
  type: 'object',
  fields: [
    {
      name: 'values',
      type: 'array',
      of: [
        {
          type: 'string',
        },
      ],
    },
  ],
}
I took out the unnecessary fields/validation rules and kept only the relevant info in.

Thank you!

Aug 4, 2021, 4:38 AM

I think you’ll want to skip the object (

tableTemplate
) in your GROQ query, so I would try either
'data': tables[].rows[].cells[]
or
'data': tables[].rows[].cells
(depending on whether you want a flattened array or an array of arrays, respectively).

Aug 4, 2021, 4:39 AM

Thanks for posting that. Oddly, your query from the first post works for me:

*[_type == 'lesson'] {
  'data': tables[].tableTemplate.rows[].cells[].values
}
With curriculumBenchmark being a document, I’m wondering if you are wanting to create a reference to it (in the
tables
array) rather than an array of that type. As it is, any documents you create of the type
curriculumBenchmark
won’t be included in the
lesson
document. (That said, it will still structure the schema used in
lesson
.)

Aug 4, 2021, 4:39 AM

yea, I was afraid this might happen. I used to use gen 1 version and then migrated to gen 2 using

apiVersion: "v2021-03-25",
and by deploying the db schema as a gen 2 using
sanity graphql deploy --dataset development --generation gen2
. The latter gave me a bunch of warnings, too many to go through, but I am now worried some of these warnings might be relevant here

Aug 4, 2021, 5:25 AM

user A
managed to solve it by running
sanity upgrade
in the sanity cli. This upgraded a number of outdated packages and now the query seems to work. Unfortunately, some other queries broke, so I am going through them right now. Thanks for your input anyway.

Aug 4, 2021, 8:27 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.