diff --git a/.travis.yml b/.travis.yml index 24370ba..35aca92 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Gemfile b/Gemfile index 41fc69b..ade3400 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/graphql-1.8.gemfile b/graphql-latest.gemfile similarity index 72% rename from graphql-1.8.gemfile rename to graphql-latest.gemfile index b258127..67ae9ad 100644 --- a/graphql-1.8.gemfile +++ b/graphql-latest.gemfile @@ -3,6 +3,6 @@ source "https://rubygems.org" gem "pry" gem 'coveralls' -gem "graphql", "~> 1.8.4" +gem "graphql", "~> 1.10" gemspec diff --git a/lib/graphql/guard.rb b/lib/graphql/guard.rb index a03de8f..b059293 100644 --- a/lib/graphql/guard.rb +++ b/lib/graphql/guard.rb @@ -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) @@ -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 diff --git a/spec/fixtures/inline_schema.rb b/spec/fixtures/inline_schema.rb index b207436..5b6a6fc 100644 --- a/spec/fixtures/inline_schema.rb +++ b/spec/fixtures/inline_schema.rb @@ -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 diff --git a/spec/fixtures/policy_object_schema.rb b/spec/fixtures/policy_object_schema.rb index 69bbda4..9e2942c 100644 --- a/spec/fixtures/policy_object_schema.rb +++ b/spec/fixtures/policy_object_schema.rb @@ -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 diff --git a/spec/graphql/guard_spec.rb b/spec/graphql/guard_spec.rb index a6c0870..0b4dab0 100644 --- a/spec/graphql/guard_spec.rb +++ b/spec/graphql/guard_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74e7a27..129e314 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,7 @@ require "bundler/setup" -ENV['GRAPHQL_RUBY_VERSION'] ||= '1_8' +ENV['GRAPHQL_RUBY_VERSION'] ||= 'LATEST' if ENV['CI'] require 'simplecov'