简体   繁体   中英

How to add a field to a Model in Prisma GraphQL?

I followed this tutorial from DigitalOcean on how to create a prisma GraphQL API. https://www.digitalocean.com/community/tutorials/how-to-build-a-graphql-api-with-prisma-and-deploy-to-digitalocean-s-app-platform

The App is accessing the PostgreSQL database hosted on digitalOcean and everything is working fine. Now I would like to add the field "test" to the "Post" model: TypeDefs:

  type Post {
    content: String
    id: ID!
    published: Boolean!
    title: String!
    author: User
    test: String

  }

Resolver:

Mutation: {
    createMeme: (parent, args) => {
      return prisma.post.create({
        data: {
          title: args.title,
          content: args.content,
          published: args.published,
          author: args.authorEmail && {
            connect: { email: args.authorEmail },
          },
          test: args.test,
          
        },
      })
    },

Mutation:

  type Mutation {
    createMeme(authorEmail: String, content: String, title: String!, published: Boolean, test: String): Post!
  }

Model in schema.prisma file:

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?
  test      String?
}

When I migrate it to my local PostgreSQL database hosted on docker it is working fine, here is what I do:

  1. Change DATABASE_URL in.env file to local database
  2. Execute: "npm prisma migrate dev"
  3. "npm start"

When I now try to change DATABASE_URL to my PostgreSQL Database hosted on digitalOcean I do the following:

  1. "git add."
  2. "git commit -m "test field added""
  3. "git push"
  4. "npx prisma migrate deploy"

In this case I got the following error: Error: P3018

A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database:

I fixed it with "npx prisma migrate reset"

When I now try to excecute the following query against my PostgreSQL database hosted on digital ocean:

mutation{
  createMeme(title: "Help", content: "Hellp", published: true, test: "Test") {
    id
    title
    content
    published
    test
  }
}

I get the following error:

  {"errors": [
    {
      "message": "\nInvalid `prisma.post.create()` invocation in\n/workspace/src/schema.js:61:26\n\n   58 },\n   59 Mutation: {\n   60   createMeme: (parent, args) => {\n→  61     return prisma.post.create({\n            data: {\n              title: 'Help',\n              content: 'Hellp',\n              published: true,\n              author: undefined,\n              test: 'Test'\n              ~~~~\n            }\n          })\n\nUnknown arg `test` in data.test for type PostCreateInput. Did you mean `title`? Available args:\ntype PostCreateInput {\n  title: String\n  content?: String | Null\n  published?: Boolean\n  author?: UserCreateNestedOneWithoutPostsInput\n}\n\n",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createMeme"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "clientVersion": "3.6.0",
          "stacktrace": [
            "Error: ",
            "Invalid `prisma.post.create()` invocation in",
            "/workspace/src/schema.js:61:26",
            "",
            "   58 },",
            "   59 Mutation: {",
            "   60   createMeme: (parent, args) => {",
            "→  61     return prisma.post.create({",
            "            data: {",
            "              title: 'Help',",
            "              content: 'Hellp',",
            "              published: true,",
            "              author: undefined,",
            "              test: 'Test'",
            "              ~~~~",
            "            }",
            "          })",
            "",
            "Unknown arg `test` in data.test for type PostCreateInput. Did you mean `title`? Available args:",
            "type PostCreateInput {",
            "  title: String",
            "  content?: String | Null",
            "  published?: Boolean",
            "  author?: UserCreateNestedOneWithoutPostsInput",
            "}",
            "",
            "",
            "    at Object.validate (/workspace/node_modules/@prisma/client/runtime/index.js:34750:20)",
            "    at PrismaClient._executeRequest (/workspace/node_modules/@prisma/client/runtime/index.js:39729:17)",
            "    at consumer (/workspace/node_modules/@prisma/client/runtime/index.js:39670:23)",
            "    at /workspace/node_modules/@prisma/client/runtime/index.js:39674:49",
            "    at AsyncResource.runInAsyncScope (async_hooks.js:189:9)",
            "    at PrismaClient._request (/workspace/node_modules/@prisma/client/runtime/index.js:39674:27)",
            "    at request (/workspace/node_modules/@prisma/client/runtime/index.js:39779:77)",
            "    at _callback (/workspace/node_modules/@prisma/client/runtime/index.js:39987:14)",
            "    at PrismaPromise.then (/workspace/node_modules/@prisma/client/runtime/index.js:39994:23)",
            "    at resolveField (/workspace/node_modules/graphql/execution/execute.js:468:26)"
          ]
        }
      }
    }
  ],
  "data": null
}

Such a weird behaviour, why is it working on my localhost but not on the digitalocean hosted db? Am I missing some steps in the terminal? Maybe some commands to execute?

Here is the whole schema.js file with all resolvers, typeDefs etc:

    const { gql } = require('apollo-server')
const { prisma } = require('./db')

const typeDefs = gql`
  type User {
    email: String!
    id: ID!
    name: String
    posts: [Post!]!
  }

  type Post {
    content: String
    id: ID!
    published: Boolean!
    title: String!
    author: User
    test: String

  }

  type Query {
    feed: [Post!]!
    post(id: ID!): Post
  }

  type Mutation {
    createUser(data: UserCreateInput!): User!
    createMeme(authorEmail: String, content: String, title: String!, published: Boolean, test: String): Post!
    publish(id: ID!): Post
  }

  input UserCreateInput {
    email: String!
    name: String
    posts: [PostCreateWithoutAuthorInput!]
  }

  input PostCreateWithoutAuthorInput {
    content: String
    published: Boolean
    title: String!
  }
`

const resolvers = {
  Query: {
    feed: (parent, args) => {
      return prisma.post.findMany({
        where: { published: true },
      })
    },
    post: (parent, args) => {
      return prisma.post.findOne({
        where: { id: Number(args.id) },
      })
    },
  },
  Mutation: {
    createMeme: (parent, args) => {
      return prisma.post.create({
        data: {
          title: args.title,
          content: args.content,
          published: args.published,
          author: args.authorEmail && {
            connect: { email: args.authorEmail },
          },
          test: args.test,
          
        },
      })
    },
    publish: (parent, args) => {
      return prisma.post.update({
        where: { id: Number(args.id) },
        data: {
          published: true,
        },
      })
    },
    createUser: (parent, args) => {
      return prisma.user.create({
        data: {
          email: args.data.email,
          name: args.data.name,
          posts: {
            create: args.data.posts,
          },
        },
      })
    },
  },
  User: {
    posts: (parent, args) => {
      return prisma.user
        .findOne({
          where: { id: parent.id },
        })
        .posts()
    },
  },
  Post: {
    author: (parent, args) => {
      return prisma.post
        .findOne({
          where: { id: parent.id },
        })
        .author()
    },
  },
}

module.exports = {
  resolvers,
  typeDefs,
}

Here is the project on GitHub: https://github.com/DaFaack/prisma-graphql.git

The only files in my.gitignore are node_modules and.env

It looks to me that there is a mismatch between the client you are using and the change you just did in the schema so you would need to generate the client so it's in sync with npx prisma generate .

awd, i have checkout your code and i can see i am able to create mutation. As you can see in the screenshot below.

在此处输入图像描述

Commands run:
npm i
npx prisma migrate dev
npm run start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM