The best way to use the Sanity Client from within the new Studio #3359
-
I was wondering what the path for using the client from within the studio was when the part system is gone? Under V2 whenever I needed to query some content for use in odd places where no client was passed into the context I needed it, I would import from the parts system like so: import sanityClient from 'part:@sanity/base/client'
const client = sanityClient.withConfig({ apiVersion: '2022-06-17' }) Now there is a new // sanity.config.js
export default createConfig({
// ...more configuration above
document: {
newDocumentOptions: (prev, {currentUser, creationContext}) => {
const verticalCategoryId = 'cebd3e27-22cc-44db-9603-5fdaf1695f8d'
// TODO get the product options defined within the category to use below
return [
...prev,
{
id: 'productByCategorySingle',
templateId: 'product-by-category',
type: 'initialValueTemplateItem',
title: 'Vertical Photography',
parameters: {
categoryId: verticalCategoryId,
variants: [] // TODO: scaffold out the variants based on product options
},
},
]
}
}
} I could definitely import the client the same way I would from a frontend application, but I figure I should check best practices since I'm already in the Studio context. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
Hi Frank. It looks like the v3 team has recommended using dependency injection, instantiating the store in a |
Beta Was this translation helpful? Give feedback.
Unfortunately the new document options resolver is still synchronous - this release only adds a
getClient()
function (not a React hook) to a specific context - the one passed to reference resolvers.