apollo-upload-client icon indicating copy to clipboard operation
apollo-upload-client copied to clipboard

Support for @defer directive?

Open petrgazarov opened this issue 2 years ago • 1 comments

@apollo/client supports @defer directive since version 3.7.0:

https://www.apollographql.com/docs/react/data/defer/

But I'm not able to use this library with @defer. It throws a JSON parsing error on this line. Are there plans to support it?

Since this library already calls parseAndCheckHttpResponse from @apollo/client, I think it can be easily fixed by doing it conditionally like in createHttpLink.ts - @apollo/client exports readMultipartBody function which it uses for multipart/mixed responses.

petrgazarov avatar Nov 23 '23 03:11 petrgazarov

FWIW, I solved this by creating a splitLink between the regular apollo link and the upload link:

// httpLink is used for defer and subscription queries
let httpLink = createHttpLink();

// uploadLink is used for all other queries. Eventually we want to use to use
// this only for upload queries, but for now we don't want to switch fully to
// the regular Apollo Link yet.
let uploadLink = createUploadLink();

const splitLink = split(
  ({ query }) => {
    // does not match custom directives beginning with @defer
    const hasDefer = hasDirectives(["defer"], query);
    const definition = getMainDefinition(query);
    return (
      (definition.kind === "OperationDefinition" &&
        definition.operation === "subscription") ||
      hasDefer
    );
  },
  httpLink,
  uploadLink
);

giulio-opal avatar Oct 30 '24 23:10 giulio-opal