How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • import { ApolloLink } from '@apollo/client';
    
    const reportErrors = (errorCallback) => new ApolloLink((operation, forward) => {
      const observable = forward(operation);
      // errors will be sent to the errorCallback
      observable.subscribe({ error: errorCallback })
      return observable;
    });
    
    const link = reportErrors(console.error);
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloLink</span> <span class="token keyword">import</span> ApolloLink</code></pre>
    Docs
    1
  • import { ApolloLink } from '@apollo/client';
    
    const authLink = new ApolloLink((operation, forward) => {
      operation.setContext(({ headers }) => ({ headers: {
        authorization: Auth.userId(), // however you get your token
        ...headers
      }}));
      return forward(operation);
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloLink</span> <span class="token keyword">import</span> ApolloLink</code></pre>
    Docs
    1
  • import {
      ApolloClient,
      InMemoryCache,
      HttpLink,
      ApolloLink
    } from "@apollo/client";
    import { asyncMap } from "@apollo/client/utilities";
    
    import { usdToEur } from './currency';
    
    const httpLink = new HttpLink({ uri: '/graphql' });
    
    const usdToEurLink = new ApolloLink((operation, forward) => {
      return asyncMap(forward(operation), async (response) => {
        let data = response.data;
        if (data.price && data.currency === "USD") {
          data.price = await usdToEur(data.price);
          data.currency = "EUR";
        }
        return response;
      });
    });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: usdToEurLink.concat(httpLink)
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloClient<span class="token operator">&lt;</span>TCacheShape<span class="token operator">></span></span> <span class="token keyword">import</span> ApolloClient</code></pre>
    Docs
    1
  • import { ApolloLink } from '@apollo/client';
    
    const roundTripLink = new ApolloLink((operation, forward) => {
      // Called before operation is sent to server
      operation.setContext({ start: new Date() });
    
      return forward(operation).map((data) => {
        // Called after server responds
        const time = new Date() - operation.getContext().start;
        console.log(`Operation ${operation.operationName} took ${time} to complete`);
        return data;
      });
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloLink</span> <span class="token keyword">import</span> ApolloLink</code></pre>
    Docs
    0
  • import { ApolloLink } from '@apollo/client';
    
    const timeStartLink = new ApolloLink((operation, forward) => {
      operation.setContext({ start: new Date() });
      return forward(operation);
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloLink</span> <span class="token keyword">import</span> ApolloLink</code></pre>
    Docs
    0
  • const httpLink = createHttpLink({ uri: "server.com/graphql" });
    const restLink = new RestLink({ uri: "api.server.com" });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: ApolloLink.from([authLink, restLink, errorLink, retryLink, httpLink])
      // Note: httpLink is terminating so must be last, while retry & error wrap
      // the links to their right. State & context links should happen before (to
      // the left of) restLink.
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> httpLink<span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    Docs
    0
  • const authRestLink = new ApolloLink((operation, forward) => {
      operation.setContext(({headers}) => {
        const token = localStorage.getItem("token");
        return {
          headers: {
            ...headers,
            Accept: "application/json",
            Authorization: token
          }
        };
      });
      return forward(operation).map(result => {
        const { restResponses } = operation.getContext();
        const authTokenResponse = restResponses.find(res => res.headers.has("Authorization"));
        // You might also filter on res.url to find the response of a specific API call
        if (authTokenResponse) {
          localStorage.setItem("token", authTokenResponse.headers.get("Authorization"));
        }
        return result;
      });
    });
    
    const restLink = new RestLink({ uri: "uri" });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: ApolloLink.from([authRestLink, restLink])
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> authRestLink<span class="token operator">:</span> <span class="token decorator"><span class="token at operator">@</span><span class="token function">apollo</span></span><span class="token operator">/</span>client<span class="token punctuation">.</span>ApolloLink</code></pre>
    Docs
    0
  • import {
      ApolloClient,
      InMemoryCache,
      HttpLink,
      ApolloLink
    } from "@apollo/client";
    import { asyncMap } from "@apollo/client/utilities";
    
    import { usdToEur } from './currency';
    
    const httpLink = new HttpLink({ uri: '/graphql' });
    
    const usdToEurLink = new ApolloLink((operation, forward) => {
      return asyncMap(forward(operation), async (response) => {
        let data = response.data;
        if (data.price && data.currency === "USD") {
          data.price = await usdToEur(data.price);
          data.currency = "EUR";
        }
        return response;
      });
    });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: usdToEurLink.concat(httpLink)
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloClient<span class="token operator">&lt;</span>TCacheShape<span class="token operator">></span></span> <span class="token keyword">import</span> ApolloClient</code></pre>
    Docs
    1
  • import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client';
    
    const httpLink = new HttpLink({ uri: '/graphql' });
    
    const addDateLink = new ApolloLink((operation, forward) => {
      return forward(operation).map(response => {
        response.data.date = new Date();
        return response;
      });
    });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: addDateLink.concat(httpLink),
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloClient<span class="token operator">&lt;</span>TCacheShape<span class="token operator">></span></span> <span class="token keyword">import</span> ApolloClient</code></pre>
    Docs
    0
  • 
    
    const client = new ApolloClient({
      link: ApolloLink.from([
        onError(({ graphQLErrors, networkError }) => {
          if (graphQLErrors) {
            graphQLErrors.map(({ message, locations, path, extensions }) => {
              if(extensions.code === 'UNAUTHENTICATED') {
                localStorage.removeItem('jwt');
                client.resetStore();
              }
              console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
            });
            if (networkError) {
              console.log(`[Network error]: ${networkError}`);
            }
          }
        }),
        AuthLink,
        link
      ]),
      cache: new InMemoryCache().restore(window.__APOLLO_STATE__)
    });
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> client<span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    /PacktPublishing/Hands-on-Full-Stack-Web-Development-with-GraphQL-and-React MIT
    0
  • 
    
    const client = new ApolloClient({
      link: ApolloLink.from([
        onError(({ graphQLErrors, networkError }) => {
          if (graphQLErrors) {
            graphQLErrors.map(({ message, locations, path, extensions }) => {
              if(extensions.code === 'UNAUTHENTICATED') {
                localStorage.removeItem('jwt');
                client.resetStore();
              }
              console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
            });
            if (networkError) {
              console.log(`[Network error]: ${networkError}`);
            }
          }
        }),
        AuthLink,
        createUploadLink({
          uri: 'http://localhost:8000/graphql',
          credentials: 'same-origin',
        }),
      ]),
      cache: new InMemoryCache().restore(window.__APOLLO_STATE__)
    });
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> client<span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    /PacktPublishing/Hands-on-Full-Stack-Web-Development-with-GraphQL-and-React MIT
    0
  • 
    
    const client = new ApolloClient({
      link: ApolloLink.from([
        onError(({ graphQLErrors, networkError }) => {
          if (graphQLErrors) {
            graphQLErrors.map(({ message, locations, path, extensions }) => {
              if(extensions.code === 'UNAUTHENTICATED') {
                localStorage.removeItem('jwt');
                client.resetStore();
              }
              console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
            });
            if (networkError) {
              console.log(`[Network error]: ${networkError}`);
            }
          }
        }),
        InfoLink,
        AuthLink,
        link
      ]),
      cache: new InMemoryCache().restore(window.__APOLLO_STATE__)
    });
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> client<span class="token operator">:</span> <span class="token builtin">any</span></code></pre>
    /PacktPublishing/Hands-on-Full-Stack-Web-Development-with-GraphQL-and-React MIT
    0
  • import {
      ApolloClient,
      InMemoryCache,
      HttpLink,
      ApolloLink
    } from "@apollo/client";
    import { asyncMap } from "@apollo/client/utilities";
    
    import { usdToEur } from './currency';
    
    const httpLink = new HttpLink({ uri: '/graphql' });
    
    const usdToEurLink = new ApolloLink((operation, forward) => {
      return asyncMap(forward(operation), async (response) => {
        let data = response.data;
        if (data.price && data.currency === "USD") {
          data.price = await usdToEur(data.price);
          data.currency = "EUR";
        }
        return response;
      });
    });
    
    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: usdToEurLink.concat(httpLink)
    });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloClient<span class="token operator">&lt;</span>TCacheShape<span class="token operator">></span></span> <span class="token keyword">import</span> ApolloClient</code></pre>
    Docs
    1
  • import { ApolloLink, HttpLink } from '@apollo/client';
    import { RetryLink } from '@apollo/client/link/retry';
    
    const directionalLink = new RetryLink().split(
      (operation) => operation.getContext().version === 1,
      new HttpLink({ uri: "http://localhost:4000/v1/graphql" }),
      new HttpLink({ uri: "http://localhost:4000/v2/graphql" })
    );
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">ApolloLink</span> <span class="token keyword">import</span> ApolloLink</code></pre>
    Docs
    0
  • const customFetch = (uri, options) => {
      const { header } = Hawk.client.header(
        "http://example.com:8000/resource/1?b=1&a=2",
        "POST",
        { credentials: credentials, ext: "some-app-data" }
      );
      options.headers.Authorization = header;
      return fetch(uri, options);
    };
    
    const link = new HttpLink({ fetch: customFetch });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> <span class="token function-variable function">customFetch</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">uri<span class="token operator">:</span> any<span class="token punctuation">,</span> options<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span>Response<span class="token operator">></span></code></pre>
    Docs
    0
  • import fetch from 'cross-fetch';
    import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
    
    export const client = new ApolloClient({
      link: new HttpLink({
        uri: '.netlify/functions/graphql_mongodb',
        fetch,
      }),
      cache: new InMemoryCache()
    });
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">import</span> fetch</code></pre>
    /panacloud-modern-global-apps/jamstack-serverless mit
    0
  • import { HttpLink } from "@apollo/client";
    
    const link = new HttpLink({ uri: "/graphql" });
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">class</span> <span class="token class-name">HttpLink</span> <span class="token keyword">import</span> HttpLink</code></pre>
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples