Implementing relation with prisma client with Variable "$_v0_data" got invalid value error


#1

I am trying to setup relations between types and wrote a mutation resolver that create the list values but getting the below error

        Error: Variable "$_v0_data" got invalid value { board: { connect: [Object] }, title: "to do", boardId: "cjyey7947hh6x0b36m231qhbc" }; Field "boardId" is not defined by type ListCreateInput. Did you mean board?
        at new CombinedError (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/errors.js:82:28)
        at Object.checkResultAndHandleErrors (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/errors.js:98:15)
        at CheckResultAndHandleErrors.transformResult (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/CheckResultAndHandleErrors.js:9:25)
        at /Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/transforms.js:18:54
        at Array.reduce (<anonymous>)
        at applyResultTransforms (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/transforms/transforms.js:17:23)
        at /Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:97:50
        at step (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:31:23)
        at Object.next (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:12:53)
        at fulfilled (/Users/gabroun/Documents/Sites/react-kanban/server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:3:58)

and here is the code snippet

    // mutation.js 
     async createList(parent, args, ctx, info) {
        const list = await ctx.db.mutation.createList(
          {
            data: {
              board: {
                connect: {
                  id: args.boardId
                }
              },
              ...args
            }
          },
          info
        );
        return list;
      }
      
      // datamodel 
    type Board {
      id: ID! @id
      title: String!
      createdAt: DateTime! @createdAt
      updatedAt: DateTime! @updatedAt
      lists: [List]!
    }

    type List {
      id: ID! @id
      title: String!
      createdAt: DateTime! @createdAt
      updatedAt: DateTime! @updatedAt
      board: Board!
    }

    // schema.graphql
    type Mutation {
      createList(title: String!, boardId: ID!): List!
    }

    // prisma.graphql
    type Mutation {
      createList(data: ListCreateInput!): List!
    }

    input ListCreateInput {
      id: ID
      title: String!
      board: BoardCreateOneWithoutListsInput!
    }


#2

I’m trying to understand what you are doing. You’re using boardId as an argument but boardId is not defined on ‘ListCreateInput’. Explains the error. also why is the field ‘board’ in your ‘ListCreateInput’?

Maybe try to change your ‘ListCreateInput’ to:

input ListCreateInput {
      boardId: ID!
      title: String!
}

Your ‘CreateList’ mutation will then use the args.boardId to connect to the board as it is defined now.

Let me know if you need more help.


#3

basically what i am trying to do to have a relation between board type and list type where list type can be related to board based on boardId which is unique id and board can have an array of lists the ListCreateInput is whats been generated by prisma in prisma.graphql, my definition of mutation createList in schema.graphql and my types are in datamodel.graphql i just put the related codes from these file into the above snippet.
and I was trying to follow docs in my implementation above as per the example here https://www.prisma.io/tutorials/a-guide-to-common-resolver-patterns-ct08#scenario-implementing-relations-with-prisma-client

and yes I would like some help with this one if possible, Thanks


#4

Your datamodel is correct. Your schema.graphql is correct. But I don’t understand why there’s is a completely different mutation type on prisma.graphql. This file seems redundant and one of the reasons why you’re getting the error. In your mutation you’re providing the ‘boardId’ variable which is not defined on the ListCreateInput. I don’t understand how the prisma.graphql is generated and why ListCreateInput is how it is. Just the input of a boardId and a title should do the job. You shouldn’t have to provide the whole board object and also a boardId. The way prisma relations work is that you only have to provide an Id to connect. So in your error I see you provide both the board object and a boardId. You should provide only one of them.

So I’m going on a guess here how I would tackle this error. If I have to keep the prisma.graphql as it is I would try and change the mutation like this:

 async createList(parent, args, ctx, info) {
    const list = await ctx.db.mutation.createList(
      {
        data: {
          board: {
            connect: {
              id: board.id
            }
          },
          ...args
        }
      },
      info
    );
    return list;
  }

The only args you have to provide are title and board. So you should remove ‘boardId’ from your input.

  1. I would remove prisma.graphql file all together. Then you only have to provide ‘boardId’ and title as your input.