Skip to content

Insert a graphQL susbcription query #19

@nicohc

Description

@nicohc

Hello,
Thanks for your nice gem,

I wondering if it's possible to send a graphQL query with the gem :
For the moment , i just receive the welcome and ping message of an external websocket. ref:
Here is my code :
(I also added a header and subprotocol, but not sure if it's the right way to mention it too)

In my controller :

  require 'websocket-client-simple'
  require 'json'

  url = "wss://ws.sorare.com/cable"
  subprotocol = 'actioncable-v1-json'
  header = {
    "Authorization" => "Bearer #{ENV['S_TKN']}"
  }

  query = <<-GRAPHQL
    subscription {
      aCardWasUpdated { slug }
    }
  GRAPHQL

  data = { 'query' => query, 'variables' => {} }
  message = { 'command' => 'subscribe', 'identifier' => { 'channel' => 'MyChannel' }, 'data' => data }.to_json
  client = WebSocket::Client::Simple.connect(url, protocol: subprotocol, header: header)

  client.on :open do
    p "opening"
    client.send message
  end

  client.on :message do |message|
    puts "Message : #{message.data}"
  end

  client.on :error do |error|
    puts "Erreur : #{error}"
  end

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions