Error deploying GraphQL API in Sanity v3 project

2 replies
Last updated: Dec 16, 2022
Hi, I’m trying to update v2 project to v3. So far going nicely, except when I run
sanity graphql deploy --dataset test --tag default
I get an error.

$ sanity graphql deploy --dataset test --tag default
WARN: More than one API defined, and --dataset/--tag is specified
WARN: This will use the specified flag for ALL APIs!
? Continue with flag override for all APIs? Yes

Error: Multiple GraphQL APIs with the same dataset and tag found (test/default)
  at Object.deployGraphQLApiAction [as default] (~/veri/narrative-content/node_modules/sanity/lib/_chunks/deployApiAction-6e38bde5.js:1555:13)
My sanity.cli.ts:

export default defineCliConfig({
  api: {
    projectId: "xxxxxx",
  },
  graphql: [
    {
      workspace: "production",
    },
    {
      workspace: "test",
    },
  ],
});
Sanity.config.ts

export default defineConfig([
  {
    name: "test", // Identifier used to reference configuration
    title: "test", // Name displayed in Sanity studio dropdown
    projectId: "xxxxxx",
    default: true, // Which config to open by default in Sanity studio
    dataset: "test",
    basePath: "/test", // URL path
    plugins: [deskTool(), media(), muxInput({ mp4_support: "standard" })],
    schema: {
      types: schemaTypes,
    },
  },
  {
    name: "production", // Identifier used to reference configuration
    title: "production", // Name displayed in Sanity studio dropdown
    projectId: "xxxxxx",
    dataset: "production",
    basePath: "/production", // URL path
    plugins: [deskTool(), media(), muxInput({ mp4_support: "standard" })],
    schema: {
      types: schemaTypes,
    },
  },
]);

When I run
$ sanity graphql deploy --help
it only lists
--dry-run
--force
and
--api
as options. Is
--dataset
and
--tag
no longer supported from cli?
Running
sanity graphql deploy
first prompts me to deploy
production
and then
test
and this way deploying works just fine. I’d just like to specify as arguments which env I want to deploy.
AI Update

Based on your configuration, you're running into this issue because in Sanity v3, the way you specify which GraphQL API to deploy has changed. The --dataset and --tag flags still exist but they're causing conflicts when you have multiple workspace-based GraphQL API configurations.

The solution is to use the --api flag instead, which is the v3 way of targeting specific GraphQL deployments when you have multiple workspaces configured.

In your sanity.cli.ts, you've defined two GraphQL APIs based on workspaces:

graphql: [
  {
    workspace: "production",
  },
  {
    workspace: "test",
  },
]

However, these configurations need explicit IDs to be targetable. Update your sanity.cli.ts to include id properties:

export default defineCliConfig({
  api: {
    projectId: "xxxxxx",
  },
  graphql: [
    {
      id: "production-api",
      workspace: "production",
    },
    {
      id: "test-api",
      workspace: "test",
    },
  ],
});

Now you can deploy specific APIs using:

sanity graphql deploy --api test-api

or

sanity graphql deploy --api production-api

The reason --dataset and --tag are causing errors is that when you have workspace-based configurations, each workspace already maps to its corresponding dataset from your sanity.config.ts. Using --dataset and --tag tries to override this for ALL APIs, which creates duplicate configurations for the same dataset/tag combination - hence the error "Multiple GraphQL APIs with the same dataset and tag found (test/default)".

As you discovered, running sanity graphql deploy without flags works because it prompts you interactively for each workspace. But using the --api flag with explicit IDs gives you the programmatic control you're looking for while respecting the workspace-based configuration pattern in v3.

The CLI GraphQL documentation confirms that --api <api-id> is the recommended approach for deploying specific APIs in multi-workspace setups.

And Immediately I get it 🤦‍♂️
I need to define id for each graphQL api like this:

export default defineCliConfig({
  api: {
    projectId: "xxxxxx",
  },
  graphql: [
    {
      id: "production",
      workspace: "production",
    },
    {
      id: "test",
      workspace: "test",
    },
  ],
});
And then

yarn sanity graphql deploy --api test
$ yarn sanity graphql deploy --api test --tag moo
WARN: More than one API defined, and --tag is specified
WARN: This will use the specified flag for ALL APIs!
? Continue with flag override for all APIs? Yes
Deploying only specified APIs: test
⚠ Generating GraphQL API: test/moo
? Do you want to enable a GraphQL playground? Yes
✔ Generating GraphQL API: test/moo

Project: xxxxx
Dataset: test
Tag:   moo
URL:   <https://xt399tbr.api.sanity.io/v1/graphql/test/moo>
✔ Deployed!

:sparkles: Done in 38.58s.

I feel the message
This will use the specified flag for ALL APIs!
is maybe a bit misleading since it actually still only deployed one API which is the test api with tag default 🤔
Also the --tag property seems to be working still, but it’s not listed in options with
sanity deploy --help

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?