Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for @defer directive? #334

Open
petrgazarov opened this issue Nov 23, 2023 · 1 comment
Open

Support for @defer directive? #334

petrgazarov opened this issue Nov 23, 2023 · 1 comment

Comments

@petrgazarov
Copy link

@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.

@giulio-opal
Copy link

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
);

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

No branches or pull requests

3 participants