apollo-boost#ApolloClient JavaScript Examples

The following examples show how to use apollo-boost#ApolloClient. 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: render.js    From tunnel-tool with MIT License 7 votes vote down vote up
configureGraph = ({ url, req }) => ({
  graph: new ApolloClient({
    ssrMode: true,
    link: new HttpLink({
      uri: url,
      onError: e => {
        console.log("APOLLO_CLIENT_ERROR");
        console.log(e.graphQLErrors);
      },
      credentials: "include",
      fetch: fetch,
      headers: {
        cookie: req.header("Cookie")
      }
    }),
    cache: new InMemoryCache()
  })
})
Example #2
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;
	}
}
Example #3
Source File: auth.js    From React-Messenger-App with MIT License 5 votes vote down vote up
function create(initialState, { getToken }) {
  const httpLink = createHttpLink({
    uri: "http://localhost:4000"
    //credentials: "same-origin"
  });

  const authLink = setContext((_, { headers }) => {
    const token = getToken();
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : ""
      }
    };
  });

  let link = authLink.concat(httpLink);

  if (process.browser) {
    const token = getToken();
    const wsLink = new WebSocketLink({
      uri: `ws://localhost:4000`,
      options: {
        reconnect: true,
        connectionParams: {
          Authorization: `Bearer ${token}`
        }
      }
    });

    link = split(
      // split based on operation type
      ({ query }) => {
        const { kind, operation } = getMainDefinition(query);
        return kind === "OperationDefinition" && operation === "subscription";
      },
      wsLink,
      authLink.concat(httpLink)
    );
  }

  // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
  return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    link,
    cache: new InMemoryCache().restore(initialState || {})
  });
}
Example #4
Source File: App.jsx    From web-frontend with MIT License 5 votes vote down vote up
createApolloClient = (cache = {}) =>
  new ApolloClient({
    ssrMode: typeof window !== "undefined",
    cache: new InMemoryCache().restore(cache),
    link: createUploadLink({ uri: "http://77.228.91.193:4000/graphql" }),
  })
Example #5
Source File: render.js    From tunnel-tool with MIT License 5 votes vote down vote up
configureGraph = ({ url, initState }) => {
  return new ApolloClient({
    cache: new InMemoryCache().restore(window.__APOLLO_STATE__),
    link: new HttpLink({ uri: url })
  });
}