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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken persisted queries support #58

Closed
silvercrown07 opened this issue May 14, 2023 · 2 comments
Closed

Broken persisted queries support #58

silvercrown07 opened this issue May 14, 2023 · 2 comments
Labels
bug Something isn't working released Has been released and published

Comments

@silvercrown07
Copy link

Hi @enisdenjo, first of all, thank you for maintaining such an awesome project!

I would like to report an issue related to persisted queries support.

As described in the recipes:

// 馃浉 server
 
import { parse, ExecutionArgs } from 'graphql';
import { createHandler } from 'graphql-sse';
import { schema } from './my-graphql';
 
// a unique GraphQL execution ID used for representing
// a query in the persisted queries store. when subscribing
// you should use the `SubscriptionPayload.query` to transmit the id
type QueryID = string;
 
const queriesStore: Record<QueryID, ExecutionArgs> = {
  iWantTheGreetings: {
    schema, // you may even provide different schemas in the queries store
    document: parse('subscription Greetings { greetings }'),
  },
};
 
export const handler = createHandler({
  onSubscribe: (_req, params) => {
    const persistedQuery =
      queriesStore[String(params.extensions?.persistedQuery)];
    if (persistedQuery) {
      return {
        ...persistedQuery,
        variableValues: params.variables, // use the variables from the client
        contextValue: undefined,
      };
    }
 
    // for extra security only allow the queries from the store
    return [null, { status: 404, statusText: 'Not Found' }];
  },
});

onSubscribe would return a ExecutionArgs result, leading graphql to execute the expected resolver.

However in current implementation, the returned result from above example is treated as ExecutionResult, hence the resolver is not triggered.

graphql-sse/src/handler.ts

Lines 587 to 600 in d4890f1

else if (
isExecutionResult(onSubscribeResult) ||
isAsyncIterable(onSubscribeResult)
)
return {
// even if the result is already available, use
// context because onNext and onComplete needs it
ctx: (typeof context === 'function'
? await context(req, params)
: context) as Context,
perform() {
return onSubscribeResult;
},
};

graphql-sse/src/handler.ts

Lines 1107 to 1110 in d4890f1

function isExecutionResult(val: unknown): val is ExecutionResult {
// TODO: comprehensive check
return isObject(val);
}

Please let me know if I missed anything, and if more details are needed, thank you!

@enisdenjo
Copy link
Owner

Hey there, thanks for reporting! I think I know where the problem is, fix coming soon...

@enisdenjo enisdenjo added the bug Something isn't working label May 15, 2023
enisdenjo pushed a commit that referenced this issue May 15, 2023
## [2.1.3](v2.1.2...v2.1.3) (2023-05-15)

### Bug Fixes

* **client:** Respect retry attempts when server goes away after connecting in single connection mode ([#59](#59)) ([e895c5b](e895c5b)), closes [#55](#55)
* **handler:** Detect `ExecutionArgs` in `onSubscribe` return value ([a16b921](a16b921)), closes [#58](#58)
@enisdenjo
Copy link
Owner

馃帀 This issue has been resolved in version 2.1.3 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

@enisdenjo enisdenjo added the released Has been released and published label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released Has been released and published
Projects
None yet
Development

No branches or pull requests

2 participants