Issue accessing models via Sanity GraphQL API
30 replies
Last updated: Sep 9, 2021
B
I've created a simple sanity model with a document called model with a name, datatable, and slug, and then tried to access it via the Sanity Graphql API. I have run sanity graphql deploy, generated a token for the client, but I have been unable to get allModels to return anything but an empty list. The dataset is public. There are at least 5 models in the studio that are published. Is there a step I'm missing?EDIT: Added url:
https://drv14kqs.api.sanity.io/v1/graphql/production/default
https://drv14kqs.api.sanity.io/v1/graphql/production/default
query{ allModels{ name _id } }
Sep 9, 2021, 1:43 AM
E
If you activate the playground (answer Y when running
sanity graphql deploy) and then check the SCHEMA menu there, you can see all your options. It might be
allModelinstead of
allModelsfor example?
Sep 9, 2021, 6:26 AM
B
Thanks for the tip, unfortunately I can read the schema in insomnia and it's definitely allModels. allModel doesn't exist. It looks like the query is correct, but I get nothing
Sep 9, 2021, 4:29 PM
Also, have you confirmed that all of your Model documents are published and not in drafts?
Sep 9, 2021, 6:55 PM
B
Yep only one dataset is available, the default production one. Yep, data is published as well. I can access it via GROQ just fine
Sep 9, 2021, 7:16 PM
Mind sharing the url of the GROQ result so I can compare? You can DM me if you'd prefer it to be private.
Sep 9, 2021, 7:18 PM
B
Not at all. I've never built a GROQ URL before, just use the JS Client
Sep 9, 2021, 7:22 PM
B
Not at all. I've never built a GROQ URL before, just use the JS Client
Sep 9, 2021, 7:22 PM
Got it! Do you have the Vision plugin installed in your studio? If so, any query you run there will give you a url right above the results.
Sep 9, 2021, 7:24 PM
Got it! Do you have the Vision plugin installed in your studio? If so, any query you run there will give you a url right above the results.
Sep 9, 2021, 7:24 PM
B
https://drv14kqs.api.sanity.io/v1/data/query/production?query=*%5B_type%20%3D%3D%20'Model'%5D
Weird that this one uses Model and graphql wants AllModels
Weird that this one uses Model and graphql wants AllModels
Sep 9, 2021, 7:24 PM
Interesting. The result is showing as empty on my end. Let me look up your project details.
Sep 9, 2021, 7:26 PM
It looks like the dataset is set to private. Is it showing as public under your Datasets tab in your project in manage.sanity.io
Sep 9, 2021, 7:29 PM
B
Sorry, I toggled that this morning to test the token with GROQ. I have switched it back to public. The GQL results above were done when it's public, although I did try setting it private and adding a token
Sep 9, 2021, 7:30 PM
You're right, that is strange that your GQL wants Models plural, not Model. I just created the same structure and mine wanted allModel. Can you try running
sanity graphql undeploythen
sanity graphql deploy?
Sep 9, 2021, 7:46 PM
B
Done, no change in the schema or output
Sep 9, 2021, 7:54 PM
B
Done, no change in the schema or output
Sep 9, 2021, 7:54 PM
It's possible that using generation 1 of the API may be causing this. Could you try
sanity graphql deploy --generation gen2or
sanity graphql deploy --generation gen3to see if either of those versions work?
Sep 9, 2021, 9:29 PM
It's possible that using generation 1 of the API may be causing this. Could you try
sanity graphql deploy --generation gen2or
sanity graphql deploy --generation gen3to see if either of those versions work?
Sep 9, 2021, 9:29 PM
B
Running either of the gens appear the same. Weirdly, I also just lost the ability to see all of my content in Sanity Studio, but the Vision plugin still returns data
Sep 9, 2021, 10:29 PM
B
Sure, very rough: https://github.com/benwis/praxis_sanity
Sep 9, 2021, 10:31 PM
B
Not at all
Sep 9, 2021, 10:39 PM
OK I got it! You'll be able to get your documents back in the desk structure if you change the
In your
Now, to get your stuff from GQL: I pulled your
And a new file called
Since I changed the schema, I redeployed GraphQL and tested it out in the playground. Keep in mind that as you keep working on this and making changes to your schema, you'll have to redeploy GQL each time you change it.
modelsdocument's
nameproperty to
Model. At some point, the schema must have gotten changed but not the
nameon the documents themselves. This worked for me:
{ title: "Models", name: "Model", type: "document", fields: [...] }
sanity.jsonfile, you can remove 'vision' from the "plugins" array, since it's enabled in the development environment already.
Now, to get your stuff from GQL: I pulled your
Modelschema into it's own file so that you wouldn't run into issues with strict schemas . So, this leaves your
schema.jsfile looking like this:
// First, we must import the schema creator import createSchema from 'part:@sanity/base/schema-creator' // Then import schema types from any plugins that might expose them import schemaTypes from 'all:part:@sanity/base/schema-type' import model from './model' // Then we give our schema to the builder and provide the result to Sanity export default createSchema({ // We name our schema name: 'praxis_oem', // Then proceed to concatenate our document type // to the ones provided by any plugins that are installed types: schemaTypes.concat([ model ]) })
model.jslike this:
export default { // This is the display name for the type title: "Models", // The identifier for this document type used in the api's name: "Model", // Documents have the type 'document'. Your schema may describe types beyond documents // but let's get back to that later. type: "document", // Now we proceed to list the fields of our document fields: [ { // The display name for this field title: "Name", // The identifier for this field used in the api's name: "name", // The type of this field type: "string", }, { name: 'specTable', type: 'table' }, { // The display name for this field title: "Slug", // The identifier for this field used in the api's name: "slug", // The type of this field type: "slug", }, ] }
Sep 9, 2021, 11:02 PM
B
Thank you. Can you commit the changes and I'll redeploy it. Thanks for all the help!
Sep 9, 2021, 11:03 PM
B
Merged and everything looks good. Thanks again!
Sep 9, 2021, 11:25 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.