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

feat: configurable persisted queries with graphql-sse #3735

Open
hrasoa opened this issue Feb 6, 2025 · 1 comment
Open

feat: configurable persisted queries with graphql-sse #3735

hrasoa opened this issue Feb 6, 2025 · 1 comment

Comments

@hrasoa
Copy link

hrasoa commented Feb 6, 2025

Is your feature request related to a problem? Please describe.

I use persisted queries, but it's not working with graphql-sse as it expects a query params.

Describe the solution you'd like

Add an optional configuration (like the configuration for usePersistedOperations) to get the document from the persisted queries json.

Describe alternatives you've considered

Additional context

@hrasoa hrasoa changed the title Able to configure persisted queries with graphql-sse feat: able to configure persisted queries with graphql-sse Feb 6, 2025
@hrasoa hrasoa changed the title feat: able to configure persisted queries with graphql-sse feat: configurable persisted queries with graphql-sse Feb 6, 2025
@hrasoa
Copy link
Author

hrasoa commented Feb 7, 2025

Here's what I did, it's basically a copy of usePersistedOperations

relay client:

function subscribe(operation: RequestParameters, variables: Variables) {
  return Observable.create<GraphQLResponse>(sink => {
    if (!operation.id) {
      return sink.error(new Error('Operation id cannot be empty'));
    }
    return subscriptionsClient.subscribe(
      {
        query: operation.id,
        operationName: operation.name,
        variables,
      },
      sink
    );
  });
}

server:

plugins: [
    useGraphQLSSE({
      extractPersistedOperationId(params) {
        return params.query;
      },
      getPersistedOperation(key) {
        // store is the peristed operation json
        return store[key];
      },
    }),
]
export function useGraphQLSSE(options = {}) {
    const { endpoint = '/graphql/stream', getPersistedOperation, extractPersistedOperationId, ...handlerOptions } = options;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const ctxForReq = new WeakMap();
    let handler;
    return {
        onYogaInit({ yoga }) {
            handler = createHandler({
                ...handlerOptions,
                async onSubscribe(req, _params) {
                    const persistedOperationKey = extractPersistedOperationId(_params);
                    const persistedQuery = await getPersistedOperation(
                      persistedOperationKey,
                      req,
                    );
                    const params = {
                      query: persistedQuery,
                      operationName: _params.operationName,
                      variables: _params.variables,
                      extensions: _params.extensions,
                    };
                    const enveloped = yoga.getEnveloped({
                        ...ctxForReq.get(req.raw),
                        request: req.raw,
                        params,
                    });

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