Vedran
Vedran

Reputation: 1150

Disable server query during build

We have a docker image that sets up the backend server and the frontend NextJS application that is using ApolloClient. During the build process of the NextJS application the Apollo client tries to query the backend server'd graphql endpoint which is not working because the server has not been started.

Is there any way to have the ApolloClient NOT query outside servers during build time? Here's the constructor:

function createApolloClient() {
  return new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: createUploadLink({
      uri: process.env.HOSTNAME + '/graphql',
      credentials: 'same-origin',
    }),
    cache: new InMemoryCache({
      typePolicies: {
        Query: {
          fields: {
            allPosts: concatPagination(),
          },
        },
      },
    }),
    onError: ({ networkError, graphQLErrors }) => {
      console.log('graphQLErrors', graphQLErrors)
      console.log('networkError', networkError)
    }
  })
}

Ultimately what we want to have is one docker container running the backend (Java) server that serves the NextJS built static files.

Upvotes: 1

Views: 491

Answers (1)

i-know-nothing
i-know-nothing

Reputation: 908

It's unclear from your question, but when do you want your query to be run?

  • If it's supposed to be build time, why is the backend server not running? Where is Next.js supposed to get the data from then?
  • At request time, by the server? Use getServerSideProps
  • At request time, by the client (browser?) Check for typeof(window) !== 'undefined' and use fetch + async/await.

Upvotes: 1

Related Questions