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

Question: Static Objects

Open andyl opened this issue 7 years ago • 4 comments
trafficstars

Looks like Graphql-client wants queries to be stored in static constants. Is that right?

If so - why? And how do I enable dynamic queries?

andyl avatar Jul 19 '18 00:07 andyl

graphql-client first parsed your query and then statically check types and mutations is matched to the schema.

The parsing time can be waste if we are querying same thing repeatly, so graphql-client asks you to store the query AST it just parsed into const to avoid GC and reuse them.

david50407 avatar Nov 01 '18 10:11 david50407

@andyl , I had the same question. The issue, at least for my use case with Rails, is I can only establish an authenticated session and setup the GraphQL client inside an instance method. Then, working with constants can be very difficult. Even using workarounds like const_set, you still have to attach that constant to some parent object.

It looks like the current version has an attribute allow_dynamic_queries that you should be able to set to true, but it's got a deprecation warning. For now, the solution I've found is either use that, or redefine the query method. It's a temporary hack. You'll need to watch for version changes closely if you update the gem.

flavio-b avatar Nov 12 '18 09:11 flavio-b

I have multiple GraphQL servers with the same API, and I want to write one client class which may be parameterized by (server + authentication for that server), e.g

gql_client = MyGraphqlClient.new( server, authentication)
qgl_client.prebuilt_query(
    parameter_1,
    parameter_2 )

I may have a parameterized query defined in the MyGraphqlClient class, but it cannot be parsed into a usable query until the GraphQL::Client is instantiated with .new(...), which I cannot do until MyGraphqlClient is instantiated (at which time I'll actually know which server to connect to, and the authentication to use to do so!) - so I cannot store the query statically, as the information required to instantiate the GraphQL::Client is not known statically.

If/when dynamic queries are actually removed, I would become unable to use this library.

awittha avatar Sep 20 '19 17:09 awittha

In my case, we have a GraphQL API server, and several backends. Each of the backends wants to be able to call the other backends through the API server. But getting the queries to compile requires the API server to be up! How do you resolve this chicken & egg problem without allow_dynamic_queries ??

Our approach was to refactor to use const_missing to do the compilation, but that seems to be outside the ken of whatever magic is being used to determine that the query is a constant in the first place. I.e., there should be very little efficiency difference between:

class Wack
   AMole = MyClient.parse(....)
   ...
end

and

class Wack
  def self.const_missing(const)
    if const == :AMole
      @amole ||= MyClient.parse(...)
    else
      super
    end
  end
  ...
end

yet this still raises https://github.com/github/graphql-client/blob/master/guides/dynamic-query-error.md . We will enable dynamic queries, but for sure we'll be leaving in the same boat as @awittha above if we can't do them at all.

cobbr2 avatar Feb 12 '20 00:02 cobbr2