Srys
Srys

Reputation: 11

502 Bad Gateway Error when Switching Pages in Next.js with Apollo Client Setup

I'm encountering a 502 Bad Gateway error when switching between pages in my Next.js application that uses Apollo Client. Here’s a brief overview of my setup:

import { ApolloClient, HttpLink, InMemoryCache, split } from "@apollo/client";
import { getMainDefinition } from "@apollo/client/utilities";

import { removeLastTrailingSlash } from "lib/util";
import getConfig from "next/config";

let client;

/**
 * getApolloClient
 */
export function getApolloClient() {
  if (!client) {
    client = _createApolloClient();
  }
  return client;
}

/**
 * _createApolloClient
 */
export function _createApolloClient() {
  const { publicRuntimeConfig } = getConfig();
  const { Directus } = publicRuntimeConfig;

  // Main GraphQL endpoint
  const directusLink = new HttpLink({
    uri: removeLastTrailingSlash(Directus.url),
  });

  // Split links based on operation name or type
  const splitLink = split(({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === "OperationDefinition" &&
      definition.operation === "query" &&
      definition.name?.value.startsWith("Directus")
    );
  }, directusLink);

  return new ApolloClient({
    link: splitLink,
    cache: new InMemoryCache(),
    defaultOptions: {
      watchQuery: {
        fetchPolicy: "no-cache",
        errorPolicy: "ignore",
      },
      query: {
        fetchPolicy: "no-cache",
        errorPolicy: "all",
      },
    },
  });
}

Issue:

Additional Information:

What I've Tried:

Question:

What could be causing the 502 Bad Gateway error when switching pages, and how can I troubleshoot or resolve this issue in my Apollo Client setup?

Upvotes: 1

Views: 108

Answers (0)

Related Questions