3

I have a nextjs project that is using apollo graphql to fetch data from the backend. I am trying to render my page using server side rendering. But I am currently using graphql apollo react hooks to fetch my data from the backend, and the react hooks prevents me from calling my backend inside of the getServerSideProps.

How do I solve this issue?

import * as React from "react";
import { useExampleQuery } from "graphql/types";

export const getServerSideProps = async ({ params }) => {
  // Here we should probably call graphql function that is now called inside Page
  return { props: { hash: params.hash } };
};

const Page = ({ hash }) => {
  /* 
    I am currently calling my graphql query here using apollo react hooks, 
    that are generated inside graphql/types using @graphql-codegen (typescript)
  */
  const { data, loading } = useExampleQuery({
    variables: {
      hash: hash,
    },
  });

  return loading ? <p>{loading}</p> : <p>{data.example.text}</p>;
};

export default Page;

1 Answer 1

8

getServerSideProps is a server-side function so you can't quite call the apollo query hooks inside it.

An approach is to use the apollo client instance query method.

See the example code below.

import { gql } from '@apollo/client';
import apolloClient from '/path/to/graphql/server/client';

// Example Query
const EXAMPLE_QUERY = gql`
  query EXAMPLE_QUERY($hash: String!) {
    exampleQuery(hash: $hash) {
      ...
    }
  }
`;

export const getServerSideProps = async ({ params }) => {
  const { data } = await apolloClient.query({
    query: EXAMPLE_QUERY,
    variables: { hash: params.hash },
  });

  return {
    props: {
      hash: params.hash,
      data,
    },
  };
};

Also, if importing your apollo client server instance is a bit unclear, you can use this graphql-request package for making graphql requests given a URL.

See example

import { GraphQLClient } from "graphql-request";

// Replace GRAPHQL_URL below to actual
const client =  new GraphQLClient(<GRAPHQL_URL>);

export const getServerSideProps = async ({ params }) => {

  const { data } = await client.request(EXAMPLE_QUERY, {
      hash: params.hash 
  });

  return {
    props: {
      hash: params.hash,
      data,
    },
  };
};
2
  • Thanks, really helpful! So, I am trying to create a new graphql client, as you say, but I am getting a "Network error: request to localhost:3001/graphql failed, reason: connect ECONNREFUSED 127.0.0.1:3001". Do you have any idea what the problem can be? I can access other urls. I do not get that error when the apollo client is initialized in my initApollo-file, but I cannot access the client from inside my initApollo
    – Atonic
    Commented Apr 27, 2021 at 12:14
  • Export the client that works in your initApollo file and use that instance here. However, can I see how you're creating a new graphql client? @Atonic
    – fortunee
    Commented Apr 27, 2021 at 18:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.