apollo-link-http#createHttpLink JavaScript Examples

The following examples show how to use apollo-link-http#createHttpLink. 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: apollo-client.js    From graphql-sample-apps with Apache License 2.0 6 votes vote down vote up
createApolloClient = (token) => {
    const httpLink = createHttpLink({
      uri: process.env.REACT_APP_GRAPHQL_ENDPOINT || config["REACT_APP_GRAPHQL_ENDPOINT"],
      options: {
        reconnect: true,
      },
    });

    const authLink = setContext((_, { headers }) => {
      // return the headers to the context so httpLink can read them
      return {
        headers: {
          ...headers,
          "X-Auth-Token": token,
        },
      };
    });

    return new ApolloClient({
      link: authLink.concat(httpLink),
      cache: new InMemoryCache()
    });
  }
Example #2
Source File: ApolloConfig.js    From graphql-sample-apps with Apache License 2.0 6 votes vote down vote up
function createApolloClient(getIdTokenClaims) {
  const GRAPHQL_ENDPOINT = process.env.REACT_APP_GRAPHQL_ENDPOINT;

  if (getIdTokenClaims == null) {
    return new ApolloClient({
      uri: GRAPHQL_ENDPOINT,
      cache: new InMemoryCache(),
    });
  }

  const httpLink = createHttpLink({
    uri: GRAPHQL_ENDPOINT,
  });

  const authLink = setContext(async (request, {headers}) => {
    const idTokenClaims = await getIdTokenClaims();
    // return the header to the context so httpLink can read them
    return {
      headers: {
        ...headers,
        'X-Auth-Token': idTokenClaims.__raw,
      },
    };
  });

  return new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache(),
  });
}
Example #3
Source File: App.js    From graphql-sample-apps with Apache License 2.0 6 votes vote down vote up
createApolloClient = () => {
  const httpLink = createHttpLink({
    uri: process.env.REACT_APP_GRAPHQL_ENDPOINT,
    options: {
      reconnect: true,
    },
  });

  return new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache()
  });
}
Example #4
Source File: App.js    From graphql-sample-apps with Apache License 2.0 6 votes vote down vote up
createApolloClient = token => {
  const httpLink = createHttpLink({
    uri: getSlashGraphQLEndpoint(),
    options: {
      reconnect: true,
    },
  });

  const authLink = setContext((request, { headers }) => {
    // return the header to the context so httpLink can read them
    return {
      headers: {
        ...headers,
        "X-Auth-Token": token,
      },
    };
  });

  return new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache()
  });
}
Example #5
Source File: App.js    From graphql-sample-apps with Apache License 2.0 6 votes vote down vote up
createApolloClient = token => {
  const httpLink = createHttpLink({
    uri: config.graphqlUrl,
    options: {
      reconnect: true,
    },
  });

  const authLink = setContext((_, { headers }) => {
    // return the headers to the context so httpLink can read them
    return {
      headers: {
        ...headers,
        "X-Auth-Token": token,
      },
    };
  });

  return new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache()
  });
}
Example #6
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 #7
Source File: ApolloProvider.js    From 0.4.1-Quarantime with MIT License 5 votes vote down vote up
httpLink = createHttpLink({
  uri: 'http://localhost:5000'
})
Example #8
Source File: index.js    From reactnative-best-practice with MIT License 5 votes vote down vote up
client = new ApolloClient({
  link: createHttpLink({uri: 'http://devcloud3.digihcs.com:14047/graphqlhrm'}),
  cache: new InMemoryCache(),
  // link: ApolloLink.from([errorLink, authLink, terminalLink]),
})
Example #9
Source File: index.js    From graphql-sample-apps with Apache License 2.0 5 votes vote down vote up
httpLink = createHttpLink({
  uri: "http://localhost:8080/graphql"
})
Example #10
Source File: App.js    From graphql-sample-apps with Apache License 2.0 5 votes vote down vote up
export default function App() {
  const { loading, user } = useAuth0();
  let headers
  if (user !== undefined) {
    headers = {"username": user.email, "email": user.email}
  }
  if (loading) {
    return <div>Loading...</div>;
  }
  const httpLink = createHttpLink({
    uri: "http://localhost:4000/graphql",
    headers: headers
  });
  
  const client = new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache(),
    request: operation => {
      operation.setContext({
        fetchOptions: {
          mode: "no-cors"
        }
      });
    },
    defaultOptions: {
      query: {
        fetchPolicy: 'network-only',
        errorPolicy: 'all'
      }
    }
  });

  return (
    <ApolloProvider client={client}>
      <div className="container">
        <Router history={history}>
          <Header />
          <Switch>
            <PrivateRoute exact path="/" component={QuestionList} />
            <PrivateRoute exact path="/create" component={CreateQuestion} />
            <PrivateRoute exact path="/view" component={ViewQuestion} />
          </Switch>
        </Router>
      </div>
    </ApolloProvider>
  );
}
Example #11
Source File: App.js    From graphql-sample-apps with Apache License 2.0 5 votes vote down vote up
export default function App() {
  const { loading } = useAuth0();
  
  if (loading) {
    return <div>Loading...</div>;
  }
  const httpLink = createHttpLink({
    uri: "http://localhost:8080/graphql"
  });
  
  const client = new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache(),
    request: operation => {
      operation.setContext({
        fetchOptions: {
          mode: "no-cors"
        }
      });
    },
    defaultOptions: {
      query: {
        fetchPolicy: 'network-only',
        errorPolicy: 'all'
      }
    }
  });

  return (
    <ApolloProvider client={client}>
      <div className="container">
        <Router history={history}>
          <Header />
          <Switch>
            <PrivateRoute exact path="/" component={QuestionList} />
            <PrivateRoute exact path="/create" component={CreateQuestion} />
            <PrivateRoute exact path="/view" component={ViewQuestion} />
          </Switch>
        </Router>
      </div>
    </ApolloProvider>
  );
}
Example #12
Source File: index.js    From graphql-sample-apps with Apache License 2.0 5 votes vote down vote up
httpLink = createHttpLink({
    uri: "http://localhost:8080/graphql"
})
Example #13
Source File: graphql.js    From lifebank with MIT License 5 votes vote down vote up
httpLink = createHttpLink({
  uri: graphqlConfig.url
})
Example #14
Source File: apolloClient.js    From web with MIT License 5 votes vote down vote up
httpLink = createHttpLink({
  uri: config.debug ? config.graphQlUriDev : config.graphQlUri,
  fetch,
})
Example #15
Source File: main.js    From minimal-portfolio with GNU General Public License v3.0 5 votes vote down vote up
httpLink = createHttpLink({
  // You should use an absolute URL here
  uri: 'https://api.github.com/graphql',
  fetch,
  headers: getHeaders()
})