feat: Update Apollo Client configuration to use explicit constructors and fix context link implementation

- 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)
This commit is contained in:
nutcas3
2026-04-25 03:40:28 +03:00
parent 3cef1fbf03
commit 919e9906e8
2 changed files with 8 additions and 10 deletions
+1 -4
View File
@@ -1,8 +1,5 @@
'use client';
import { ApolloProvider } from '@apollo/client';
import { apolloClient } from '@/lib/apollo-client';
export function Providers({ children }: { children: React.ReactNode }) {
return <ApolloProvider client={apolloClient}>{children}</ApolloProvider>;
return <>{children}</>;
}
+7 -6
View File
@@ -1,19 +1,20 @@
import { ApolloClient, InMemoryCache, createHttpLink, from } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client';
import { SetContextLink } from '@apollo/client/link/context';
// GraphQL API endpoint
const httpLink = createHttpLink({
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 = setContext((_, { headers }) => {
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: {
...headers,
...prevContext.headers,
authorization: token ? `Bearer ${token}` : '',
},
};
@@ -21,7 +22,7 @@ const authLink = setContext((_, { headers }) => {
// Create Apollo Client instance
export const apolloClient = new ApolloClient({
link: from([authLink, httpLink]),
link: ApolloLink.from([authLink, httpLink]),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {