Apollo-link: add apollo-link-local-query

Created on 17 Oct 2017  ·  10Comments  ·  Source: apollographql/apollo-link

Can we add support for https://github.com/af/apollo-local-query as well?

This is useful for server-rendering a React/Apollo app when your GraphQL server is running in the same server process. Rather than go out and back through the networking stack (eg. connecting to localhost), with this module the query runs in the same process as your rendering code.

Most helpful comment

Hi, author of apollo-local-query here, just wanted to +1 the idea of including a local Link in Apollo Client, if possible. It's a small amount of code so the maintenance burden should be minimal, and as an Apollo Client user it'd be really nice to have it fully ready for SSR out of the box.

All 10 comments

Here is my working version. I hope that it will help you

import ApolloClient from 'apollo-client';
import { ApolloLink, Observable } from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';

class LocalLink extends ApolloLink {
  constructor(schema, context, rootValue) {
    super();

    this.schema = schema;
    this.context = context;
    this.rootValue = rootValue;
  }

  request(operation) {
    const { schema, rootValue, context } = this;
    const { query, variables, operationName } = operation;

    return new Observable(observer => {
      let canceled = false;

      execute(schema, query, rootValue, context, variables, operationName)
        .then(result => {
          if (canceled) {
            return;
          }
          // we have data and can send it to back up the link chain
          observer.next(result);
          observer.complete();
          return result;
        })
        .catch(err => {
          if (canceled) {
            return;
          }

          observer.error(err);
        });

        return () => {
          canceled = true;
        };
    });
  }
}


const cache = new InMemoryCache();
const link = new LocalLink(schema, graphQLContext);
const apolloClient = new ApolloClient({
   ssrMode: true,
   cache,
   link,
});

Hi @seeden! Thanks for building this link. :grinning:

Could you please create a repo for this link (or add it to your existing one) and publish it as an npm package (maybe apollo-link-local-query)? Our apollo-link repo only contains links that we intend to help maintain.

We're still figuring out the best way to feature community links, but once we do, we would love to see apollo-link-local-query added as a featured community link!

Hi Peggy. No problem I will create new npm package and after that I will close this issue

Would be great if this was officially supported as it's pretty much required for SSR and is even mentioned in the official docs (albeit with no link to a solution): https://www.apollographql.com/docs/react/recipes/server-side-rendering.html#local-queries

I managed to get this working in my project (thanks @seeden):

https://github.com/nick/boulder-vote/blob/master/src/ApolloLinkLocal.js
https://github.com/nick/boulder-vote/blob/master/index.js#L67-L71

Will use the npm module once it's available.

Hi, author of apollo-local-query here, just wanted to +1 the idea of including a local Link in Apollo Client, if possible. It's a small amount of code so the maintenance burden should be minimal, and as an Apollo Client user it'd be really nice to have it fully ready for SSR out of the box.

Hi, I am waiting for npm module too. Will copy it in local for now. Thank you for building it.

Thanks for this!

yay!

@jbaxleyiii Thanks for this!

Was this page helpful?
0 / 5 - 0 ratings