graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Readme gives bad advise on how to build Schema, Client and Queries

Open fnordfish opened this issue 6 years ago • 4 comments

The samples suggest to load the Schema while the ruby code is loaded. This will lead to an ruby application not loading at all, if the remote api is offline or responding invalid data.

I'd suggest to at least promote lazy handling like this:

# Star Wars API example wrapper
module SWAPI
  # Configure GraphQL endpoint using the basic HTTP network adapter.
  HTTP = GraphQL::Client::HTTP.new("https://example.com/graphql") do
    def headers(context)
      # Optionally set any HTTP headers
      { "User-Agent": "My Client" }
    end
  end  

  # Fetch latest schema on init, this will make a network request
  def self.Schema
    @schema ||= GraphQL::Client.load_schema(HTTP)
  end

  def self.Client 
    @client ||= GraphQL::Client.new(schema: Schema, execute: HTTP)
  end
end

fnordfish avatar Dec 13 '18 15:12 fnordfish

I've made the ||= changes myself too to get around ruby giving the "warning: already initialized", "Warning: already initialized constant" warnings. I can't seem to get around the HTTP still giving me the warnings though. Any suggestions? Thanks!

JayNova avatar Jan 11 '19 15:01 JayNova

@JayNova If you mean this error

/initializers/test.rb:2: warning: toplevel constant HTTP referenced by GraphQL::Client::HTTP
/initializers/test.rb:2:in `<module:ApiTest>': undefined method `new' for HTTP:Module (NoMethodError)

I had this problem but it was because I wasn't requiring these

require "graphql/client"
require "graphql/client/http"

It seemed to fix the problem when I did

mathewdbutton avatar Jan 16 '19 21:01 mathewdbutton

This is an issue for me as I am trying to write a gem which will be used by other applications. I would like users of my gem to configure it with (for example) a Rails initializer. This does not appear to be possible with the gem's current design without doing some pretty weird hacks.

I am setting allow_dynamic_queries on my Client instance to true but I notice in the source code that this option will be removed in future versions. Please provide a solution to my situation if you decide to do this. :-)

It should (IMO) be possible to require a gem that uses graphql-client without having to make a request to an external HTTP API or load a schema from disk.

Thanks for the gem !

bobf avatar Mar 08 '20 14:03 bobf

@mathewdbutton Totally missed your response and it'd been forever! But I'll just throw this out there. I keep getting these warnings even after doing the requires:

warning: already initialized constant ModuleName::HTTP
warning: previous definition of HTTP was here

We run microservices and I have a module for every service. So this can be a bit annoying...

JayNova avatar Sep 24 '20 19:09 JayNova