Matt Coady
Matt Coady

Reputation: 3856

Prisma graphql updateNode mutation

I'm trying to setup the updateNode mutation in graphql with Prisma running on GraphQL-yoga server. Here's the error I'm receiving when I try to run the mutation:

"Variable \"$_v0_data\" got invalid value { data: { name: \"Test\" }, where: { id: \"cjqulnr0yftuh0a71sdkek697\" } }; Field \"data\" is not defined by type CocktailUpdateInput.\nVariable \"$_v0_data\" got invalid value { data: { name: \"Test\" }, where: { id: \"cjqulnr0yftuh0a71sdkek697\" } }; Field \"where\" is not defined by type CocktailUpdateInput."

Here's my mutation resolver:

const Mutation = {
  async updateCocktail(parent, args, ctx, info) {
    const data = { ...args };
    delete data.id;
    const where = {id: args.id};
    return await ctx.db.mutation.updateCocktail({ data, where }, info);
  },
}

datamodel.prisma:

type Cocktail {
  id: ID! @unique
  name: String!
  info: String
  glass: Glass
  ingredients: [Ingredient]
  steps: [Step]
}

schema.graphql

type Mutation {
  updateCocktail(data: CocktailUpdateInput!, where: CocktailWhereUniqueInput!): Cocktail
}

and finally here's what I'm trying to execute in playground:

mutation{
  updateCocktail(
    where: {id: "cjqulnr0y0tuh0a71sdkek697"},
    data: {
      name: "Test"
    }
  ){
    id
    name
  }
}

Upvotes: 2

Views: 800

Answers (2)

Errorname
Errorname

Reputation: 2469

If I read your resolver correctly, you resolvers does the following:

  • Take the args and put them in data (without the id)
  • Take the id in the args and put it in where

But, in the playground, you give the following args:

args = {
  where: {id: "cjqulnr0y0tuh0a71sdkek697"},
  data: {
    name: "Test"
  }
}

They are already well formed! Which means your resolvers is gonna do the step as follow and build the following variables:

data = {
  where: {id: "cjqulnr0y0tuh0a71sdkek697"},
  data: {
    name: "Test"
  }
}

where = { id: null }

You can fix this two ways:

1/ Don't rebuild data and where in the resolvers and just pass the args down to prisma

2/ When calling your mutations, give it the args as follow:

updateCocktail(id: "abc", name: "Test") {...}

Upvotes: 1

nickzor
nickzor

Reputation: 84

According to your error, the problem should lie in your playground execution. It is taking your "where" and "data" as data types.

You could try doing something more like this:

 mutation UpdateCocktailMutation($data: CocktailUpdateInput!, $where: CocktailWhereUniqueInput!) {
    updateCocktail(data: $data, where: $where) {
      id
      name
    }
  }

and in your bottom of the playground they have a query variable field. Fill it will your variable data. Do correct my case sensitivity and naming conventions as I may have missed out on parts of it.

Upvotes: 0

Related Questions