Skip to content

Commit

Permalink
feat(Subscriptions::Event) improve subscription data API
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Apr 17, 2017
1 parent bffa642 commit a24f6f1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/graphql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def initialize(name)
# @return [String, nil] the triggered event, if this query is a subscription update
attr_reader :subscription_name

# @return [String, nil]
attr_reader :operation_name

# Prepare query `query_string` on `schema`
# @param schema [GraphQL::Schema]
# @param query_string [String]
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/query/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def spawn(key:, selection:, parent_type:, field:)
)
end

def to_h
@values
end

class FieldResolutionContext
extend Forwardable

Expand Down
1 change: 1 addition & 0 deletions lib/graphql/subscriptions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require "graphql/subscriptions/event"
require "graphql/subscriptions/instrumentation"

module GraphQL
Expand Down
13 changes: 13 additions & 0 deletions lib/graphql/subscriptions/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
module GraphQL
module Subscriptions
class Event
attr_reader :name, :arguments, :context
def initialize(name:, arguments:, context:)
@name = name
@arguments = arguments
@context = context
end
end
end
end
8 changes: 6 additions & 2 deletions lib/graphql/subscriptions/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def before_query(query)

def after_query(query)
subscriptions = query.context[:subscriptions]
if subscriptions
if subscriptions && subscriptions.any?
@subscriber.register(query, subscriptions)
end
end
Expand All @@ -42,7 +42,11 @@ def initialize(inner_proc)
def call(obj, args, ctx)
subscriptions = ctx[:subscriptions]
if subscriptions
subscriptions << [args, ctx]
subscriptions << Subscriptions::Event.new(
name: ctx.field.name,
arguments: args,
context: ctx,
)
nil
elsif ctx.field.name == ctx.query.subscription_name
# The root object is _already_ the subscription update:
Expand Down
29 changes: 25 additions & 4 deletions spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize(schema:, **options)
end

def register(query, subscriptions)
subscriptions.each do |(args, ctx)|
subscriptions.each do |ev|
# The `ctx` is functioning as subscription data.
# IRL you'd have some other model that persisted the subscription
@database.add(ctx.field.name, args, ctx)
@database.add(ev.name, ev.arguments, ev.context)
end
end

Expand Down Expand Up @@ -102,6 +102,7 @@ def int
otherPayload: InMemoryBackend::Payload.new,
)
}
let(:database) { InMemoryBackend::Database.new }
let(:schema) {
payload_type = GraphQL::ObjectType.define do
name "Payload"
Expand All @@ -120,14 +121,14 @@ def int
end

query_type = subscription_type.redefine(name: "Query")

db = database
GraphQL::Schema.define do
query(query_type)
subscription(subscription_type)
use GraphQL::Subscriptions,
subscriber_class: InMemoryBackend::Subscriber,
options: {
database: InMemoryBackend::Database.new,
database: db,
}
end
}
Expand Down Expand Up @@ -166,4 +167,24 @@ def int
assert_equal({"str" => "Update", "int" => 3}, socket_1.deliveries[1]["data"]["payload"])
end
end

describe "subscribing" do
it "doesn't call the subscriber for invalid queries" do
query_str = <<-GRAPHQL
subscription ($id: ID){
payload(id: $id) { str, int }
}
GRAPHQL

res = schema.execute(query_str, context: { socket_id: "1" }, variables: { "id" => "100" }, root_value: root_object)
assert_equal true, res.key?("errors")
assert_equal 0, database.subscriptions.size
end
end

describe "trigger" do
it "coerces args somehow?"
it "pushes errors"
it "handles errors during trigger somehow?"
end
end

0 comments on commit a24f6f1

Please sign in to comment.