2

我正在关注 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"
      ]
    }
  ]
}

我什至不确定我应该返回什么,但是当我执行“信息”查询时,我会从我的 index.js 文件中返回信息:“数据”:{“信息”:“这是用于黑客新闻克隆!,Get Rigth Witcha,我得到你!” }

所以,我知道信息查询正在工作。当我尝试执行 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"
      ]
    }
  ]
}

因此,只有 Info 查询有效,其他查询不断发回错误。我很迷茫,我真的很想用 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'))

架构.graphql:

type Query {
  info: String!
  feed: [Link!]!
}

type Mutation {
  post(url: String!, description: String!): Link!
}

type Link {
  id: ID!
  description: String!
  url: String!
}

棱镜.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

数据模型.棱镜:

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
}
4

1 回答 1

3

您的数据模型中似乎有错字,它应该@unique@ unique.

于 2019-01-19T17:16:03.027 回答