jeddeloh / rescript-apollo-client

ReScript bindings for the Apollo Client ecosystem
MIT License
126 stars 18 forks source link

Using custom ws implementation #33

Closed mununki closed 4 years ago

mununki commented 4 years ago

Hi,

Actually, I'm trying to use this module on the ground of Gatsby. I faced the one problem which is the error in process of building the static page. I guess the ws is not existing in node env during building Gatsby static page.

image

So, I tried to add ~webSocketImpl, but no gain.

Can you give me a tip to use a custom ws implementation? using JS modulews or bs-ws which is binding to ws.

reference https://github.com/apollographql/subscriptions-transport-ws/issues/191

jeddeloh commented 4 years ago

Can you give me a little more detail of what you mean by "no gain"? I would expect something like this to work:

[@bs.module "ws"]
external ws: ApolloClient.Link.WebSocketLink.webSocketImpl = "default";

let wsLink =
  ApolloClient.Link.WebSocketLink.make(~uri="wss://yourendpoint.com", ~webSocketImpl=ws, ());

Are you getting a type error or what exactly is the error you're encountering?

I haven't looked at bs-ws, but if you want to use that, it will not have the same types. You'll need to cast it to ApolloClient.Link.WebSocketLink.webSocketImpl using %identity or Obj.magic and then pass it in.

mununki commented 4 years ago

@jeddeloh Thank you very much, it fixes my problem. Frankly, I'm very new to ReasonML, so I'm not much experienced with @bs decorator yet. Thank you again.

[@bs.module "ws"]
external ws: ApolloClient.Link.WebSocketLink.webSocketImpl = "default";

let wsLink =
  ApolloClient.Link.WebSocketLink.(
    make(
      ~uri="ws://" ++ graphqlEndpoint,
      ~options=
        ClientOptions.make(
          ~connectionParams=ConnectionParams(Obj.magic(headers)),
          ~reconnect=true,
          (),
        ),
      ~webSocketImpl=ws,
      (),
    )
  );
jeddeloh commented 4 years ago

No problem. Welcome to Reason!