github-community-projects / github-community-projects/graphql-client

Can't use `#to_param` as a field any more

Aperta
#20 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Ruby
Stelle
78
Fork
228
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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}"

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

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.

Valutazione

Stack tecnologico
graphql, ruby
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.