Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subquery within a select #301

Open
2 tasks done
Designer023 opened this issue Nov 4, 2024 · 1 comment
Open
2 tasks done

Subquery within a select #301

Designer023 opened this issue Nov 4, 2024 · 1 comment

Comments

@Designer023
Copy link

Designer023 commented Nov 4, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Question

This is a bit of a strange question, but I have a schema where I have an array of types, so that I can use it as a page builder kind of thing:

{
    name: "content",
    type: "array",
    title: "Content",
    of: [
        defineArrayMember({
            type: "block",
        },
        defineArrayMember({
            type: "image",
        }),
        ...
        // Here's the important one:
        defineArrayMember({
            type: "articleFeed",
        }),
     ]
}

There are more types than in the code, but this is just an example.

I can query each of these in my parent schema using something like:

const query = q("*").filter("page").grab({
  body: q("body[]", { isArray: true }).select({
    "_type == 'image'": ["@", ImageQuery],
    "_type == 'block'": ["@", q.contentBlock()],
    "_type == 'articleFeed'": ["@", q.object({
      // @todo
    })],
  }),
})

I can successfully get the articleFeed data back here, but what I would like to do is to, instead of @ do a "sub query" for a feed of articles. I have managed to do this by replacing the @ with a raw query, but is there any way I can make this into a groqd query, so that it's cleaner and more robust?

.select({
    "_type == 'articleFeed'": ["{items, 'articles': *[_type == 'article']{title, 'slug':slug.current}[1..5] }", ArticleCardQuery],
    })
})

Bonus

Is it conceivably possible to use a value from the articleFeed, such as count or tag so that the article feed can be configured from the sanity CMS?

I'm not worried about if the query is slow as this is a build-time query and won't be run very often

@Designer023
Copy link
Author

Looks like I have figured this out:

Create a query first

const articleFeed =  q("").grab({
  articles: q("*")
    .filterByType("article")
    .grab({
      title: q.string()
    })
})

Then we can use it, and as a bonus we can use the schema too:

{
  "_type == 'articleFeed'":
  [
    articleFeed.query,
    articleFeed.schema
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant