0
I have an expo app that is using a graphql api endpoint. I am attempting to pass an authorization token with each request, but my set up does not seems to be setting or sending my authorization header. In my graphql.config.yml I can set the authorization header here like this and it works:
schema: schema.graphql
extensions:
endpoints:
Dev Backend:
url: https://localhost:8000/graphql
introspect: true
headers:
authorization: "My token"
But in my actual client setup code it is not setting my authorization header. The headers being logged on the backend look like this: Headers({‘content-length’: ‘113’, ‘content-type’: ‘application/json; charset=UTF-8’, ‘host’: ‘localhost:8000’, ‘connection’: ‘Keep-Alive’, ‘user-agent’: ‘Apache-HttpClient/4.5.14 (Java/17.0.7)’, ‘accept-encoding’: ‘gzip,deflate’})
Any help on this would be greatly appreciated.
import {ApolloClient, InMemoryCache, ApolloLink, concat} from '@apollo/client';
import {setContext} from '@apollo/client/link/context';
import {getUserSession} from "./session-handler";
import {createUploadLink} from "apollo-upload-client"; // Import AsyncStorage from the correct package
// Create an HTTP link to your GraphQL server
const httpLink = createUploadLink({
uri: 'https://localhost:8000/',
credentials: 'include',
});
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
authorization: "THIS IS A TEST TOKEN",
}
}));
return forward(operation);
})
const client = new ApolloClient({
cache: new InMemoryCache(),
link: concat(authMiddleware, httpLink),
});
export {
client
};
1
See michelfloyd.hashnode.dev/jwt-authentication-in-apollo-v4-part-1 for a working auth example with Apollo and Expo.
22 mins ago
|