2
votes

How can I be able to update a node with only one field change and leave the rest of the fields alone?

My User Type

type User {
        id: ID!
        user_id: String!
        username: String!
        email: String!
        role: Role!
        isVerified: Boolean!
    }

My Input Types

input UserUpdateInput {
    user_id: String
    username: String
    email: String
    password: String
    role: Role
    isVerified: Boolean
    }
input UserWhereUniqueInput {
    id: ID
    user_id: String
    email: String
    }

My Mutation type

type Mutation {
        updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput): User
    }

My Resolver

function updateUser(root, args, context, info){
    return context.db.mutation.updateUser({
      data: args.data,
      where: {
      id: args.where.id     
      }
    }, info)
  }

This is the request am sending on the GraphQL playground

mutation{
    updateUser(
    data: {
      isVerified: true
    }
    where:{
    user_id :  "afc485b"
        }
    )
  {
    isVerified
  }
}

This is the error am getting

{
  "errors": [
    {
      "message": "Cannot read property 'mutation' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'mutation' of undefined"

Someone help me. What am I missing? After updating my server as suggested by Daniel Rearden on the answer section, am getting a new error

    {
      "message": "Cannot read property 'updateUser' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updateUser"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'updateUser' of undefined"
2

2 Answers

0
votes

The error is the result of not correctly adding the db property to you context. Assuming you're still using version 1, your code should look something like this:

const { prisma } = require('./generated/prisma-client')

const server = new ApolloServer({
  ...
  context: {
    db: prisma,
  },
})
0
votes

First thing I notice is you GQL query is not correct.

Yours:

mutation{
    updateUser(
    data: {
      isVerified: true
    }
    where:{
    user_id :  "afc485b"
        }
    )
  {
    isVerified
  }
}
  1. after the word "mutation" you set a name to the call, i.e. "UpdateUser" but can literally be anything. for each part of the

  2. where clause you need to make the check value an object, i.e. where: { myProperty: {eq: "some value"}}

So your query should be more like this:

mutation UpdateUser {
    updateUser(
      data: {isVerified: true}
      where:{user_id : {eq: "afc485b"}}
    )
  {
    isVerified
  }
}

Hope that helps a little... I didn't fully read the rest but thought this would help with the initial error you were getting.