Skip to content

Commit

Permalink
Accept blocks to reference schema root types
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 6, 2024
1 parent f068799 commit f5033aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
52 changes: 33 additions & 19 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,44 +430,58 @@ def connections
end
end

def query(new_query_object = nil)
if new_query_object
def query(new_query_object = nil, &lazy_load_block)
if new_query_object || block_given?
if @query_object
raise GraphQL::Error, "Second definition of `query(...)` (#{new_query_object.inspect}) is invalid, already configured with #{@query_object.inspect}"
dup_defn = new_query_object || yield
raise GraphQL::Error, "Second definition of `query(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@query_object.inspect}"
elsif use_schema_subset?
@query_object = block_given? ? lazy_load_block : new_query_object
else
@query_object = new_query_object
add_type_and_traverse(new_query_object, root: true) unless use_schema_subset?
nil
@query_object = new_query_object || lazy_load_block.call
add_type_and_traverse(@query_object, root: true)
end
nil
elsif @query_object.is_a?(Proc)
@query_object = @query_object.call
else
@query_object || find_inherited_value(:query)
end
end

def mutation(new_mutation_object = nil)
if new_mutation_object
def mutation(new_mutation_object = nil, &lazy_load_block)
if new_mutation_object || block_given?
if @mutation_object
raise GraphQL::Error, "Second definition of `mutation(...)` (#{new_mutation_object.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
dup_defn = new_mutation_object || yield
raise GraphQL::Error, "Second definition of `mutation(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
elsif use_schema_subset?
@mutation_object = block_given? ? lazy_load_block : new_mutation_object
else
@mutation_object = new_mutation_object
add_type_and_traverse(new_mutation_object, root: true) unless use_schema_subset?
nil
@mutation_object = new_mutation_object || lazy_load_block.call
add_type_and_traverse(@mutation_object, root: true)
end
nil
elsif @mutation_object.is_a?(Proc)
@mutation_object = @mutation_object.call
else
@mutation_object || find_inherited_value(:mutation)
end
end

def subscription(new_subscription_object = nil)
if new_subscription_object
def subscription(new_subscription_object = nil, &lazy_load_block)
if new_subscription_object || block_given?
if @subscription_object
raise GraphQL::Error, "Second definition of `subscription(...)` (#{new_subscription_object.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
dup_defn = new_subscription_object || yield
raise GraphQL::Error, "Second definition of `subscription(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
elsif use_schema_subset?
@subscription_object = block_given? ? lazy_load_block : new_subscription_object
else
@subscription_object = new_subscription_object
add_subscription_extension_if_necessary
add_type_and_traverse(new_subscription_object, root: true) unless use_schema_subset?
nil
@subscription_object = new_subscription_object || lazy_load_block.call
add_type_and_traverse(@subscription_object, root: true)
end
nil
elsif @subscription_object.is_a?(Proc)
@subscription_object = @subscription_object.call
else
@subscription_object || find_inherited_value(:subscription)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/support/dummy/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,9 @@ def test; "Test"; end
end

class Schema < GraphQL::Schema
query DairyAppQuery
mutation DairyAppMutation
subscription Subscription
query { DairyAppQuery }
mutation { DairyAppMutation }
subscription { Subscription }
max_depth 5
orphan_types Honey
trace_with GraphQL::Tracing::CallLegacyTracers
Expand Down

3 comments on commit f5033aa

@palkan
Copy link

@palkan palkan commented on f5033aa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rmosolgo!

Looks like this commit broke subscriptions: we don't call add_subscription_extension_if_necessary anymore when an object is passed to #subscription anymore (only if we use block API, but it's not mandatory, right?).

@rmosolgo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry for the trouble and thanks for reporting this. It turns out it was broken if you called subscription(...) after use SomeSubscription.

I worked up a patch in #5065 and just released it in 2.3.14. Please let me know if it gives you any more trouble!

@palkan
Copy link

@palkan palkan commented on f5033aa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix!

Please sign in to comment.