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
N
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:
The GraphQL query (that works), is this:
The GROQ query I am trying and fails is this:
This works when I stop at the
But when I add
Anyone any suggestions on what I am doing wrong?
Thanks
{ name: 'table', fields: [ name: 'tableTemplate', type: 'object', fields: [ name: 'rows', type: 'array', .... ] ]
query MyQuery { lessons: allLesson { tables { tableTemplate { rows { cells { values } } } } } }
*[_type == "lesson"]{ "data": tables[].tableTemplate.rows[].cells[].values }
rows[], like so:
*[_type == "lesson"]{ "data": tables[].tableTemplate.rows[] }
.cells[]they have none of it.
Anyone any suggestions on what I am doing wrong?
Thanks
Aug 3, 2021, 5:08 PM
N
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
G
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
fieldsproperty) but in your query it’s an array. Thanks!
Aug 4, 2021, 4:22 AM
N
oh yea sure, I am sorry --the one I shared is a bit rushed.
I took out the unnecessary fields/validation rules and kept only the relevant info in.
Thank you!
// 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', }, ], }, ], }
Thank you!
Aug 4, 2021, 4:38 AM
G
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
G
Thanks for posting that. Oddly, your query from the first post works for me:
With curriculumBenchmark being a document, I’m wondering if you are wanting to create a reference to it (in the
*[_type == 'lesson'] { 'data': tables[].tableTemplate.rows[].cells[].values }
tablesarray) rather than an array of that type. As it is, any documents you create of the type
curriculumBenchmarkwon’t be included in the
lessondocument. (That said, it will still structure the schema used in
lesson.)
Aug 4, 2021, 4:39 AM
N
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
N
user A
managed to solve it by running sanity upgradein 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 – build remarkable experiences at scale
Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.