You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-7Lines changed: 41 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,14 @@
1
1
# 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.
2
4
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.
4
8
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.
6
12
7
13
## Installation
8
14
@@ -14,15 +20,43 @@ gem 'graphql-query-resolver'
14
20
15
21
And then execute:
16
22
17
-
$ bundle
23
+
$ bundle
18
24
19
25
Or install it yourself as:
20
26
21
-
$ gem install graphql-query-resolver
27
+
$ gem install graphql-query-resolver
22
28
23
29
## 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 :projectsdo
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 :commentdo
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
+
```
26
60
27
61
## Development
28
62
@@ -34,7 +68,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
34
68
35
69
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.
36
70
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:
0 commit comments