-
Notifications
You must be signed in to change notification settings - Fork 2
OXEAN-133 Add nested relation filter opt-in #1691
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,30 +4,24 @@ module Graphoid | |
| module Resolvers | ||
| def self.resolver_class(relation_class, relation_type, association) | ||
| association_name = association.name | ||
| relation_name = Utils.graphqlize(relation_class.name) | ||
|
|
||
| Class.new(GraphQL::Schema::Resolver) do | ||
| type [relation_type], null: true | ||
|
|
||
| self.const_set(:ASSOCIATION_NAME, association_name) | ||
|
|
||
| filter = Graphoid::Filters::LIST[relation_class] | ||
| filter = "Graphoid::Types::#{relation_name}Filter" unless filter | ||
| order = Graphoid::Sorter::LIST[relation_class] | ||
| order = "Graphoid::Types::#{relation_name}Sorter" unless order | ||
|
|
||
| argument :where, filter, required: false | ||
| order = Graphoid::Sorter::LIST[relation_class] | ||
| order = "Graphoid::Types::#{Utils.graphqlize(relation_class.name)}Sorter" unless order | ||
| argument :order, order, required: false | ||
| argument :limit, GraphQL::Types::Int, required: false | ||
| argument :skip, GraphQL::Types::Int, required: false | ||
|
|
||
| def resolve(where: nil, order: nil, limit: nil, skip: nil) | ||
| def resolve(order: nil, limit: nil, skip: nil) | ||
| obj = self.object | ||
| processor = Graphoid::Queries::Processor | ||
|
|
||
| association_name = self.class.const_get(:ASSOCIATION_NAME) | ||
| result = obj.send(association_name) | ||
| result = processor.execute(result, where) if where.present? | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer executes or validates records {
data {
attachments(where: { type: "picture" }) {
id
}
}
}However, other operations are still viable records {
data {
attachments(limit: 10, skip: 0, order: { id: ASC }) {
id
}
}
} |
||
|
|
||
| if order.present? | ||
| order = processor.parse_order(obj.send(association_name), order) | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tells Mongoid to allow the new type of param in the association |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Graphoid | ||
| module MongoidNestedFilter | ||
| private | ||
|
|
||
| def define_association!(macro_name, name, options = {}, &block) | ||
| options = options.dup | ||
| nested_filter = options.delete(:graphoid_nested_filter) | ||
| association = super | ||
|
|
||
| # Mongoid validates association options before Graphoid can inspect the reflection. | ||
| association.options[:graphoid_nested_filter] = nested_filter if nested_filter | ||
| association | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Mongoid::Association::Macros::ClassMethods.prepend(Graphoid::MongoidNestedFilter) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'when querying generated relations' do | ||
| def validation_errors(query) | ||
| TesterMongoSchema.validate(query) | ||
| end | ||
|
|
||
| def expect_argument_not_accepted(query, argument) | ||
| error = validation_errors(query).first | ||
|
|
||
| expect(error.message).to include("doesn't accept argument '#{argument}'") | ||
| end | ||
|
|
||
| it 'should allow an explicitly opted-in relation filter' do | ||
| errors = validation_errors('{ people(where: { account: { id_not: null } }) { id } }') | ||
|
|
||
| expect(errors).to be_empty | ||
| end | ||
|
|
||
| %w[some none every].each do |operator| | ||
| it "should reject a to-many #{operator} relation filter" do | ||
| expect_argument_not_accepted( | ||
| "{ accounts(where: { labels_#{operator}: { id_not: null } }) { id } }", | ||
| "labels_#{operator}" | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| it 'should reject where on a selected to-many relation' do | ||
| expect_argument_not_accepted( | ||
| '{ accounts { labels(where: { name: "label" }) { id } } }', | ||
| 'where' | ||
| ) | ||
| end | ||
|
|
||
| it 'should allow scalar filters and unfiltered relation selection' do | ||
| errors = validation_errors(<<~GRAPHQL) | ||
| { | ||
| accounts(where: { stringField: "account" }) { | ||
| labels(order: { id: ASC }, limit: 1, skip: 0) { id } | ||
| } | ||
| } | ||
| GRAPHQL | ||
|
|
||
| expect(errors).to be_empty | ||
| end | ||
| end |
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before graphoid added every Mongoid association to the generated filter inputs.
Now, graphql-ruby rejects these during static schema validation: