gatsby-source-sanity icon indicating copy to clipboard operation
gatsby-source-sanity copied to clipboard

Group on fields in nested object structures doesn't seem to work

Open kmelve opened this issue 6 years ago • 11 comments
trafficstars

Let's say you want to group posts by tags.

types: [
    ...schemaTypes,
    {
      name: 'post',
      type: 'document',
      title: 'Post',
      fields: [
        {
          name: 'title',
          type: 'string',
          title: 'Title',
        },
        {
          name: 'tags',
          type: 'array',
          title: 'Tags',
          of: [
              { type: 'reference', to: [{type: 'tag'}]}
          ]
        },
      ]
    },
    {
      name: 'tag',
      type: 'document',
      title: 'Tag',
      fields: [
        {
          name: 'title',
          type: 'string',
          title: 'Title',
        },
      ]
    }
  ])

This results in either image

or

image

kmelve avatar Feb 19 '19 15:02 kmelve

@kmelve What is the correct way to do this? Both of these attempts to group by an array of objects. Even the correct behavior here would not make sense. What would be the natural and correct way to express this in GraphQL?

simen avatar Mar 06 '19 12:03 simen

_raw fields are of JSON type. You can't group on JSON fields. Is this also a problem if querying an array or string field?

rexxars avatar Mar 06 '19 13:03 rexxars

Oh, I see. In the second example, you are querying tags____NODE, which I am surprised even exists - this sounds like a Gatsby bug. You are also trying to group by something which is an object type, whereas all the documentation examples group by simple values (strings). I'm not sure what you are trying to achieve is actually possible in Gatsby (basically group(field: "tags.title"))

rexxars avatar Mar 06 '19 13:03 rexxars

Oh, so ___ actually means "nested", so I suppose this would actually be group(field: "tags___title") or group(field: "tags___id")?

rexxars avatar Mar 06 '19 13:03 rexxars

Yes, that's my understanding too.

kmelve avatar Mar 06 '19 13:03 kmelve

Did you try the tags___title variant?

rexxars avatar Mar 06 '19 13:03 rexxars

have we solved this or does it still need prioritising?

jemmima avatar Mar 13 '19 16:03 jemmima

I think this will be solved with the next release.

kmelve avatar Mar 13 '19 21:03 kmelve

Next release of Gatsby or us?

jemmima avatar Mar 15 '19 08:03 jemmima

Any updates on this yet, please? 😊

danielvanc avatar May 08 '19 20:05 danielvanc