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

Persistent Queries #60

Open
ntelkedzhiev opened this issue Apr 22, 2019 · 7 comments
Open

Persistent Queries #60

ntelkedzhiev opened this issue Apr 22, 2019 · 7 comments

Comments

@ntelkedzhiev
Copy link
Contributor

ntelkedzhiev commented Apr 22, 2019

Any plans of supporting persistent queries?

https://facebook.github.io/relay/docs/en/persisted-queries.html

@nodkz
Copy link
Collaborator

nodkz commented Apr 22, 2019

We are currently using Apollo.
So need help from the community for the implementation of this feature.

@maraisr
Copy link
Member

maraisr commented Jul 31, 2019

@nodkz are you using Apollo for network layer, but relay as the runtime?

@ntelkedzhiev I'll be happy to take a look at this. ⚡️

@rramaa
Copy link
Contributor

rramaa commented Oct 1, 2020

I have written a middleware to send persisted queries to gql server. Would be happy contribute. Would be cool if the persisted queries middleware is supported out of the box

@felippepuhle
Copy link
Member

@rramaa can you open a PR when you get some free time?

@rramaa
Copy link
Contributor

rramaa commented Oct 5, 2020

Yeah...will open a PR as soon as I get some free time...been a bit busy last couple of weeks

@limaAniceto
Copy link
Contributor

a middleware to send persisted queries to gql server. Would be happy contribute. Would be cool if the persisted queries middleware is supported out of the box

Hey @rramaa,
Could you please share here that middleware for the community?

Appreciate the help 🙏

@limaAniceto
Copy link
Contributor

limaAniceto commented Sep 7, 2021

Just for reference we are using react-relay-network-modern and ended up going with this middleware (with some other tweaks but this should be clear enough )

Just sharing it to save someone else's time

UPDATE: This catch fallback needs improvements, we've iterated on it and we'll post it on a new comment below in the coming days - Please see the PR associated with this issue

// @flow
import { RelayNetworkLayerRequestBatch } from 'react-relay-network-modern';

type PersistedQueriesMiddlewareOptions = { hash: string };

const persistedQueriesMiddleware = (
    options: PersistedQueriesMiddlewareOptions = {}
) => (next: Function) => async (req: Object) => {
    try {
        if (req instanceof RelayNetworkLayerRequestBatch) {
            throw new Error(
                'Batched requests are not supported by current version.'
            );
        }
        const { cacheID: queryId, text: queryText } = req.operation;

        if (!queryId && (!options.hash || !queryText)) {
            throw new Error(
                'Either query id or hashing function & query must be defined!'
            );
        }

        const body = JSON.parse(req.fetchOpts.body);
        delete body.query;
        body.doc_id = queryId;
        req.fetchOpts.body = JSON.stringify(body);
        delete req.operation.text;

        const res = await next(req);

        return res;
    } catch (Error) {
        return await next(req);
    }
};

export default persistedQueriesMiddleware;

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

No branches or pull requests

6 participants