apollo-boost#HttpLink JavaScript Examples

The following examples show how to use apollo-boost#HttpLink. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: graphql.js    From mongodb-graphql-demo with Apache License 2.0 6 votes vote down vote up
initGraphQLClient = () => {
	hasLoggedInUser();
	if (!hasLoggedInUser()) {
		loginAnonymous();
	}

	if (hasLoggedInUser()) {
		const accessToken = getCurrentUser().auth.activeUserAuthInfo.accessToken;

		// Add an Authorization header to each GraphQL request
		const authLink = setContext((_, { headers }) => ({
			headers: {
				...headers,
				Authorization: `Bearer ${accessToken}`,
			},
		}));

		// Connect Apollo to the GraphQL Endpoint
		const GRAPHQL_URL = `https://stitch.mongodb.com/api/client/v2.0/app/${CONFIG.APP_ID}/graphql`;
		const httpLink = new HttpLink({ uri: GRAPHQL_URL });

		// Instantiate the Apollo Client
		const client = new ApolloClient({
			link: authLink.concat(httpLink),
			cache: new InMemoryCache(),
		});

		return client;
	}
}