rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.36k stars 1.37k forks source link

Broadcasting for nodes field #5006

Open pert5432 opened 2 days ago

pert5432 commented 2 days ago

Hi, We are trying to implement broadcasting and can't set broadcastable: true for nodes fields from Relay.

Setting it in the field definition like this: field :replies, Types::Chat::MessageType.connection_type, broadcastable: true doesn't work presumably because connection_type doesn't pass any field options to creation of the node field: (schema/member/relay_shortcuts.rb:53)

def connection_type
   initialize_relay_metadata
   @connection_type ||= begin
     conn_name = self.graphql_name + "Connection"
     edge_type_class = self.edge_type
     Class.new(connection_type_class) do
       graphql_name(conn_name)
       edge_type(edge_type_class) # This function takes field_options param
    end
  end
end

types/relay/connection_behaviors.rb:57

def edge_type(edge_type_class, ..., field_options: nil)
...

...
  define_nodes_field(node_nullable, field_options: field_options) # field_options is always nil here because connection_type does not set it when calling edge_type
end

If I hardcode the parameter into the edge_type call like this it makes the field broadcastable:

def connection_type
...
edge_type(edge_type_class, field_options: { broadcastable: true }) 
...
end

I tried setting the param in our connection type like so nodes_field(field_options: { broadcastable: true }) with no success. The connection type is used in our BaseObject like this: connection_type_class Types::ExtendedConnection.

Is there a way we can set these fields to broadcastable?

rmosolgo commented 2 days ago

Hey, thanks for the detailed report. I agree there should be some way to make nodes broadcastable. It seemse like field_options is supposed to be used for that, but I guess it's not quite doing the job! I'll take a look soon and follow up here.