Hi,
I’ve written my own hook to implement a graphql client, but I’m having trouble with the types of HttpLink.
Here’s my hook:
import { setContext } from '@apollo/client/link/context';
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
export function useGraphql() {
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
},
};
});
const httpLink = new HttpLink({
uri: 'https://example.com/graphql',
credentials: 'include',
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(httpLink),
});
return client;
}
Here’s the test:
import apollo, { HttpLink } from '@apollo/client';
import { useGraphql } from './use-graphql';
import 'whatwg-fetch';
jest.mock('@apollo/client', () => ({
...jest.requireActual('@apollo/client'),
}));
describe('useGraphql', () => {
it('should return the Apollo client', async () => {
const graphqlApi = 'http://example.com/graphql';
const HttpLinkObject = {
options: {
uri: graphqlApi,
credentials: "include"
}
};
const client = useGraphql();
jest.spyOn(HttpLink, HttpLinkObject)
await expect(HttpLink).toHaveBeenCalledWith({
uri: graphqlApi,
credentials: 'include',
});
expect(ApolloClient).toHaveBeenCalledWith({
cache: new InMemoryCache(),
link: {},
});
expect(client).toBeInstanceOf(apollo.ApolloClient);
});
});
The error that is tripping me up is
Type '{ options: { uri: string; credentials: string; }; }' is not assignable to type '"concat"'.
Overload 2 of 4, '(object: typeof HttpLink, method: never): SpyInstance<never, never>', gave the following error.
Argument of type '{ options: { uri: string; credentials: string; }; }' is not assignable to parameter of type 'never'.
A nudge in the right direction would be much appreciated!