Skip to content
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

Deprecate .graphql_definition #3750

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/graphql/base_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def initialize_copy(other)
alias :graphql_name :name
# Future-compatible alias
# @see {GraphQL::SchemaMember}
alias :graphql_definition :itself
def graphql_definition(silence_deprecation_warning: false)
itself
end

def type_class
metadata[:type_class]
Expand Down Expand Up @@ -194,7 +196,7 @@ def self.resolve_related_type(type_arg)
resolve_related_type(Object.const_get(type_arg))
else
if type_arg.respond_to?(:graphql_definition)
type_arg.graphql_definition
type_arg.graphql_definition(silence_deprecation_warning: true)
else
type_arg
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/boolean_type.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::BOOLEAN_TYPE = GraphQL::Types::Boolean.graphql_definition
GraphQL::BOOLEAN_TYPE = GraphQL::Types::Boolean.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/directive/deprecated_directive.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::Directive::DeprecatedDirective = GraphQL::Schema::Directive::Deprecated.graphql_definition
GraphQL::Directive::DeprecatedDirective = GraphQL::Schema::Directive::Deprecated.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/directive/include_directive.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::Directive::IncludeDirective = GraphQL::Schema::Directive::Include.graphql_definition
GraphQL::Directive::IncludeDirective = GraphQL::Schema::Directive::Include.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/directive/skip_directive.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::Directive::SkipDirective = GraphQL::Schema::Directive::Skip.graphql_definition
GraphQL::Directive::SkipDirective = GraphQL::Schema::Directive::Skip.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/float_type.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::FLOAT_TYPE = GraphQL::Types::Float.graphql_definition
GraphQL::FLOAT_TYPE = GraphQL::Types::Float.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/id_type.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::ID_TYPE = GraphQL::Types::ID.graphql_definition
GraphQL::ID_TYPE = GraphQL::Types::ID.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/int_type.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::INT_TYPE = GraphQL::Types::Int.graphql_definition
GraphQL::INT_TYPE = GraphQL::Types::Int.graphql_definition(silence_deprecation_warning: true)
2 changes: 1 addition & 1 deletion lib/graphql/relay/global_id_resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call(obj, args, ctx)
if obj.is_a?(GraphQL::Schema::Object)
obj = obj.object
end
type = @type.respond_to?(:graphql_definition) ? @type.graphql_definition : @type
type = @type.respond_to?(:graphql_definition) ? @type.graphql_definition(silence_deprecation_warning: true) : @type
ctx.query.schema.id_from_object(obj, type, ctx)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/relay/page_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
module GraphQL
module Relay
# Wrap a Connection and expose its page info
PageInfo = GraphQL::Types::Relay::PageInfo.graphql_definition
PageInfo = GraphQL::Types::Relay::PageInfo.graphql_definition(silence_deprecation_warning: true)
end
end
25 changes: 18 additions & 7 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ class << self
# - Cause the Schema instance to be created, if it hasn't been created yet
# - Delegate to that instance
# Eventually, the methods will be moved into this class, removing the need for the singleton.
def_delegators :graphql_definition,
def_delegators :deprecated_graphql_definition,
# Execution
:execution_strategy_for_operation,
# Configuration
Expand All @@ -854,6 +854,10 @@ class << self
:id_from_object=, :object_from_id=,
:remove_handler

def deprecated_graphql_definition
graphql_definition(silence_deprecation_warning: true)
end

# @return [GraphQL::Subscriptions]
attr_accessor :subscriptions

Expand Down Expand Up @@ -896,8 +900,15 @@ def find(path)
@find_cache[path] ||= @finder.find(path)
end

def graphql_definition
@graphql_definition ||= to_graphql
def graphql_definition(silence_deprecation_warning: false)
@graphql_definition ||= begin
unless silence_deprecation_warning
message = "Legacy `.graphql_definition` objects are deprecated and will be removed in GraphQL-Ruby 2.0. Use a class-based definition instead."
caller_message = "\n\nCalled on #{self.inspect} from:\n #{caller(1, 25).map { |l| " #{l}" }.join("\n")}"
GraphQL::Deprecation.warn(message + caller_message)
end
to_graphql
end
end

def default_filter
Expand Down Expand Up @@ -932,16 +943,16 @@ def plugins
def to_graphql
schema_defn = self.new
schema_defn.raise_definition_error = true
schema_defn.query = query && query.graphql_definition
schema_defn.mutation = mutation && mutation.graphql_definition
schema_defn.subscription = subscription && subscription.graphql_definition
schema_defn.query = query && query.graphql_definition(silence_deprecation_warning: true)
schema_defn.mutation = mutation && mutation.graphql_definition(silence_deprecation_warning: true)
schema_defn.subscription = subscription && subscription.graphql_definition(silence_deprecation_warning: true)
schema_defn.validate_timeout = validate_timeout
schema_defn.validate_max_errors = validate_max_errors
schema_defn.max_complexity = max_complexity
schema_defn.error_bubbling = error_bubbling
schema_defn.max_depth = max_depth
schema_defn.default_max_page_size = default_max_page_size
schema_defn.orphan_types = orphan_types.map(&:graphql_definition)
schema_defn.orphan_types = orphan_types.map { |t| t.graphql_definition(silence_deprecation_warning: true) }
schema_defn.disable_introspection_entry_points = disable_introspection_entry_points?
schema_defn.disable_schema_introspection_entry_point = disable_schema_introspection_entry_point?
schema_defn.disable_type_introspection_entry_point = disable_type_introspection_entry_point?
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/input_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def to_graphql
type_defn.mutation = mutation
type_defn.ast_node = ast_node
all_argument_definitions.each do |arg|
type_defn.arguments[arg.graphql_definition.name] = arg.graphql_definition # rubocop:disable Development/ContextIsPassedCop -- legacy-related
type_defn.arguments[arg.graphql_definition(silence_deprecation_warning: true).name] = arg.graphql_definition(silence_deprecation_warning: true) # rubocop:disable Development/ContextIsPassedCop -- legacy-related
end
# Make a reference to a classic-style Arguments class
self.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(type_defn)
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def to_graphql
type_defn.type_membership_class = self.type_membership_class
type_defn.ast_node = ast_node
fields.each do |field_name, field_inst| # rubocop:disable Development/ContextIsPassedCop -- legacy-related
field_defn = field_inst.graphql_definition
field_defn = field_inst.graphql_definition(silence_deprecation_warning: true)
type_defn.fields[field_defn.name] = field_defn # rubocop:disable Development/ContextIsPassedCop -- legacy-related
end
type_defn.metadata[:type_class] = self
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class List < GraphQL::Schema::Wrapper
include Schema::Member::ValidatesInput

def to_graphql
@of_type.graphql_definition.to_list_type
@of_type.graphql_definition(silence_deprecation_warning: true).to_list_type
end

# @return [GraphQL::TypeKinds::LIST]
Expand Down
11 changes: 9 additions & 2 deletions lib/graphql/schema/member/cached_graphql_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ module CachedGraphQLDefinition
# A cached result of {.to_graphql}.
# It's cached here so that user-overridden {.to_graphql} implementations
# are also cached
def graphql_definition
@graphql_definition ||= to_graphql
def graphql_definition(silence_deprecation_warning: false)
@graphql_definition ||= begin
unless silence_deprecation_warning
message = "Legacy `.graphql_definition` objects are deprecated and will be removed in GraphQL-Ruby 2.0. Use a class-based definition instead."
caller_message = "\n\nCalled on #{self.inspect} from:\n #{caller(1, 25).map { |l| " #{l}" }.join("\n")}"
GraphQL::Deprecation.warn(message + caller_message)
end
to_graphql
end
end

# This is for a common interface with .define-based types
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/non_null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NonNull < GraphQL::Schema::Wrapper
include Schema::Member::ValidatesInput

def to_graphql
@of_type.graphql_definition.to_non_null_type
@of_type.graphql_definition(silence_deprecation_warning: true).to_non_null_type
end

# @return [GraphQL::TypeKinds::NON_NULL]
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def visit(schema, member, context_description)
end
when Class
if member.respond_to?(:graphql_definition)
graphql_member = member.graphql_definition
graphql_member = member.graphql_definition(silence_deprecation_warning: true)
visit(schema, graphql_member, context_description)
else
raise GraphQL::Schema::InvalidTypeError.new("Unexpected traversal member: #{member} (#{member.class.name})")
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/string_type.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# frozen_string_literal: true
GraphQL::STRING_TYPE = GraphQL::Types::String.graphql_definition
GraphQL::STRING_TYPE = GraphQL::Types::String.graphql_definition(silence_deprecation_warning: true)
40 changes: 20 additions & 20 deletions spec/graphql/execution/typecast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ def subtype?(*args)
end

it "counts the same type as a subtype" do
assert subtype?(Dummy::Milk.graphql_definition, Dummy::Milk.graphql_definition)
assert !subtype?(Dummy::Milk.graphql_definition, Dummy::Cheese.graphql_definition)
assert subtype?(Dummy::Milk.graphql_definition.to_list_type.to_non_null_type, Dummy::Milk.graphql_definition.to_list_type.to_non_null_type)
assert subtype?(Dummy::Milk.graphql_definition(silence_deprecation_warning: true), Dummy::Milk.graphql_definition(silence_deprecation_warning: true))
assert !subtype?(Dummy::Milk.graphql_definition(silence_deprecation_warning: true), Dummy::Cheese.graphql_definition(silence_deprecation_warning: true))
assert subtype?(Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_list_type.to_non_null_type, Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_list_type.to_non_null_type)
end

it "counts member types as subtypes" do
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Cheese.graphql_definition)
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Milk.graphql_definition)
assert subtype?(Dummy::DairyProduct.graphql_definition, Dummy::Milk.graphql_definition)
assert subtype?(Dummy::DairyProduct.graphql_definition, Dummy::Cheese.graphql_definition)
assert subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), Dummy::Cheese.graphql_definition(silence_deprecation_warning: true))
assert subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), Dummy::Milk.graphql_definition(silence_deprecation_warning: true))
assert subtype?(Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true), Dummy::Milk.graphql_definition(silence_deprecation_warning: true))
assert subtype?(Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true), Dummy::Cheese.graphql_definition(silence_deprecation_warning: true))

assert !subtype?(Dummy::DairyAppQuery.graphql_definition, Dummy::DairyProduct.graphql_definition)
assert !subtype?(Dummy::Cheese.graphql_definition, Dummy::DairyProduct.graphql_definition)
assert !subtype?(Dummy::Edible.graphql_definition, Dummy::DairyProduct.graphql_definition)
assert !subtype?(Dummy::Edible.graphql_definition, GraphQL::DEPRECATED_STRING_TYPE)
assert !subtype?(Dummy::Edible.graphql_definition, Dummy::DairyProductInput.graphql_definition)
assert !subtype?(Dummy::DairyAppQuery.graphql_definition(silence_deprecation_warning: true), Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true))
assert !subtype?(Dummy::Cheese.graphql_definition(silence_deprecation_warning: true), Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true))
assert !subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true))
assert !subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), GraphQL::DEPRECATED_STRING_TYPE)
assert !subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), Dummy::DairyProductInput.graphql_definition(silence_deprecation_warning: true))
end

it "counts lists as subtypes if their inner types are subtypes" do
assert subtype?(Dummy::Edible.graphql_definition.to_list_type, Dummy::Milk.graphql_definition.to_list_type)
assert subtype?(Dummy::DairyProduct.graphql_definition.to_list_type, Dummy::Milk.graphql_definition.to_list_type)
assert !subtype?(Dummy::Cheese.graphql_definition.to_list_type, Dummy::DairyProduct.graphql_definition.to_list_type)
assert !subtype?(Dummy::Edible.graphql_definition.to_list_type, Dummy::DairyProduct.graphql_definition.to_list_type)
assert !subtype?(Dummy::Edible.graphql_definition.to_list_type, GraphQL::DEPRECATED_STRING_TYPE.to_list_type)
assert subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true).to_list_type, Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_list_type)
assert subtype?(Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true).to_list_type, Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_list_type)
assert !subtype?(Dummy::Cheese.graphql_definition(silence_deprecation_warning: true).to_list_type, Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true).to_list_type)
assert !subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true).to_list_type, Dummy::DairyProduct.graphql_definition(silence_deprecation_warning: true).to_list_type)
assert !subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true).to_list_type, GraphQL::DEPRECATED_STRING_TYPE.to_list_type)
end

it "counts non-null types as subtypes of nullable parent types" do
assert subtype?(Dummy::Milk.graphql_definition, Dummy::Milk.graphql_definition.to_non_null_type)
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Milk.graphql_definition.to_non_null_type)
assert subtype?(Dummy::Edible.graphql_definition.to_non_null_type, Dummy::Milk.graphql_definition.to_non_null_type)
assert subtype?(Dummy::Milk.graphql_definition(silence_deprecation_warning: true), Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_non_null_type)
assert subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true), Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_non_null_type)
assert subtype?(Dummy::Edible.graphql_definition(silence_deprecation_warning: true).to_non_null_type, Dummy::Milk.graphql_definition(silence_deprecation_warning: true).to_non_null_type)
assert subtype?(
GraphQL::DEPRECATED_STRING_TYPE.to_non_null_type.to_list_type,
GraphQL::DEPRECATED_STRING_TYPE.to_non_null_type.to_list_type.to_non_null_type,
Expand Down
8 changes: 4 additions & 4 deletions spec/graphql/schema/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

it "uses the argument class" do
arg_defn = field.graphql_definition.arguments.values.first
arg_defn = field.graphql_definition(silence_deprecation_warning: true).arguments.values.first
assert_equal :ok, arg_defn.metadata[:custom]
end

Expand All @@ -34,11 +34,11 @@
end

it "attaches itself to its graphql_definition as type_class" do
assert_equal field, field.graphql_definition.metadata[:type_class]
assert_equal field, field.graphql_definition(silence_deprecation_warning: true).metadata[:type_class]
end

it "camelizes the field name, unless camelize: false" do
assert_equal 'inspectInput', field.graphql_definition.name
assert_equal 'inspectInput', field.graphql_definition(silence_deprecation_warning: true).name
assert_equal 'inspectInput', field.name

underscored_field = GraphQL::Schema::Field.from_options(:underscored_field, String, null: false, camelize: false, owner: nil) do
Expand Down Expand Up @@ -214,7 +214,7 @@ def argument_details(argument_details:, arg1: nil, arg2:)

describe "type" do
it "tells the return type" do
assert_equal "[String!]!", field.type.graphql_definition.to_s
assert_equal "[String!]!", field.type.graphql_definition(silence_deprecation_warning: true).to_s
end

it "returns the type class" do
Expand Down
4 changes: 2 additions & 2 deletions spec/graphql/schema/input_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class TestInput2 < GraphQL::Schema::InputObject

messages = []
GraphQL::Deprecation.stub(:warn, ->(message) { messages << message; nil }) do
input_object.graphql_definition
input_object.graphql_definition(silence_deprecation_warning: true)
end
assert_equal [expected_warning], messages
end
Expand All @@ -716,7 +716,7 @@ class TestInput2 < GraphQL::Schema::InputObject
end

assert_output "", "" do
input_object.graphql_definition
input_object.graphql_definition(silence_deprecation_warning: true)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/schema/interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def new_method
assert new_object_2.method_defined?(:id)

# It gets an overridden description:
assert_equal "The ID !!!!!", new_object_2.graphql_definition.fields["id"].description
assert_equal "The ID !!!!!", new_object_2.graphql_definition(silence_deprecation_warning: true).fields["id"].description
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/schema/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def assert_deep_equal(expected_type, actual_type)
]
}
}
}).graphql_definition
}).graphql_definition(silence_deprecation_warning: true)
end
end

Expand Down
Loading