You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When implementing Apollo Client with Next.js App Router, I'm encountering a syntax error with the ApolloNextAppProvider component. The error suggests there's an issue with JSX syntax, specifically expecting a '>' character.
I tried to use the example from the README and add the authentication logic we have with our GraphQL API.
Current Code
export function ApolloWrapper({ children }: React.PropsWithChildren) {
return (
<ApolloNextAppProvider makeClient={makeClient}>
{children}
</ApolloNextAppProvider>
);
}
Error Message
'>' expected.
Unterminated regular expression literal.
Declaration or statement expected.
Code
client-wrapper.ts
"use client";
import { Observable, from, FetchResult, Operation } from "@apollo/client";
import {
ApolloNextAppProvider,
ApolloClient,
InMemoryCache,
} from "@apollo/experimental-nextjs-app-support";
import { onError } from "@apollo/client/link/error";
import { setContext } from "@apollo/client/link/context";
import { getAuth } from "firebase/auth";
import Cookies from "js-cookie";
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
import { PropsWithChildren } from "react";
// Types
type PendingRequest = () => void;
// Gestion du refresh token
let isRefreshing = false;
let pendingRequests: PendingRequest[] = [];
const resolvePendingRequests = () => {
pendingRequests.forEach((callback) => callback());
pendingRequests = [];
};
function makeClient() {
// Upload link pour supporter les fichiers
const httpLink = createUploadLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT + "/query",
fetchOptions: { cache: "no-store" },
});
// Gestion de l'authentification
const authLink = setContext(async (_, { headers }) => {
const token = Cookies.get("auth_token");
return {
headers: {
...headers,
"X-Atticard-Authorization": token ? `Bearer ${token}` : "",
},
};
});
// Gestion du refresh token
const handleRefresh = (operation: Operation, forward: any) => {
if (!isRefreshing) {
isRefreshing = true;
return new Observable<FetchResult>((observer) => {
getAuth()
.currentUser?.getIdToken(true)
.then((newToken) => {
Cookies.set("auth_token", newToken);
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
"X-Atticard-Authorization": `Bearer ${newToken}`,
},
}));
})
.then(() => {
isRefreshing = false;
resolvePendingRequests();
forward(operation).subscribe(observer);
})
.catch((error) => {
console.error("Error refreshing auth token:", error);
isRefreshing = false;
pendingRequests = [];
observer.error(error);
});
});
} else {
return new Observable<FetchResult>((observer) => {
pendingRequests.push(() => {
forward(operation).subscribe(observer);
});
});
}
};
// Gestion des erreurs
const errorLink = onError(
({ graphQLErrors, networkError, operation, forward }) => {
if (
networkError &&
"statusCode" in networkError &&
networkError.statusCode === 401
) {
return handleRefresh(operation, forward);
}
if (graphQLErrors) {
for (const err of graphQLErrors) {
if (err.extensions?.code === "UNAUTHENTICATED") {
return handleRefresh(operation, forward);
}
}
}
}
);
// Configuration du client Apollo
return new ApolloClient({
link: from([errorLink, authLink, httpLink]),
cache: new InMemoryCache(),
defaultOptions: {
query: {
fetchPolicy: "network-only",
},
watchQuery: {
fetchPolicy: "network-only",
},
},
});
}
// Provider component
export function ApolloWrapper({ children }: React.PropsWithChildren) {
return (
<ApolloNextAppProvider makeClient={makeClient}>
{children}
</ApolloNextAppProvider>
);
}
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
Description
When implementing Apollo Client with Next.js App Router, I'm encountering a syntax error with the ApolloNextAppProvider component. The error suggests there's an issue with JSX syntax, specifically expecting a '>' character.
I tried to use the example from the README and add the authentication logic we have with our GraphQL API.
Current Code
Error Message
Code
client-wrapper.ts
package.json
tsconfig.json
Screens :
The text was updated successfully, but these errors were encountered: