🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue accessing models via Sanity GraphQL API

30 replies
Last updated: Sep 9, 2021
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
query{
  allModels{
    name
    _id
  }
}
Sep 9, 2021, 1:43 AM
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
allModel
instead of
allModels
for example?
Sep 9, 2021, 6:26 AM
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
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
Not at all. I've never built a GROQ URL before, just use the JS Client
Sep 9, 2021, 7:22 PM
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
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
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 undeploy
then
sanity graphql deploy
?
Sep 9, 2021, 7:46 PM
Done, no change in the schema or output
Sep 9, 2021, 7:54 PM
Done, no change in the schema or output
Sep 9, 2021, 7:54 PM
Super weird! I'm getting more eyes on this.
Sep 9, 2021, 8:26 PM
It's possible that using generation 1 of the API may be causing this. Could you try 
sanity graphql deploy --generation gen2
 or 
sanity graphql deploy --generation gen3
to 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 gen2
 or 
sanity graphql deploy --generation gen3
to see if either of those versions work?
Sep 9, 2021, 9:29 PM
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
Do you have a repo you can share?
Sep 9, 2021, 10:30 PM
Sep 9, 2021, 10:31 PM
Do you mind if I sign into your studio to take a look?
Sep 9, 2021, 10:37 PM
Do you mind if I sign into your studio to take a look?
Sep 9, 2021, 10:37 PM
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
models
document's
name
property to
Model
. At some point, the schema must have gotten changed but not the
name
on the documents themselves. This worked for me:
{
      title: "Models",
      name: "Model",
      type: "document",
      fields: [...]
}
In your
sanity.json
file, 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
Model
schema into it's own file so that you wouldn't run into issues with strict schemas . So, this leaves your
schema.js
file 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
  ])
})
And a new file called
model.js
like 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",
    },
  ]
}
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.
Sep 9, 2021, 11:02 PM
Thank you. Can you commit the changes and I'll redeploy it. Thanks for all the help!
Sep 9, 2021, 11:03 PM
Just created the pull request!
Sep 9, 2021, 11:09 PM
Merged and everything looks good. Thanks again!
Sep 9, 2021, 11:25 PM
Happy to help! Glad we got it sorted out!
Sep 9, 2021, 11:50 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.

Was this answer helpful?