我正在遵循How to GraphQL Node.js教程,并且位于“使用Prisma绑定连接服务器和数据库”部分。我认为我已经按照教程正确设置了所有内容,但是当我尝试在GraphQL Playground中执行后突变时,会引发以下错误:
{
"data": null,
"errors": [
{
"message": "Variable '$data' cannot be non input type 'LinkCreateInput!'. (line 1, column 18):\nmutation ($data: LinkCreateInput!) {\n ^",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"post"
]
}
]
}
我什至不确定我应该返回什么,但是当我执行“ info”查询时,我从index.js文件中获取了信息:“ data”:{ “ info”:“这是Hacker News克隆的API!Get Rigth Witcha,imma get ya!” }
因此,我知道信息查询正在运行。当我尝试执行Feed查询时,我在GraphQl游乐场中得到了以下错误:
{
"data": null,
"errors": [
{
"message": "Cannot query field 'links' on type 'Query'. (line 2, column 3):\n links {\n ^",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"feed"
]
}
]
}
因此,只有信息查询有效,其他查询继续发送回错误。我很迷茫,我真的想获得更多使用GRaohQL的技能。当我转到Prisma Client时,无法访问我的服务。他们只让我查看服务器列表。我有控制台的屏幕截图,但是我没有足够的声誉来向他们发布这个问题。
我尝试遍历每个步骤并确保我的代码正确。我还检查了HowToGraphql git存储库,但是它们只包含完整的服务器代码,我想从头开始构建我的数据库,因此它们的代码没有太大帮助。我不确定问题是否出在Prisma Client或我的代码上。任何反馈或帮助将不胜感激!
这是存储库的链接: https://github.com/thelovesmith/HckrNws-Cln-GrphQL-Srvr
请帮助!
src / index.js:
const { GraphQLServer } = require('graphql-yoga')
const { prisma } = require('./generated/prisma-client/index')
// Revolvers
const resolvers = {
Query: {
info: () => 'This is the API for the Hacker News clone!,
Get Rigth Witcha, imma get ya !!' ,
feed: (root, args, context, info) => {
return context.prisma.links()
}
},
Mutation: {
post : (root, args, context) => {
return context.prisma.createLink({
url: args.url,
description: args.description,
})
}
},
}
//Server
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
//initializing context object that is being passed to each
resolver
context: { prisma },
})
server.start(() => console.log('Server is running on
http://localhost:4000'))
schema.graphql:
type Query {
info: String!
feed: [Link!]!
}
type Mutation {
post(url: String!, description: String!): Link!
}
type Link {
id: ID!
description: String!
url: String!
}
prisma.yml:
# The HTTP endpoint for your Prisma API
#endpoint: ''
endpoint: https://us1.prisma.sh/avery-dante-hinds/hckrnws/dev
# Points to the file that contains your datamodel
datamodel: datamodel.prisma
# Specifies language & location for the generated Prisma client
generate:
- generator: javascript-client
output: ../src/generated/prisma-client
datamodel.prisma:
type Link {
id: ID! @ unique
createdAt: DateTime!
description: String!
url: String!
}
当我运行Prisma Deploy时,我会显示一条错误消息:
ERROR: Syntax error while parsing GraphQL query. Invalid input "{
\n id: ID! @ ", expected ImplementsInterfaces, DirectivesConst or
FieldDefinitions (line 1, column 11):
type Link {
^
{
"data": {
"deploy": null
},
"errors": [
{
"locations": [
{
"line": 2,
"column": 9
}
],
"path": [
"deploy"
],
"code": 3017,
"message": "Syntax error while parsing GraphQL query. Invalid
input \"{ \\n id: ID! @ \", expected ImplementsInterfaces,
DirectivesConst or FieldDefinitions (line 1, column 11):\ntype Link {
\n ^",
"requestId": "us1:cjr0vodzl2ykt0a71zuql5544"
}
],
"status": 200
}
答案 0 :(得分:2)
似乎您的数据模型中有错字,应该是@unique
而不是@ unique
。