Can't use `#to_param` as a field any more
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu bằng cách chạy bản tái hiện Ruby trong issue với graphql-client 0.21.0 và so sánh với 0.16.0. Theo dõi cách các field có kiểu được dispatch qua GraphQL::Client::Schema::ObjectClass#method_missing, đặc biệt khi Object đã định nghĩa #to_param. Được xem là hoàn tất khi một field có tên to_param được phân giải thành giá trị của GraphQL field mà không làm hỏng các phương thức thông thường của object.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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}"
- Ngôn ngữ chính
- Ruby
- Star
- 78
- Fork
- 228
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của github-community-projects/graphql-client
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
github-community-projects/graphql-client#78 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
github-community-projects/graphql-client#77 · 2 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
github-community-projects/graphql-client#76 · 4 bình luận · 1 reaction ·
-
Errors not populating correctlyĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
github-community-projects/graphql-client#75 · 1 bình luận · 1 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
github-community-projects/graphql-client#67 · 2 bình luận ·
Tất cả issue của github-community-projects/graphql-client
Issue tương tự
-
Kanton Column not HiddenĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 61/100
hitobito/hitobito_pfadi_de#164 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 7 ngày
-
Tallied counts an insertion even when the underlying insert raises without changing the dataĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Windows: packaged_agent_ruby? never matches 'Puppet Labs\Puppet', so the openvox gem is not foundĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
-
Falha nos links do postĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100