Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

Is it possible to query document schema metadata in Sanity/GROQ?

I have a simple singleton document schema defined in my Sanity/NextJS project, to model my "Colophon" page (richText is a custom block field type):

export default {
    title: 'Colophon',
    name: 'colophon',
    type: 'document',
    __experimental_actions: ['update', 'publish'],
    fields: [
        {
            title: 'Body',
            name: 'body',
            type: 'richText',
            validation: Rule => Rule.required(),
        },
    ],
};

I retrieve this document with a simple query in my NextJS application:

export async function getStaticProps() {
    const colophon = await client.fetch(`
        *[_type == "colophon"][0]
    `);
    // ...
};

Is it possible to write a GROQ query to retrieve the meta title defined in the schema, i.e. Colophon? Although this is a singleton document, I would like to avoid repeating this string in my project if possible. At the moment, I can only see the fields on the document in my results, i.e. body.

Thanks for reading!

question from:https://stackoverflow.com/questions/65901665/is-it-possible-to-query-document-schema-metadata-in-sanity-groq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, I don't believe there is.

As far as I understand what you're after; The schema is defined in the studio-instance and not the datastore. Those two are not hard coupled. I have several studio-instances with small variations on the schemas using one single project/datastore. The API you query to get data does not care which studio and schema was used and cant answer for the actual schema details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...