How to use the apollo-link.concat function in apollo-link

To help you get started, we’ve selected a few apollo-link 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 gsans / todo-apollo-v2 / src / app / graphql.module.ts View on Github external
})
    );

    const authMiddleware = new ApolloLink((operation, forward) => {
      operation.setContext({
        headers: { 'Authorization': localStorage.getItem('Auth0IdToken') || '' }
      });
      return forward(operation);
    });
    
    const link = ApolloLink.split(
      this.isSubscription,
      /* if true use  */ websocket,
      /* if false use */ http,
    );
    return concat(authMiddleware, link);
  }
github vnovick / graphql-jwt-tutorial / nextjsapp / pages / app.js View on Github external
})

 const authMiddleware = new ApolloLink((operation, forward)=> {
  if (appJWTToken) {
    operation.setContext({
      headers: {
        Authorization: `Bearer ${appJWTToken}`
      }
    });
  } 
  return forward(operation);
 })


const apolloClient = new ApolloClient({
  link: logoutLink.concat(concat(authMiddleware, httpLink)),
  cache: new InMemoryCache(),
});


const App = ({ accessToken }) => {
  let previousToken = appJWTToken;
  appJWTToken = accessToken.token
  return (
    
      
        
          {({loading, error, data, refetch}) => {
            // refetch if token has changed
            if (!previousToken && (previousToken !== appJWTToken)) {
              refetch()
            }
github cult-of-coders / apollo / client / index.js View on Github external
if (config.uri) {
    config.httpLinkOptions.uri = config.uri;
  } else {
    // Allow GRAPHQL_ENDPOINT to be changed
    config.httpLinkOptions.uri = config.httpLinkOptions.uri
      ? config.httpLinkOptions.uri
      : GRAPHQL_ENDPOINT;
  }

  const uploadLink = createUploadLink(config.httpLinkOptions);

  // We define the HTTP Link
  const httpLink = createHttpLink(config.httpLinkOptions);

  if (!config.disableMeteorAccounts) {
    terminatingLink = ApolloLink.concat(meteorAccountsLink, uploadLink, httpLink);
  } else {
    terminatingLink = ApolloLink.concat(uploadLink, httpLink);
  }

  // A chance to add change the links
  terminatingLink = Config.getLink(terminatingLink);

  if (!config.disableWebsockets) {
    wsLink = new WebSocketLink({
      uri: config.httpLinkOptions.uri.replace(/http/, 'ws'),
      options: {
        reconnect: true,
        connectionParams: () => ({
          [AUTH_TOKEN_KEY]: localStorage.getItem('Meteor.loginToken'),
        }),
      },
github kidunot89 / wp-graphql-composer / dist / provider.js View on Github external
export var createClient = function createClient(httpLink) {
  // Add the authorization to the headers
  var authMiddleware = new ApolloLink(function (operation, forward) {
    operation.setContext({
      headers: {
        authorization: localStorage.getItem('user-token') || null
      }
    });
    return forward(operation);
  });
  return new ApolloClient({
    link: concat(authMiddleware, httpLink),
    cache: new InMemoryCache({
      dataIdFromObject: function dataIdFromObject(object) {
        return object.id;
      },
      fragmentMatcher: fragmentMatcher
    }),
    clientState: {}
  });
};
export var WPProvider = function WPProvider(_ref) {
github hasura / graphql-engine / community / sample-apps / nextjs-8-serverless / with-apollo-jwt / app / lib / init-apollo.js View on Github external
operation.setContext({
      headers: {
        authorization: getToken(),
      }
    })

    return forward(operation);
  })
  const httpLink = new HttpLink({
      uri: 'https://my-app.herokuapp.com/v1/graphql', // Server URL (must be absolute)
      credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
  })
  return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    link: concat(authMiddleware, httpLink),
    cache: new InMemoryCache().restore(initialState || {})
  })
}
github benjaminadk / youtube-clone / client / src / index.js View on Github external
const wsLink = new WebSocketLink({
  uri: ws,
  options: {
    reconnect: true
  }
})
const authMiddleware = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => ({
    headers: {
      ...headers,
      authorization: localStorage.getItem('TOKEN') || null
    }
  }))
  return forward(operation)
})
const linkWithMiddleware = concat(authMiddleware, httpLink)
const link = split(
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query)
    return kind === 'OperationDefinition' && operation === 'subscription'
  },
  wsLink,
  linkWithMiddleware
)
const cache = new InMemoryCache()
const client = new ApolloClient({
  link,
  cache
})

const root = document.getElementById('root')
github zencrepes / zencrepes / imports / ui / services / ApolloProviderGithub.js View on Github external
operation.setContext({
        headers: {
            authorization: token ? `Bearer ${token}` : "",
        }
    });
    return forward(operation).map(response => {
        if (response.errors !== undefined && response.errors.length > 0) {
            response.data.errors = response.errors;
        }
        return response;
    });
});


const client = new ApolloClient({
    link: concat(authMiddleware, httpLink),
    //link: authLink.concat(link),
    cache: cache,
});

/*
export default (
    ({ children }) => (
        {children}
    )
);

*/

class ApolloProviderGithub extends Component {
    constructor(props) {
        super(props);
github tsevdos / repocompare / app / lib / apolloClient.js View on Github external
const httpLink = createHttpLink({ uri: "https://api.github.com/graphql" });

const authMiddleware = new ApolloLink((operation, forward) => {
  operation.setContext({
    headers: {
      authorization: localStorage.getItem("token")
        ? `Bearer ${localStorage.getItem("token")}`
        : null
    }
  });

  return forward(operation);
});

const apolloClient = new ApolloClient({
  link: concat(authMiddleware, httpLink),
  cache: new InMemoryCache()
});

export default apolloClient;
github Canner / apollo-link-firebase / src / index.ts View on Github external
export const createRtdbLink = ({database}: {database: firebaseDatabase.Database}) => {
  return concat(new RtdbQueryLink({database}), new RtdbSubLink({database}));
};
github aerogear / offix / packages / sync / src / links / compositeQueueLink.ts View on Github external
export const compositeQueueLink = (config: DataSyncConfig, filter?: TYPE_MUTATION): ApolloLink => {
  const offlineLink = new OfflineQueueLink(config, filter);
  offlineLink.openQueueOnNetworkStateUpdates();
  const localLink = new LocalDirectiveFilterLink();
  return concat(offlineLink, localLink);
};