Skip to content

Commit

Permalink
Fix compatibility with new graphql v1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jan 22, 2020
1 parent 9ed1ab4 commit b38637e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 25 deletions.
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ matrix:
- gemfile: graphql-1.7.gemfile
env: GRAPHQL_RUBY_VERSION=1_7 CI=true
rvm: 2.3.8
- gemfile: graphql-1.8.gemfile
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
- gemfile: graphql-latest.gemfile
env: GRAPHQL_RUBY_VERSION=LATEST CI=true
rvm: 2.3.8
- gemfile: graphql-1.7.gemfile
env: GRAPHQL_RUBY_VERSION=1_7 CI=true
rvm: 2.4.5
- gemfile: graphql-1.8.gemfile
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
- gemfile: graphql-latest.gemfile
env: GRAPHQL_RUBY_VERSION=LATEST CI=true
rvm: 2.4.5
- gemfile: graphql-1.7.gemfile
env: GRAPHQL_RUBY_VERSION=1_7 CI=true
rvm: 2.5.7
- gemfile: graphql-1.8.gemfile
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
- gemfile: graphql-latest.gemfile
env: GRAPHQL_RUBY_VERSION=LATEST CI=true
rvm: 2.5.7
- gemfile: graphql-1.7.gemfile
env: GRAPHQL_RUBY_VERSION=1_7 CI=true
rvm: 2.6.5
- gemfile: graphql-1.8.gemfile
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
- gemfile: graphql-latest.gemfile
env: GRAPHQL_RUBY_VERSION=LATEST CI=true
rvm: 2.6.5
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gem "pry"

gem "graphql", "~> 1.8.4"
gem "graphql", "~> 1.10"

# Specify your gem's dependencies in graphql-guard.gemspec
gemspec
2 changes: 1 addition & 1 deletion graphql-1.8.gemfile → graphql-latest.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ source "https://rubygems.org"
gem "pry"
gem 'coveralls'

gem "graphql", "~> 1.8.4"
gem "graphql", "~> 1.10"

gemspec
24 changes: 17 additions & 7 deletions lib/graphql/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ def initialize(policy_object: nil, not_authorized: DEFAULT_NOT_AUTHORIZED)

def use(schema_definition)
schema_definition.instrument(:field, self)
schema_definition.target.instance_eval do
def default_filter
GraphQL::Filter.new(except: default_mask).merge(only: ->(schema_member, ctx) {
schema_member.metadata[:mask] ? schema_member.metadata[:mask].call(ctx) : true
})
end
end
add_schema_masking!(schema_definition)
end

def instrument(type, field)
Expand Down Expand Up @@ -55,6 +49,22 @@ def guard_proc(type, field)

private

def add_schema_masking!(schema_definition)
default_filter_proc = Proc.new do
def default_filter
GraphQL::Filter.new(except: default_mask).merge(only: ->(schema_member, ctx) {
schema_member.metadata[:mask] ? schema_member.metadata[:mask].call(ctx) : true
})
end
end

if schema_definition.is_a?(Class) # GraphQL-Ruby version >= 1.10
schema_definition.class_eval(&default_filter_proc)
else
schema_definition.target.instance_eval(&default_filter_proc)
end
end

def policy_object_guard(type, field_name)
policy_object && policy_object.guard(type, field_name)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/inline_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Inline
GraphQL::ExecutionError.new("Not authorized to access #{type}.#{field}")
})
end
when '1_8'
when 'LATEST'
class PostType < GraphQL::Schema::Object
guard ->(_post, _args, ctx) { ctx[:current_user].admin? }
field :id, ID, null: false
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/policy_object_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.guard(type, field)
query QueryType
use GraphQL::Guard.new(policy_object: GraphqlPolicy)
end
when '1_8'
when 'LATEST'
class PostType < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: true
Expand Down
20 changes: 15 additions & 5 deletions spec/graphql/guard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,21 @@

result = Inline::Schema.execute(query, variables: {'userId' => user.id}, context: {current_user: user})

expect(result['errors']).to include({
"message" => "Field 'postsWithMask' doesn't exist on type 'Query'",
"locations" => [{"line" => 1, "column" => 23}],
"fields" => ["query", "postsWithMask"]
})
case ENV['GRAPHQL_RUBY_VERSION']
when '1_7'
expect(result['errors']).to include({
"message" => "Field 'postsWithMask' doesn't exist on type 'Query'",
"locations" => [{"line" => 1, "column" => 23}],
"fields" => ["query", "postsWithMask"]
})
when 'LATEST'
expect(result['errors']).to include({
"message" => "Field 'postsWithMask' doesn't exist on type 'Query'",
"locations" => [{"line" => 1, "column" => 23}],
"path" => ["query", "postsWithMask"],
"extensions" => {"code" => "undefinedField", "typeName" => "Query", "fieldName" => "postsWithMask"}
})
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "bundler/setup"

ENV['GRAPHQL_RUBY_VERSION'] ||= '1_8'
ENV['GRAPHQL_RUBY_VERSION'] ||= 'LATEST'

if ENV['CI']
require 'simplecov'
Expand Down

0 comments on commit b38637e

Please sign in to comment.