logaretm / villus

🏎 A tiny and fast GraphQL client for Vue.js

Home Page:https://villus.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Query normalization with `useSubscription`

imjasonmiller opened this issue · comments

Hey, great library!

Perhaps I am holding it wrong, but I was caught by surprise by the documentation mentioning that queries parsed with gql from graphql-tag can be passed to useQuery, while this seems to work a bit different for useSubscription.

While the API reference states a DocumentNode can be passed as well, I received that exact DocumentNode within the subscriptionForwarder by setting it up as described by the guide. It looks like that will result in an error for graphql-ws's client as it takes a string and no normalization is performed, unlike useQuery.

Is this intended behavior or would it be possible to apply the normalization to query for useSubscription as well and/or expose the normalizeQuery function so it can be easily used within the subscriptionForwarder as well?

I'd gladly open a pull request, if so.

Thanks!

This is indeed a bug. I think it could be fixed inside handleSubscription.ts

something like this:

    const normalizedQuery = normalizeQuery(operation.query);
    if (!normalizedQuery) {
      throw new Error('A query must be provided.');
    }

    useResult(forward({ ...operation, query: normalizedQuery }) as any, true);

Feel free to PR, otherwise, I will PR it tomorrow since it looks like a serious bug for non apollo-server users and breaks compact with other protocol libs.

EDIT: Really wanted you to have a go at this but I think this is a bug that needs to be fixed ASAP, many thanks for pointing it out and love the way you explained it. Thanks again!