Skip to content

Commit 5746b83

Browse files
committed
Add Readme
1 parent 901bf6d commit 5746b83

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# GraphQL::QueryResolver
2+
GraphQL::QueryResolver is an add-on to [graphql-ruby](https://github.com/rmosologo/graphql-ruby)
3+
that allows your field resolvers to minimize N+1 SELECTS issued by ActiveRecord.
24

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/graphql/activerecord`. To experiment with that code, run `bin/console` for an interactive prompt.
5+
GraphQL::QueryResolver will analyze the AST from incoming GraphQL queries and
6+
try to match query selections to `ActiveRecord::Reflections` present in your
7+
`ActiveRecord` models.
48

5-
TODO: Delete this and the text above, and describe your gem
9+
Every matched selection will be then passed on to
10+
`ActiveRecord::Associations::Preloader.new` so your queries now only issue
11+
one `SELECT` statement for every level of the GraphQL AST.
612

713
## Installation
814

@@ -14,15 +20,43 @@ gem 'graphql-query-resolver'
1420

1521
And then execute:
1622

17-
$ bundle
23+
$ bundle
1824

1925
Or install it yourself as:
2026

21-
$ gem install graphql-query-resolver
27+
$ gem install graphql-query-resolver
2228

2329
## Usage
24-
25-
TODO: Write usage instructions here
30+
```ruby
31+
require 'graphql/query_resolver'
32+
33+
# In your field resolver
34+
# Assuming `Project < ActiveRecord::Base` and a `ProjectType` GraphQL type:
35+
#
36+
field :projects do
37+
type types[ProjectType]
38+
39+
resolve -> (obj, args, ctx) {
40+
# Wrap your field resolve operation with `GraphQL::QueryResolver`
41+
GraphQL::QueryResolver.run(Project, ctx, ProjectType) do
42+
Project.all
43+
end
44+
}
45+
end
46+
47+
# QueryResolver works the same way for single objects
48+
49+
field :comment do
50+
type CommentType
51+
argument :id, !types.ID
52+
53+
resolve -> (obj, args, ctx) {
54+
GraphQL::QueryResolver.run(Comment, ctx, CommentType) do
55+
Comment.find(args['id'])
56+
end
57+
}
58+
end
59+
```
2660

2761
## Development
2862

@@ -34,7 +68,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
3468

3569
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphql-query-resolver. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
3670

37-
To run the specs across all supported versions of Rails, check out the repo and follow these steps:
71+
To run the specs across all supported versions of ActiveRecord, check out the repo and follow these steps:
3872
```bash
3973
$ bundle install
4074
$ bundle exec appraisal install

0 commit comments

Comments
 (0)