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

How do you use version 2 of api with apollo? #61

Open
jahglow opened this issue Jan 17, 2022 · 1 comment
Open

How do you use version 2 of api with apollo? #61

jahglow opened this issue Jan 17, 2022 · 1 comment

Comments

@jahglow
Copy link

jahglow commented Jan 17, 2022

I encountered that Apollo would error on { crunched: Object, version:number } response due to its parseAndCheckHttpResponse method looking for data and errors keys and finding those above. To make things worse we have to make it friendly with batchedHttpLink, it crunches responses across all responses in the batch array. What do you use crunch v2 with?

@jgillick
Copy link

It worked for me following the instructions. Sounds like the HTTP linker is running before cruncher. Be sure that cruncher comes first in the linking concat method.

For example:

new ApolloClient({
  ssrMode: options.ssr === true,
  link: concat(uncruncher, httpLink),
  cache: new InMemoryCache({}),
});

Here a simplified example of my implementation:

Server

import { ApolloServer } from 'apollo-server-express';
import { crunch } from 'graphql-crunch';
import { schema } from './schema';

export const createGraphQLServer  = () => {
  return new ApolloServer({
      schema,
      formatResponse: (response) => {
        response.data = crunch(response.data, 2);
        return response;
      },
    });
}

Client

import { ApolloClient, InMemoryCache, ApolloLink, HttpLink, concat } from '@apollo/client';
import { uncrunch } from 'graphql-crunch';

export function apolloClient(options) {
  
  // Crunch compression expander
  const uncruncher = new ApolloLink((operation, forward) =>
    forward(operation).map((response) => {
      const { data } = response;
      if (typeof data['crunched'] !== 'undefined') {
        response.data = uncrunch(data);
      }
      return response;
    })
  );

  const httpLink = new HttpLink({
    uri: `http://localhost:3000/graphql`,
  });

  return new ApolloClient({
    link: concat(uncruncher, httpLink),
    cache: new InMemoryCache({}),
  });
}

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

No branches or pull requests

2 participants