sanity-io / GROQ

Specification for GROQ - The Query Language for JSON
https://groq.dev
MIT License
398 stars 15 forks source link

Spread operators breaks sibling reference field projections, or reference field projections in conditionals #97

Closed toddpadwick closed 2 years ago

toddpadwick commented 2 years ago

I have an array containing multiple object types within it. Within some of these, there are reference fields or even some that are reference arrays. So I need to follow the references, but then catch all the other normal fields with the spread like so:

    landingPageBuilder[] {
            _type == 'industries' => {
             industries[]->{title,summary,headline,icon}, // this works fine, if all the spread operators are removed
             ...
           },
           _type == 'organisationalRoles' => {
             roles[]{
               role->{title,summary,headline,icon}, // this works fine, if all the spread operators are removed
               ...
             },
             ...
           },
           _type == 'testimonial' => {
             testimonial->, // this works fine, if all the spread operators are removed
             ... 
           },
           ... // To catch all other types as normal, which don't contain any reference fields
        }

Unless I've missed something, the above looks correct according to the docs, but those spreads seem to break all references projections. If i remove them, they work fine. but that means I can't catch all l other fields. Is this a bug, or have I made some kind of error?

toddpadwick commented 2 years ago

Solved. I discovered that the spread operators need to be before any functions or conditionals.

judofyr commented 2 years ago

More concretely: The order matters. If you have the spread operator at the end, it will overwrite any fields. You can do {"a": 123, ...} if you want to have "a": 123 as a default value that will be overwritten if the field exists.