I am using MongoDB realm GraphQL API to access data. I am using Apollo-Angular.
The GraphQL API needs an API key to access the data. I tried with the postman and it work. Using the postman post request I pass the key in the Authorization that added in Header with the key: apiKey, value: The_Api_Key_from_Mongo
But how can I add this authorization Apollo-Angular settings?
const uri =
'https://ap-south-1.aws.realm.mongodb.com/api/client/v2.0/app/myapp_with_id/graphql';
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
return {
link: httpLink.create({
uri,
}),
cache: new InMemoryCache(),
};
}
Simply: I have GraphQL api uri to access this API I have to pass api key. How can I do this in Apollo-Angular?
You need to refactor your Apollo Angular return statement by passing this new param to the
HttpLink. So instead of thelinkkey inholds a single object it will hold an array of objects. The new API key can be attached as an item in the array after passing it to the
setContext()method.1- Set the new authentication API key using
setContext():2- Set your URI as another
constsimilar to theAPIKeyin step 1 above3- Refactor your return statement to be
4- Do not forget to import
fromin the return statement above from'@apollo/client/core'andsetContext()from'@apollo/client/link/context'