score:5
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import gql from 'graphql-tag';
const token = "YOUR_ACCESS_TOKEN";
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : null,
}
}
});
const client = new ApolloClient({
link: authLink.concat(new HttpLink({ uri: 'https://api.github.com/graphql' })),
cache: new InMemoryCache()
});
client.query({
query: gql`
query ViewerQuery {
viewer {
login
}
}
`
})
.then(resp => console.log(resp.data.viewer.login))
.catch(error => console.error(error));
Source: stackoverflow.com
Related Query
- Authentication for GitHub API v4 with Apollo-Client
- How to handle httpOnly cookie authentication in next.js with apollo client
- Dynamically set GraphQL queries for React components with Apollo Client
- Why query with apollo client on fragment gives me an empty object while api is working fine?
- .Net Core 2.1 with IdentityServer4 Cookie and API JWT based authentication for the same app
- How to contact twitter api with client authentication using express as proxy and xhr as client
- How to convert Apollo Subscription with Authentication using Apollo Client 2.x from 1.x
- How to fix authentication error when querying Yelp GraphQL API using Apollo Client
- How to make a second api call with data received from the first api call in Apollo client with React?
- How to do Batch Mutations with Apollo Client
- How to correctly redirect after catching authentication failure with Apollo and React
- Reset store after logout with Apollo client
- Problem with protected routes, context API and firebase user authentication request
- Difference between AWS Amplify & Apollo Client for GraphQL?
- Is graphql's ID type necessary if I've set an unique identifier with dataIdFromObject in Apollo Client
- Apollo client 2 with React couldn't make query
- Storing User-Id in Client Side App for API Request
- Using different API url for development and production with React and axios
- Issue with refetchQueries in the Apollo Client useMutation hook
- How to refresh firebase IdToken with apollo Reactjs client
- How to use devise-jwt with devise for signin, signup and signout in rails api
- Does Apollo client optimistic update changes reference for every list item?
- Replacing Redux with Apollo for local state management
- Reactjs client for Swagger / OpenAPI API specification
- Active Directory Authentication with .NET Core Web API and React
- react firebase authentication with apollo graphql
- Next Js combined with an external REST API Authentication and atuhorization
- How to detect disconnect and reconnect for subscription(websocket) in apollo client
- How to implement remember me functionality for authentication in ReactJS when i only receive jwt token from backend api
- Apollo client - local field with computed value from object
More Query from same tag
- Testing React component that observes state
- how to pass outlet context to nested route in react router v6
- Error: findComponentRoot when using divs
- NextJS Typescript Modal
- Cannot find module '@babel/plugin-transform-runtime' from
- React how to: replace only one component on click (dynamic UI)
- Keyboard navigation on react data grid custom editor with semantic ui calendar date picker
- React Context Api using function from context provider
- React Component interface to methods
- Not able to access session storage while page routing in reactjs
- Setting API data as state in React is undefined, but the API data exists
- React prop being marked as undefined
- How to create an array in react with typescript?
- React eventsource is not closed
- iam learning REACTJS but i had an error if anyone can help me
- Labels for Nested Doughtnut Chart React ChartJS
- My collapsable item doesn't transition smoothly on close, bottom border jumps to the top
- How can I fetch data on every app start using nextjs
- How to handle React Config.js file in Azure DevOps Pipeline
- TypeError: arrayObject.sort is not a function in react jest enzyme
- Editing states between high and low order components
- React - How to Pass an Argument to an Event Handler
- Can't render image from state in React/JSX
- onClick executing a function and setState in react
- Will the background component unmount when modal is open in react? If not, how to maintain/store states of modal and react component using redux?
- how to add whatever value is selected to an array?
- Lazy loaded React router routes loading anyway
- How can I resize a button with css in ReactJS
- Iterate over array if the prop is there without getting 'cant iterate over undefined' error
- Multiple API handling through Axios in react application