Refactor Apollo Client setup by removing createLink function
- Updated the `createApolloClient` function to directly handle authorization headers and GraphQL URI configuration, improving modularity and reducing complexity. - Removed the `createLink` function, streamlining the client setup process and enhancing maintainability. - Renamed type `Parameters` to `Parameters_` for clarity in the client function signature.
This commit is contained in:
parent
c4b76a4755
commit
8aaae245a7
@ -1,17 +1,18 @@
|
||||
import { env as environment } from '../config/env';
|
||||
import { getToken } from '../config/token';
|
||||
import { createLink } from './link';
|
||||
import { ApolloClient, InMemoryCache } from '@apollo/client/core';
|
||||
|
||||
type Parameters = { token: null | string | undefined };
|
||||
type Parameters_ = { token: null | string | undefined };
|
||||
|
||||
export function createApolloClient(parameters?: Parameters) {
|
||||
export function createApolloClient(parameters?: Parameters_) {
|
||||
return new ApolloClient({
|
||||
cache: new InMemoryCache(),
|
||||
link: createLink({
|
||||
token: parameters?.token,
|
||||
uri: environment.URL_GRAPHQL_CACHED,
|
||||
}),
|
||||
headers: parameters?.token
|
||||
? {
|
||||
Authorization: `Bearer ${parameters.token}`,
|
||||
}
|
||||
: undefined,
|
||||
uri: environment.URL_GRAPHQL_CACHED,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
import { ApolloLink, from, HttpLink } from '@apollo/client/core';
|
||||
|
||||
type Parameters = { token: null | string | undefined; uri: string };
|
||||
|
||||
export function createLink({ token, uri }: Parameters) {
|
||||
const cacheLink = new ApolloLink((operation, forward) => {
|
||||
return forward(operation);
|
||||
});
|
||||
|
||||
const httpLink = new HttpLink({
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||
uri,
|
||||
});
|
||||
|
||||
return from([cacheLink, httpLink]);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user