首页 > 解决方案 > 变量和参数的类型不匹配 (Int / Int)

问题描述

给定以下 GraphQL 请求和变量:

要求:

query accounts($filter:AccountFilter, $first_:String, $skip_:Int) {
  accounts(filter: $filter, first: $first_, skip: $skip_) {
    id
  }
}

变量:

{
  "filter": {},
  "first_": "3",
  "skip_": 0
}

注意:first_我在和变量名称中添加了下划线,skip_以帮助将它们与参数first和. 区分开来skip

我收到以下错误:

"Type mismatch on variable $first_ and argument first (String / Int)"

"Type mismatch on variable $skip_ and argument skip (Int / Int)"

我故意创建的第一个错误是为了进行健全性检查。变量中的值应该是"first": 3,而不是"first": "3",。第二个错误我不明白为什么我得到它。Int和类型如何Int不匹配?当我正确传递first并更改为.(Int / Int)3StringInt

我究竟做错了什么?

后端规范: Ruby on Rails

参数规格:

"""
Interface for the Account model
"""
type Account {
  friendlyId: String!
  id: ID!
  locations: [Location!]!
  name: String!
  participants: [User!]!
  primaryLocation: Location!
  primarySiteContact: User!
  siteContacts: [User!]!
}

input AccountFilter {
  OR: [AccountFilter!]
}

type Query {
  """
  Details for an Account
  """
  accountDetails(id: ID): Account

  """
  A list of Accounts
  """
  accounts(filter: AccountFilter, first: Int, skip: Int): [Account!]
}

标签: graphqlsyntax-errortype-mismatch

解决方案


所以这是 Ruby 的 graphql 的一个已知问题。graphql这个问题似乎有一个 PR 和可用的修复程序,但即使在更新我的gem之后我仍然遇到这个问题。

您可以在此处查看问题: https ://github.com/rmosolgo/graphql-ruby/issues/2784

该帖子中包含的对我有用的修复程序在这里:

def self.type_from_ast(*args)
  type = super
  type == GraphQL::Types::Int ? type.to_graphql : type
end

推荐阅读