
Product
Announcing Precomputed Reachability Analysis in Socket
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Experimental tools for GraphQL over a long-lived connection, including subscription
, @defer
and @stream
. (demo)
# Requires an experimental branch of graphql:
gem "graphql", github: "rmosolgo/graphql-ruby", branch: "defer-directive"
gem "graphql-streaming"
This gem supports queries with @defer
and @stream
.
Use GraphQL::Execution::DeferredExecution
:
# only available in the defer-directive branch:
MySchema.query_execution_strategy = GraphQL::Execution::DeferredExecution
MySchema.subscription_execution_strategy = GraphQL::Execution::DeferredExecution
Choose a transport (HTTP Streaming or ActionCable) and get its client (both built-in clients depend on Object.assign
)
StreamCollector
uses stream.write(patch)
to send chunked responses to the client. For example, you can use it with ActionController::Live
.
Create a collector
and include it in the query as context[:collector]
:
class ChunkedGraphqlsController < ApplicationController
include ActionController::Live
def create
# ...
# initialize the collector with `response.stream`
context = {
collector: StreamCollector.new(response.stream)
}
Schema.execute(query_string, variables: variables, context: context)
# close the stream when the query is done:
response.stream.close
end
end
From JavaScript, use StreamingGraphQLClient
to fetch data:
//= require graphql-streaming/streaming_graphql_client
onResponse = function(response) {
// Handle response.errors / response.data
}
StreamingGraphQLClient.fetch(
"/graphql/",
`query getPost($postId: Int!){
post(id: $postId) {
title
comments @stream {
body
}
}
}`,
{postId: 1},
onResponse,
)
The onResponse
function will be called with GraphQL response after each patch is added.
You can use Rails 5's ActionCable
to send and receive GraphQL.
In your channel, implement an action called #fetch(data)
for executing GraphQL queries. It should use stream_graphql_query
using data["query_id"]
from the client. For example:
class GraphqlChannel < ApplicationCable::Channel
include GraphQL::Streaming::ActionCableChannel
def fetch(data)
query_string = data["query"]
variables = JSON.parse(data["variables"] || "{}")
context = {
# ...
}
# Get the query ID, which is added by the GraphQLChannel client
query_id = data["query_id"]
# Make the query within a `stream_graphql_query` block
stream_graphql_query(query_id: query_id) do |stream_ctx|
# the block provides a subscriber and collector,
# merge them into your context:
merged_ctx = context.merge(stream_ctx)
# don't forget to prevent stale data
merged_ctx[:current_user].reload
MySchema.execute(query_string, variables: variables, context: merged_ctx)
end
end
end
Then, create a GraphQLChannel
to make requests. GraphQLChannel.subscription
contains defaults for App.cable.subscription.create
:
//= require graphql-streaming/graphql_channel
App.graphqlChannel = App.cable.subscriptions.create("GraphqlChannel", GraphQLChannel.subscription)
// OPTIONAL forward log messages to console.log:
// GraphQLChannel.log = console.log.bind(console)
And you can provide overrides if you want:
// Trigger `graphql-channel:ready` when the channel is connected
App.graphqlChannel = App.cable.subscriptions.create(
"GraphqlChannel",
Object.assign(GraphQLChannel.subscription, {
connected: function() {
$(document).trigger("graphql-channel:ready")
},
})
)
Send queries with graphqlChannel.fetch
:
var queryString = "{ ... }"
var queryVariables = { /* ... */ }
var onResponse = function(response) { /* handle response.errors & response.data */}
App.graphqlChannel.fetch(queryString, queryVariables, onResponse)
The onResponse
handler will be called with the whole response each time a patch is received.
ActionCableSubscriber
uses ActionCable
as a backend for GraphQL subscriptions. There are three parts:
The subscriber rides along with your query (as context[:subscriber]
). It listens for triggers from the application, and when they happen, it re-evaluates the query and pushes an update over its channel.
class GraphqlChannel < ApplicationCable::Channel
include GraphQL::Streaming::ActionCableChannel
def fetch(data)
# ...
query_id = data["query_id"]
stream_graphql_query(query_id: query_id) do |stream_ctx|
stream_ctx[:subscriber] # => #<ActionCableSubscriber ... >
# ...
end
end
end
SubscriptionType
is a plain GraphQL::ObjectType
, but its fields are special. They correspond to application triggers. When a trigger is fired, any subscriber who is listening to the corresponding field will re-evaluate its query.
Define subscription fields with subscription
:
SubscriptionType = GraphQL::ObjectType.define do
name "Subscription"
subscription :post, PostType do
argument :id, !types.Int
resolve -> (obj, args, ctx) {
Post.find(args[:id])
}
end
end
MySchema = GraphQL::Schema.define(subscription: SubscriptionType ...)
From your application, you can trigger events on subscription fields. For example, to tell clients that a Post with a given ID changed:
class Post
def after_commit
GraphQL::Streaming::ActionCableSubscriber.trigger(:post, {id: id})
end
end
The arguments passed to .trigger
will be tested against field arguments. Any subscribers who requested a matching query will be updated. For example:
subscription {
post(id: 1) {
... postFields
}
}
would be updated by
GraphQL::Streaming::ActionCableSubscriber.trigger(:post, {id: 1})
A client can unsubscribe from future patches with .clear
. For example:
// Subscribe to data from the server
var queryHandle = App.graphqlChannel.fetch(queryString, queryVariables, onResponse)
// Unsubscribe from server pushes
App.graphqlChannel.clear(queryHandle)
No further patches will be sent to the client.
bundle exec rake test
to run the tests@channel.send(:transmit, payload)
?The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that graphql-streaming demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.