Issue with fetching drafts from Sanity and resolving it with the correct token.
14 replies
Last updated: Feb 5, 2024
M
Hey all,
I have an issue with fetching drafts from Sanity, here is my code, it's pretty basic:
I'm trying to get all drafts, if I run the same query inside Vision inside the Sanity studio, it does return 1 document. If I change the query to return non-drafts (e.g. *[]), the code also works fine, it's just drafts that it doesn't want to return (it returns an empty array, no errors). Could it be some permissions or set up issue?
I have an issue with fetching drafts from Sanity, here is my code, it's pretty basic:
const clientConfig: ClientConfig = { dataset, projectId, apiVersion, useCdn: false, perspective: "previewDrafts", }; const client = createClient(clientConfig); const query = `*[ (_id in path('drafts.**')) ]`; const data = await client.fetch(query);
Feb 5, 2024, 1:13 PM
H
That kinda sounds like the client isnt authenticated đ¤ Since that is the only reason it shouldnt return any documents for draft-queries, but it works correctly for other queries: https://www.sanity.io/docs/ids#:~:text=The%20default%2C%20fixed,on%20your%20plan .
Feb 5, 2024, 1:52 PM
H
Ie, all documents with
.in the
_idneed to be gotten by a authenticated client.
Feb 5, 2024, 1:54 PM
M
How do I authenticate the client? I tried adding an API token in the client config, but it didn't make a difference
Feb 5, 2024, 2:03 PM
H
Oh, the
perspective: previewDraftsactually fetches how any given document would look if its drafts were publisdeh and it would be returned by a given query. Ie, it merges the draft-version into a specific
_idand stops using the
drafts.*prefix: https://www.sanity.io/docs/js-client#previewdrafts
Feb 5, 2024, 2:08 PM
H
Ie, since youre searching for âgive me all draftsâ in a mode that says âno draft exists, they are all publishedâ you get 0 results.
Feb 5, 2024, 2:09 PM
H
Authenticating a client should be as simple as setting
token: TOKENbut in this case that isnt the problem, but it will be relevant dependent on how you move forward đ ( ie, change
perspectiveto something normal and continue using
_id in pathsor change your query to just get the documents you want drafts for with
previewDraftsenabled )
Feb 5, 2024, 2:12 PM
H
M
Hmm, I tried every combination of perspective (published, raw, no perspective option), with or without an API token, still returning empty
Feb 5, 2024, 2:30 PM
M
The docs seem to say this about the token:
// token: process.env.SANITY_SECRET_TOKEN // Only if you want to update content with the client
Feb 5, 2024, 2:30 PM
H
Any document with a
client.config() after configuring and verifying the token is in the returned configuration-object⢠run a ânormalâ query which should return something against a private dataset, with and without token, thus verifying your authentication is correct, if the results are different with and without token
.in its
_idwill need to be authenticated to be queried ( per the gotcha here, and painful experience: https://www.sanity.io/docs/ids#:~:text=The%20default%2C%20fixed,on%20your%20plan . ) . To verify if you have successfully authenticated with your client you can:⢠run
client.config() after configuring and verifying the token is in the returned configuration-object⢠run a ânormalâ query which should return something against a private dataset, with and without token, thus verifying your authentication is correct, if the results are different with and without token
Feb 5, 2024, 2:43 PM
H
This might be the wrong path, the error might be something else. But if you get the result you want from vision and not from your client, authentication-problems are what my intuition tells me is the problem đ
Feb 5, 2024, 2:45 PM
M
Thanks, will try debugging config and see if I find any issues
Feb 5, 2024, 2:56 PM
M
Turns out the token wasn't the correct one, once I replaced it, it works!
Feb 5, 2024, 4:04 PM
M
Thanks for the help
user C
Feb 5, 2024, 4:05 PM
Sanityâ build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.