我按照 DigitalOcean 的教程学习了如何创建一个 Prisma GraphQL API。 https://www.digitalocean.com/community/tutorials/how-to-build-a-graphql-api-with-prisma-and-deploy-to-digitalocean-s-app-platform
该应用程序正在访问托管在 digitalOcean 上的 PostgreSQL 数据库,并且一切正常。 现在我想向“Post”模型添加字段“test”: TypeDefs:
type Post {
content: String
id: ID!
published: Boolean!
title: String!
author: User
test: String
}
解析器:
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,
},
})
},
变异:
type Mutation {
createMeme(authorEmail: String, content: String, title: String!, published: Boolean, test: String): Post!
}
模型在 schema.prisma 文件中:
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?
}
当我将它迁移到托管在 Docker 上的本地 PostgreSQL 数据库时,它可以正常工作,以下是我的操作步骤:
- 更改.env文件中的DATABASE_URL为本地数据库
- 执行:“npm prisma migrate dev”
- “npm start”
- “git add .”
- “git commit -m“test field added””
- “git push”
- “npx prisma migrate deploy”
一个迁移无法应用。在从错误中恢复之前,不能应用新的迁移。了解有关如何解决生产数据库中的迁移问题的更多信息:
我通过“npx prisma migrate reset”修复了它。
当我现在尝试针对托管在Digital Ocean上的PostgreSQL数据库执行以下查询时:
mutation{
createMeme(title: "Help", content: "Hellp", published: true, test: "Test") {
id
title
content
published
test
}
}
我收到以下错误:
{"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
}
这是一个奇怪的行为,为什么在我的本地主机上可以运行,但在DigitalOcean托管的数据库上却不能工作? 我在终端中漏掉了一些步骤吗?也许需要执行一些命令?
这是完整的schema.js文件,包括所有解析器、typeDefs等:
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,
}
这是GitHub上的项目: https://github.com/DaFaack/prisma-graphql.git
我在.gitignore文件中只忽略了node_modules和.env两个文件。
.env
文件。 - ArchNoob