Can't use `#to_param` as a field any more
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia eseguendo la riproduzione Ruby nell’issue con graphql-client 0.21.0 e confrontala con 0.16.0. Traccia il modo in cui i campi tipizzati vengono inoltrati tramite GraphQL::Client::Schema::ObjectClass#method_missing, soprattutto quando Object definisce già #to_param. Il lavoro è completo quando un campo denominato to_param viene risolto nel valore del campo GraphQL senza compromettere i normali metodi degli oggetti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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}"
- Lingua principale
- Ruby
- Stelle
- 78
- Fork
- 228
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di github-community-projects/graphql-client
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
github-community-projects/graphql-client#78 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
github-community-projects/graphql-client#77 · 2 commenti ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
github-community-projects/graphql-client#76 · 4 commenti · 1 reazione ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
github-community-projects/graphql-client#75 · 1 commento · 1 reazione ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
github-community-projects/graphql-client#67 · 2 commenti ·
Tutte le issue di github-community-projects/graphql-client
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
FreeCAD/homebrew-freecad#870 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
security
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
OSCON 2016Apertacontent
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
rubyevents/rubyevents#2148 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
we-promise/sure#3838 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno