mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-25 14:12:16 +00:00
- Replace createHttpLink with HttpLink constructor - Replace setContext with SetContextLink constructor - Update authLink to use prevContext parameter and spread operator for proper context merging - Replace from with ApolloLink.from for link composition - Remove ApolloProvider wrapper from Providers component (render children directly)
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client';
|
|
import { SetContextLink } from '@apollo/client/link/context';
|
|
|
|
// GraphQL API endpoint
|
|
const httpLink = new HttpLink({
|
|
uri: process.env.NEXT_PUBLIC_API_SERVER_URL || 'http://localhost:8080/v1/graphql',
|
|
});
|
|
|
|
// Auth link to add authentication token to requests
|
|
const authLink = new SetContextLink((prevContext, operation) => {
|
|
// Get the authentication token from local storage if it exists
|
|
const token = localStorage.getItem('auth_token');
|
|
|
|
return {
|
|
...prevContext,
|
|
headers: {
|
|
...prevContext.headers,
|
|
authorization: token ? `Bearer ${token}` : '',
|
|
},
|
|
};
|
|
});
|
|
|
|
// Create Apollo Client instance
|
|
export const apolloClient = new ApolloClient({
|
|
link: ApolloLink.from([authLink, httpLink]),
|
|
cache: new InMemoryCache(),
|
|
defaultOptions: {
|
|
watchQuery: {
|
|
fetchPolicy: 'cache-and-network',
|
|
},
|
|
query: {
|
|
fetchPolicy: 'network-only',
|
|
},
|
|
mutate: {
|
|
fetchPolicy: 'network-only',
|
|
},
|
|
},
|
|
});
|
|
|
|
// GraphQL queries and mutations can be defined here
|
|
// Example:
|
|
/*
|
|
import { gql } from '@apollo/client';
|
|
|
|
export const GET_SERVICES = gql`
|
|
query GetServices {
|
|
services {
|
|
id
|
|
name
|
|
status
|
|
health
|
|
}
|
|
}
|
|
`;
|
|
*/
|