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

TypedDocumentNode not working with useSubscription #148

Closed
robertgirschick opened this issue Jan 21, 2022 · 1 comment
Closed

TypedDocumentNode not working with useSubscription #148

robertgirschick opened this issue Jan 21, 2022 · 1 comment

Comments

@robertgirschick
Copy link

I really like the possibility to use TypedDocumentNode from the GraphQL Code Generator in queries. But unfortunately, it doesn't seem to work as presented here in the docs (at least not with useSubscription which is what I need):

https://villus.logaretm.com/guide/typescript-codgen#using-typed-document

The problem seems to be that the normalizeQuery call comes too late in execute<TData, TVars>. It is called only in the getQueryKey function, but the normalized query value (using print from the graphql package) is needed in the first ...operation property (= query and values) already.

private async execute<TData, TVars>(

 const context: ClientPluginContext = {
      useResult(pluginResult, terminate) {
        if (terminate) {
          terminateSignal = true;
        }

        result = pluginResult as OperationResult<TData>;
      },
      afterQuery(cb) {
        afterQuery.push(cb);
      },
      operation: {
        ...operation,
        key: getQueryKey(operation),
        type,
        cachePolicy:
          ('cachePolicy' in operation ? operation.cachePolicy : this.defaultCachePolicy) || this.defaultCachePolicy,
      },
      opContext,
    };

I tested to insert

operation.query = normalizeQuery(operation.query);

somewhere before creating const context. Then everything works. Of course, you wouldn't need to call normalizeQuery again in getQueryKey then, but simply pass along the value from above.

@robertgirschick
Copy link
Author

Turns out my solution above was wrong. The issue was rather that the provided variables were not passed along in the message.

I simply had to insert variables: operation.variables in the subscriptionForwarder for graphql-ws:

const subscriptionForwarder = operation => {
  return {
    subscribe: obs => {
      wsClient.subscribe(
        {
          query: operation.query,
          variables: operation.variables,
        },
        obs
      );
    },
  };
};

Would be great if this could be mentioned in the docs here:

https://villus.logaretm.com/guide/subscriptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant