Skip to main content

Schema Types

The .grab and .grabOne methods are used to "project" and select certain values from documents, and these are the methods that dictate the shape of the resulting schema/data. To indicate what type specific fields should be, we use schemas provided by the groqd library, such as q.string, q.number, q.boolean, and so on.

For example:

q("*")
.filter("_type == 'pokemon'")
.grab({
// string field
name: q.string(),

// number field
hp: ["base.HP", q.number()],

// boolean field
isStrong: ["base.Attack > 50", q.boolean()],
});

The available schema types are shown below.

q.string

Corresponds to Zod's string type.

q.number

Corresponds to Zod's number type.

q.boolean

Corresponds to Zod's boolean type.

q.literal

Corresponds to Zod's literal type.

q.union

Corresponds to Zod's union type.

q.date

A custom Zod schema that can accept Date instances or a date string (and it will transform that date string to a Date instance).

caution

Date objects are not serializable, so you might end up with a data object that can't be immediately serialized – potentially a problem if using groqd in e.g. a Next.js backend data fetch.

q.null

Corresponds to Zod's null type.

q.undefined

Corresponds to Zod's undefined type.

q.array

Corresponds to Zod's array type.

q.object

Corresponds to Zod's object type.

q.slug

A convenience schema to easily grab a slug. Takes a single argument, which is the name of the slug field.

q("*")
.filterByType("blogPost")
.grab({
// 👇 slug schema
slug: q.slug("slug"),
// equivalent to...
slug: ["slug.current", q.string()]
});

q.contentBlock

A custom Zod schema to match Sanity's block type, helpful for fetching data from a field that uses Sanity's block editor. For example:

q("*")
.filter("_type == 'user'")
.grab({ body: q.array(q.contentBlock()) });

Pass an object of the shape { markDefs: z.ZodType } to q.contentBlock to specify custom markdef types, useful if you have custom markdefs, e.g.:

q("*")
.filter("_type == 'user'")
.grab({
body: q.array(q.contentBlock({
markDefs: q.object({ _type: q.literal("link"), href: q.string() })
}))
});

q.contentBlocks

q.contentBlocks, a custom Zod schema, to match a list of q.contentBlock's. Pass an argument of the shape { markDefs: z.ZodType } to specify custom markdef types.

q("*")
.filter("_type == 'user'")
.grab({ body: q.contentBlocks() });