I'm trying to authenticate Apollo client with AWS iam.
I have the following typescript script for configuring my Apollo client
/* eslint-disable import/prefer-default-export */
/* eslint-disable import/no-mutable-exports */
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import axios from 'axios';
import { aws4Interceptor } from 'aws4-axios';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { buildAxiosFetch } from '@lifeomic/axios-fetch';
interface IConnection {
uri: string;
region: string;
accessKeyId: string;
secretAccessKey: string;
sessionToken: string;
}
export const initApolloClient = (connection: IConnection): ApolloClient<any> => {
let apolloClient: ApolloClient<any> = null;
const interceptor = aws4Interceptor(
{
region: connection.region,
service: 'execute-api',
},
{
accessKeyId: connection.accessKeyId,
secretAccessKey: connection.secretAccessKey,
sessionToken: connection.sessionToken,
}
);
axios.interceptors.request.use(interceptor);
console.log('Set apollo client');
apolloClient = new ApolloClient({
link: createHttpLink({
uri: connection.uri,
fetch: buildAxiosFetch(axios),
}),
cache: new InMemoryCache(),
});
return apolloClient;
};
This code works fine in my backend lambda node js project. But the same code in my frontend react project causes this error when I try to query data.
users-list.tsx:9 error Error: Network error: url_1.URL is not a constructor
at new ApolloError (ApolloError.ts:46)
at ObservableQuery.getCurrentResult (ObservableQuery.ts:199)
at QueryData.getQueryResult (QueryData.ts:333)
at QueryData._this.getExecuteResult (QueryData.ts:140)
at QueryData.execute (QueryData.ts:61)
at useBaseQuery.ts:63
at useDeepMemo (useDeepMemo.ts:18)
at useBaseQuery (useBaseQuery.ts:62)
at useQuery (useQuery.ts:11)
at UsersList (users-list.tsx:6)
at renderWithHooks (react-dom.development.js:14985)
at updateFunctionComponent (react-dom.development.js:17356)
at beginWork (react-dom.development.js:19063)
at beginWork$1 (react-dom.development.js:23940)
at performUnitOfWork (react-dom.development.js:22776)
at workLoopSync (react-dom.development.js:22707)
at renderRootSync (react-dom.development.js:22670)
at performSyncWorkOnRoot (react-dom.development.js:22293)
at react-dom.development.js:11327
at unstable_runWithPriority (scheduler.development.js:646)
at runWithPriority$1 (react-dom.development.js:11276)
at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
at flushSyncCallbackQueue (react-dom.development.js:11309)
at scheduleUpdateOnFiber (react-dom.development.js:21893)
at dispatchAction (react-dom.development.js:16139)**strong text**
Here is my package.json for the non working frontend project.
Here is my package.json for the working backend project.
You can see that the versions for all the packages I am using is the same.
My Code
I have created a fresh react app with yarn create react-app my-app --template typescript and created a small demo of my problem in a github repo. (There are no iam credentials in the repo but you can still see the problem without them)
Does anyone have any idea;
- What could cause this issue?
- How could I debug this?
Thanks