Can't use `#to_param` as a field any more
还没有人认领这个 Issue。
评估
调研方向
首先,使用 graphql-client 0.21.0 运行 issue 中的 Ruby 复现,并将其与 0.16.0 进行比较。跟踪类型化字段如何通过 GraphQL::Client::Schema::ObjectClass#method_missing 进行分派,尤其是在 Object 已经定义了 #to_param 的情况下。当名为 to_param 的字段能够解析为 GraphQL 字段值,同时不破坏普通对象方法时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Below is a piece of code that used to work fine until v0.16.0 and stopped since v0.17.0. Tested with Ruby 3.2.2.
We use similar code to make the objects returned from GraphQL queries work with Rails' route helpers and for that we need to add #to_param to our type definitions.
In a Rails project #to_param is defined on the base Object class and by default returns self.to_s (source).
The objects returned from queries inherit from GraphQL::Client::Schema::ObjectClass which inherits from Object. So when we call #to_param on an object returned from the query it gets dispatched as Object#to_param and doesn't end up in GraphQL::Client::Schema::ObjectClass#method_missing. Thus instead of IDs of records we get strings like "#<#<Module:0x00000001062529f0>::User:0x0000000106316d50>".
It looks that in v0.16.0 all the typed fields were defined as methods on the query objects and since v0.17.0 the library moved to method_missing dispatch. That's why it's no longer possible to use fields that correspond to instance methods of Object, like #to_param.
#!/usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "graphql", "1.13.2"
# gem "graphql-client", "0.16.0" # <--- this works as expected
gem "graphql-client", "0.21.0"
gem "activesupport", "7.1.3.2"
end
require "graphql"
require "graphql/client"
require "active_support/core_ext/object/to_query"
module MyData
User = Data.define(:id)
end
module MyTypes
class User < GraphQL::Schema::Object
field :id, ID, null: false
field :to_param, String, null: false, resolver_method: :resolve_to_param
def resolve_to_param
"user-#{object.id}"
end
end
class Query < GraphQL::Schema::Object
field :current_user, User, null: false
def current_user
MyData::User.new(id: 1)
end
end
end
class MySchema < GraphQL::Schema
query(MyTypes::Query)
end
client = GraphQL::Client.new(schema: MySchema, execute: MySchema)
TEST_QUERY = client.parse(<<~GRAPHQL)
query {
currentUser {
toParam
}
}
GRAPHQL
current_user = client.query(TEST_QUERY).data.current_user
puts "current_user is #{current_user.inspect}"
puts "Expecting #to_param to return \"user-1\", got: #{current_user.to_param.inspect}"
- 主要语言
- Ruby
- 星标
- 78
- 派生
- 228
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
github-community-projects/graphql-client 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 45/100
github-community-projects/graphql-client#78 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
github-community-projects/graphql-client#77 · 2 条评论 ·
-
难度 5/5 一周以上 新手友好度 35/100
github-community-projects/graphql-client#76 · 4 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 35/100
github-community-projects/graphql-client#75 · 1 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 35/100
github-community-projects/graphql-client#67 · 2 条评论 ·
查看 github-community-projects/graphql-client 的全部 Issue
相似的 Issue
-
P2 testing
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
performance
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
openSUSE/open-build-service#20338 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
バグ
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复