Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphiQL.createFetcher does not work for subscriptions-transport-ws #1800

Closed
junminstorage opened this issue Feb 11, 2021 · 12 comments · Fixed by #1819 or #1806
Closed

GraphiQL.createFetcher does not work for subscriptions-transport-ws #1800

junminstorage opened this issue Feb 11, 2021 · 12 comments · Fixed by #1819 or #1806
Labels

Comments

@junminstorage
Copy link

junminstorage commented Feb 11, 2021

As shown in this PR on express-graphql: https://github.com/graphql/express-graphql/pull/739/files#diff-f851f2ce2fbcb0220aaca183def93d7ced5b533fead2016551cfd6d37b357ae5, I started the graphql server with subscription using legacy subscriptions-transport-ws.

Then on the client side I create fetcher like this:

window.GraphiQL.createFetcher({url: fetchURL, subscriptionUrl: ${safeSerialize(
            subscriptionEndpoint,
          )}});

Then I got error as shown in the below screenshot:
Screen Shot 2021-02-10 at 9 29 46 PM

The same client side code works if I started a graphql server using graphql-ws though:
https://github.com/graphql/express-graphql/pull/739/files#diff-4f5f57981087dfef3de9e960e86656c7a1d1ef9f424fd0a75e32751163a286e1

@acao
Copy link
Member

acao commented Feb 11, 2021

@junminstorage thank you for looking into this, this is strange and not how it should behave.

@acao acao added the bug label Feb 11, 2021
@junminstorage
Copy link
Author

I also pulled in @graphiql/toolkit since the fetcher is moved under toolkit now, I still got the same error. The bug may be somewhere in those lines in this function?

export const createWebsocketsFetcherFromUrl = (
url: string,
connectionParams?: ClientOptions['connectionParams'],
) => {
let wsClient: Client | null = null;
let legacyClient: SubscriptionClient | null = null;
if (url) {
try {
try {
// TODO: defaults?
wsClient = createClient({

@acao
Copy link
Member

acao commented Feb 12, 2021

@junminstorage no changes have been made to address your bug yet. we will probably remove support for the legacy protocol

@acao
Copy link
Member

acao commented Feb 12, 2021

I will let you know when there is an update, but for now just letting you know that the location of the package has changed

@junminstorage
Copy link
Author

junminstorage commented Feb 13, 2021

I tried out the approach as in this function:

export const createWebsocketsFetcherFromUrl = (
, both wsClient and legacyClient can be initialized against either of two types of subscription ws servers without any errors being thrown. so it indeed is a bug.

the difference is that, if we add a listener for connected event using legacy client, like below:

          let client = new SubscriptionClient(${safeSerialize(subscriptionEndpoint)}, {
            reconnect: true
          });
         client.on("connected", listenerFn);

The listenerFn function will only be called if it is subscription-transport-ws server.

The express-graphql is already on legacy ws client, it will be great if we support it. how hard is to add legacyClient as one of the options in createFetcher?

@acao
Copy link
Member

acao commented Feb 13, 2021

express graphql should not be using deprecated packages imo. the legacy protocol is deprecated, so we will remove it in the next release

@junminstorage
Copy link
Author

https://www.npmjs.com/package/subscriptions-transport-ws still have 1.7 millions weekly download. we have the chance to support both protocols with little cost here.

if we remove it from express-graphql, next release will be breaking change. And we also need to make change and fix the bug in this rc as well.

anyway, just let me know the direction we eventually take, I can help with this issue as well.

@acao
Copy link
Member

acao commented Feb 13, 2021

@junminstorage do you think you could introduce a PR for this fix then? maybe collaborate with @ryan-m-walker and/or @imolorhe since i’m taking a break?

@junminstorage
Copy link
Author

Surely and happy to do so. Let me draft a PR to start the discussion with them.

@acao
Copy link
Member

acao commented Mar 29, 2021

I have a fix coming!

@acao
Copy link
Member

acao commented Mar 29, 2021

@junminstorage introduced the legacyClient option as requested in the above PR, and just made it so that only graphql-ws is tried with subscriptionsUrl since the failover logic is too complex and unnecessary, when most projects are instantiating a custom client instance anyways.

thanks for all your work @junminstorage getting this working in the ecosystem, hoping to help you get everything working nicely here and in express-graphql in the following weeks!

@junminstorage
Copy link
Author

@acao make sense. thank you!

@acao acao closed this as completed in #1819 Apr 3, 2021
acao added a commit that referenced this issue Apr 3, 2021
…script types, fixes #1800.  (#1819)

`createGraphiQLFetcher` now only attempts an `graphql-ws` connection when only `subscriptionUrl` is provided. In order to use `graphql-transport-ws`, you'll need to provide the `legacyClient` option only, and no `subscriptionUrl` or `wsClient` option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants