How to use apollo-cache-inmemory - 10 common examples

To help you get started, we’ve selected a few apollo-cache-inmemory examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tomitrescak / apollo-mobx / src / testing / index.tsx View on Github external
loadingComponent
}: ApolloProps) {
  // add crap around mocks
  const finalMocks = {
    ...resolvers,
    Mutation: () => mutations || {},
    Query: () => queries || {},
  };

  const schema = makeExecutableSchema({ typeDefs, resolvers: global.globalResolvers });
  addMockFunctionsToSchema({
    mocks: finalMocks,
    schema,
  });

  const apolloCache = new InMemoryCache(global.__APOLLO_STATE_);

  const spyLink = new SpyLink();
  const graphqlClient: ApolloClient = new ApolloClient({
    cache: apolloCache as any,
    context,
    link: ApolloLink.from([
      spyLink,
      new MockLink({ schema }),
    ]),
    loadingComponent
  });
  graphqlClient.spyLink = spyLink;

  return graphqlClient;
}
github orbiting / publikator-frontend / lib / apollo / initApollo.js View on Github external
? ApolloLink.split(
      hasSubscriptionOperation,
      new WebSocketLink({
        uri: API_WS_URL,
        options: {
          reconnect: true,
          timeout: 50000
        }
      }),
      http
    )
    : http

  return new ApolloClient({
    connectToDevTools: process.browser,
    cache: new InMemoryCache({
      fragmentMatcher,
      dataIdFromObject
    }).restore(initialState || {}),
    ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    link
  })
}
github hasura / graphql-engine / community / sample-apps / react-apollo-todo / src / apollo.js View on Github external
);

  // chose the link to use based on operation
  const link = split(
    // split based on operation type
    ({ query }) => {
      const { kind, operation } = getMainDefinition(query);
      return kind === "OperationDefinition" && operation === "subscription";
    },
    wsLink,
    httpLink
  );

  const client = new ApolloClient({
    link: link,
    cache: new InMemoryCache({
      addTypename: true
    })
  });

  return client;
};
github kyma-project / console / logging / src / app / app.module.ts View on Github external
},
    });

    const link = split(
      // split based on operation type
      ({ query }) => {
        const definition = getMainDefinition(query);
        return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
      },
      ws,
      http,
    );

    apollo.create({
      link,
      cache: new InMemoryCache(),
    });
  }
}
github okgrow / advanced-graphql / web / src / client.js View on Github external
// http://localhost:8080 is put behind a proxy by webpack (cors)
const uri = '/graphql';

const link = ApolloLink.from([
  errorLink,
  authLink.split(
    hasSubscriptionOperation,
    new WsLink({
      uri: 'ws://localhost:8081/graphql',
      options: { reconnect: true },
    }),
    new HttpLink({ uri })
  ),
]);

const cache = new Cache();

const client = new ApolloClient({
  link,
  cache: cache.restore(window.__APOLLO_STATE__ || {}),
});

export default () => (
  
    
      
    
  
);
github ammezie / techies / src / main.js View on Github external
// get the authentication token from localstorage if it exists
  const token = localStorage.getItem('USER_TOKEN')

  // return the headers to the context so httpLink can read them
  return {
    headers: {
      ...headers,
      Authorization: token ? `Bearer ${token}` : ''
    }
  }
})

// Create the apollo client
const apolloClient = new ApolloClient({
  link: httpLinkAuth.concat(httpLink),
  cache: new InMemoryCache()
})

// Install the vue plugin
Vue.use(VueApollo)

const apolloProvider = new VueApollo({
  defaultClient: apolloClient
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    localStorage.getItem('USER_TOKEN') ? next() : next('/login')
  } else {
    next()
  }
})
github taskcluster / taskcluster / ui / src / App / index.jsx View on Github external
dataIdFromObject: object => {
      switch (object.__typename) {
        case 'TaskStatus': {
          const taskId = object.taskId || null;

          return taskId
            ? `${object.taskId}-${object.__typename}`
            : defaultDataIdFromObject(object);
        }

        default: {
          // fall back to default handling
          return defaultDataIdFromObject(object);
        }
      }
    },
    /* eslint-enable no-underscore-dangle */
github mirumee / saleor-dashboard / src / index.tsx View on Github external
dataIdFromObject: (obj: any) => {
      // We need to set manually shop's ID, since it is singleton and
      // API does not return its ID
      if (obj.__typename === "Shop") {
        return "shop";
      }
      return defaultDataIdFromObject(obj);
    }
  }),
github birkir / kvikmyndr-app / src / store / index.js View on Github external
import { ApolloProvider } from 'react-apollo';
import config from '../config';
import UI from './UI';
import Auth from './Auth';

if (__DEV__) { // eslint-disable-line
  const nativeXMLHttpRequest = XMLHttpRequest; // eslint-disable-line
  const devXMLHttpRequest = GLOBAL.originalXMLHttpRequest ? GLOBAL.originalXMLHttpRequest : GLOBAL.XMLHttpRequest; // eslint-disable-line
  GLOBAL.XMLHttpRequest = devXMLHttpRequest;
}

// Create new Apollo client
const link = new Link({
  uri: config.GRAPHQL_ENDPOINT,
});
const cache = new InMemoryCache();
const client = new ApolloClient({
  link,
  cache,
});


export default class Store {
  client = client;
  ui = new UI();
  auth = new Auth();
  
  async setup() {
    return true;
  }
}
github okgrow / advanced-graphql / web / src / server.js View on Github external
app.use(async (req, res) => {
  const context = {};
  const sheet = new ServerStyleSheet();

  try {
    const cache = new Cache();

    const client = new ApolloClient({
      link: new HttpLink({
        uri: `${API_HOST}/graphql`,
      }),
      cache,
    });

    // to be used by react-apollo
    const component = (