Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in initialized schema as an option #107

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

kbaum
Copy link
Contributor

@kbaum kbaum commented Oct 9, 2023

Not sure if this is a problem others have run into or if there is a better solution. We are using Graphlient within sidekiq and since it is a multithreaded process, we are creating an instance of Graphlient::Client during each job execution. Even though we are caching our schema on disk, our schema.json is large and there is significant time and memory spent loading the schema from disk. My thought is that we might be able to instead load one schema in a rails initializer and share it among all threads. Thus far it has seemed to work in a multithreaded sidekiq instance:

initializer code

$graphlient_schema = Graphlient::Schema.new('https://graphql.foo.com/graphql', 'lib/graphql_schema_foo.json')

Initialize graphlient client

Graphlient::Client.new(
  $graphlient_schema.http,
  schema_path: $graphlient_schema.path,
  schema: $graphlient_schema,
  headers: {
    'content-type' => 'application/json',
    'Authorization' => 'Bearer 123',
  }
)

Let me know what you think. Is it fully theadsafe to share a Graphlient::Schema like this?

@dblock
Copy link
Collaborator

dblock commented Oct 9, 2023

Looks good! I don't see a reason why this can't work.

  • I think with this schema_path and schema become mutually exclusive, so we should raise an error if both are specified?
  • Add to README and CHANGELOG.

@kbaum
Copy link
Contributor Author

kbaum commented Oct 9, 2023

I think with this schema_path and schema become mutually exclusive, so we should raise an error if both are specified?
Great point!

Will raise an error if both schema and schema_path are provided and add to README.

Thanks @dblock!

@kbaum kbaum force-pushed the pass-in-loaded-schema branch 2 times, most recently from 6e413c6 to 4535bd3 Compare October 10, 2023 01:18
@kbaum
Copy link
Contributor Author

kbaum commented Oct 10, 2023

@dblock raises an error from the constructor of both schema_path and schema are given as an option. Also added some documentation. Let me what you think.

Thanks!

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some language nits, please?

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
lib/graphlient/client.rb Outdated Show resolved Hide resolved
lib/graphlient/client.rb Outdated Show resolved Hide resolved
@kbaum
Copy link
Contributor Author

kbaum commented Oct 12, 2023

Addressed all comments. Have another look when you get a chance.

Thanks!

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more nit in the markdown and we're good to go. Consider the others. Thanks for hanging in here with me!

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@kbaum
Copy link
Contributor Author

kbaum commented Oct 13, 2023

Updated with the latest requested changes. Thanks for feedback!

@dblock
Copy link
Collaborator

dblock commented Oct 14, 2023

Rubocop is failing, https://github.com/ashkan18/graphlient/actions/runs/6510603592/job/17703718899?pr=107, needs a rubocop -a ; rubocop --auto-gen-config.

Also I was thinking that this code looks a bit awkward, we pass both schema.http and schema.

client = Graphlient::Client.new(
  schema.http, # uses schema here
  schema: schema, # uses schema here
  headers: {
    'Authorization' => 'Bearer 123',
  }
)

Can we simplify further or rewrite this example not to use schema.http?

@kbaum
Copy link
Contributor Author

kbaum commented Oct 14, 2023

Rubocop is failing, https://github.com/ashkan18/graphlient/actions/runs/6510603592/job/17703718899?pr=107, needs a rubocop -a ; rubocop --auto-gen-config.

Running into trouble on my local machine running rubocop. First trying with ruby-3.2.2 I get:

➜  graphlient git:(pass-in-loaded-schema) bundle exec rubocop
wrong number of arguments (given 5, expected 1)
/Users/karlbaum/.rbenv/versions/3.2.2/lib/ruby/3.2.0/psych.rb:322:in `safe_load'
/Users/karlbaum/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-0.56.0/lib/rubocop/config_loader.rb:185:in `yaml_safe_load'
/Users/karlbaum/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-0.56.0/lib/rubocop/config_loader.rb:158:in `load_yaml_configuration'

Noticed the rubocop build uses ruby-2.7.2 so tried that and the funny thing is I get a message from rubocop saying this ruby version is not supported. I'm confused how it is working in the build.

➜  graphlient git:(pass-in-loaded-schema) bundle show rubocop
/Users/karlbaum/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-0.56.0
➜  graphlient git:(pass-in-loaded-schema) bundle exec rubocop
Error: Unknown Ruby version 2.7 found in `.ruby-version`.
Supported versions: 2.1, 2.2, 2.3, 2.4, 2.5

Tried install ruby-2.5 since i had come this far but that gave me all sorts of errors including that it's not supported:

➜  graphlient git:(pass-in-loaded-schema) rbenv install 2.5.9
To follow progress, use 'tail -f /var/folders/bw/xp6pctg57gj8s_vxy0yyf53w0000gn/T/ruby-build.20231014123604.94280.log' or pass --verbose
Downloading openssl-1.1.1u.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/e2f8d84b523eecd06c7be7626830370300fbcc15386bf5142d72758f6963ebc6
Installing openssl-1.1.1u...
Installed openssl-1.1.1u to /Users/karlbaum/.rbenv/versions/2.5.9

Downloading ruby-2.5.9.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.9.tar.bz2
Installing ruby-2.5.9...

WARNING: ruby-2.5.9 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

ruby-build: using readline from homebrew
ruby-build: using libyaml from homebrew
ruby-build: using gmp from homebrew

BUILD FAILED (macOS 13.5.2 using ruby-build 20230717)

I finally got rubocop to somewhat work by changing back to ruby-2.7.2 and upgrading locally to rubocop 0.80.1. I had to also change TargetRubyVersion: 2.2 to TargetRubyVersion: 2.4 in .rubocop.yml. Even then, when I ran bundle exec rubocop, I got a bunch of cop failures not related to my changes.

Can't explain why it works in build, but maybe we should update rubocop to the latest version?

@kbaum kbaum force-pushed the pass-in-loaded-schema branch 2 times, most recently from dc294ab to 151d581 Compare October 14, 2023 17:23
@kbaum
Copy link
Contributor Author

kbaum commented Oct 14, 2023

Can we simplify further or rewrite this example not to use schema.http?

For now I took away the schema.http.

Thinking we could simplify further by allowing for passing in the schema as the first argument since the schema has a reference to the url.

client = Graphlient::Client.new(
  schema, # pass in url or schema here
  headers: {
    'Authorization' => 'Bearer 123',
  }
)

@dblock
Copy link
Collaborator

dblock commented Oct 14, 2023

1 Warning
⚠️ Unless you're refactoring existing code or improving documentation, please update CHANGELOG.md.
Here's an example of a CHANGELOG.md entry:

* [#107](https://github.com/ashkan18/graphlient/pull/107): Pass in initialized schema as an option - [@kbaum](https://github.com/kbaum).

Generated by 🚫 Danger

You do need to add this too.

@dblock
Copy link
Collaborator

dblock commented Oct 14, 2023

rubocop

I think you may see different results with doing bundle exec rubocop …, generally this gets sorted out when using rbenv or similar

Can't explain why it works in build, but maybe we should update rubocop to the latest version?

We should in a separate PR.

@kbaum
Copy link
Contributor Author

kbaum commented Oct 14, 2023

Added changelog entry. Sorry I missed that. Thanks!

@dblock dblock merged commit 3638cd8 into ashkan18:master Oct 16, 2023
9 checks passed
@dblock
Copy link
Collaborator

dblock commented Oct 16, 2023

Merged, thanks!

@kbaum
Copy link
Contributor Author

kbaum commented Oct 16, 2023

Thanks @dblock!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants