node.js - GraphQL/Prisma 客户端服务器错误 : Variable '$data' cannot be non input type 'LinkCreateInput!' .(第 1 行,第 18 列)

标签 node.js server graphql prisma prisma-graphql

我正在关注 How To GraphQL Node.js 教程,我在“使用 Prisma 绑定(bind)连接服务器和数据库”部分。我想我已经根据教程正确设置了所有内容,但是当我尝试在 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 文件中获取了信息:“数据”:{ "info": "这是 Hacker News 克隆的 API!Get Rigth Witcha,imma get ya !!"

所以,我知道信息查询正在运行。当我尝试执行 Feed 查询时,我在 GraphQl playground 中返回了这个错误:

{
  "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 客户端时,我无法访问我的服务。他们只让我查看我的服务器列表。我有控制台的屏幕截图,但我没有足够的声誉来发布这个问题。

我已尝试遍历每个步骤并确保我的代码正确无误。我还检查了 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!
}

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

数据模型.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
}

最佳答案

您的数据模型中似乎有错字,应该是 @unique 而不是 @unique

关于node.js - GraphQL/Prisma 客户端服务器错误 : Variable '$data' cannot be non input type 'LinkCreateInput!' .(第 1 行,第 18 列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203167/

相关文章:

javascript - 如何在node.js中使用console.log写入窗口的不同部分?

javascript - Node Handlebars 在渲染 View 之前添加额外的文本 Node

javascript - 无法使用 Mongoose 模型

node.js - 如何在我的本地计算机上接收来自互联网上远程设备的 http 请求?

android - 将 1000 多条记录上传到服务器的最佳方法是什么,该服务器还包含来自 iOS/Android 应用程序的每条记录的图像?

html - 名称服务器与 URL 转发

javascript - 使用 getInitialProps 保护路由 - Next.js

javascript - Keystone 6 自定义架构 - 突变不起作用 - 超出最大调用堆栈大小

node.js - graphql + apollo-server-express,如何处理 express authMiddleware 中的身份验证错误?

node.js - NodeJs GraphQL 动态查询变量