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

Detect N+1 Queries #66

Open
eshaiju opened this issue Sep 7, 2017 · 4 comments
Open

Detect N+1 Queries #66

eshaiju opened this issue Sep 7, 2017 · 4 comments

Comments

@eshaiju
Copy link

eshaiju commented Sep 7, 2017

Is there any tool like bullet gem to detect N+1 queries in GraphQL resolvers. Does Bullet gem will detect it?

@dylanahsmith
Copy link
Contributor

You can use GraphQL::Batch::Executor.current.loading to detect whether, at the time of a query, it is in a loaders perform method or from outside of a loaders perform method.

For active record, I think you can use something like the following to raise for unbatched queries

def assert_no_unbatched_queries
  callback = lambda do |name, start, finish, message_id, values|
    raise "unbatched query: #{values[:sql]}" unless GraphQL::Batch::Executor.current.loading
  end
  ActiveSupport::Notifications.subscribed('sql.active_record', callback) do
    yield
  end
end

which allow you to write tests for unbatched queries.

You would need something similar to prevent N+1 queries against other datastores (e.g. memcached or redis).

Let me know if that works for you. This repo should probably at least have an example and mention of how to do something like this in the README.

Does Bullet gem will detect it?

You tell me. I don't have any experience with it.

@eshaiju
Copy link
Author

eshaiju commented Oct 18, 2017

@dylanahsmith Bullet gem seams not picking GraphQl N+1queries.

@feliperoveran
Copy link

feliperoveran commented Mar 19, 2018

@eshaiju I have used bullet on a few projects using GraphQL and it does detect N+1 queries.For reference, I'm using the following configuration on my development.rb environment file:

config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
    Bullet.add_footer = true
  end

@jiss-joy
Copy link

@eshaiju
I am currently using bullet along with GraphQL and it looks like I am also facing the same issue.
Bullet gem not detecting N+1 queries for GraphQL requests.

Is there any update on this?

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

No branches or pull requests

4 participants