Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 1.02 KB

use-query-ssr.md

File metadata and controls

57 lines (42 loc) · 1.02 KB

How to use useQuery during server-side rendering?

Insead of using useQuery, you should initialize an ApolloClient and call the query method:

Init a client:

# lib/apollo.ts

const apolloClient = new ApolloClient({
  uri: "http://localhost:3001/graphql",
  cache: new InMemoryCache(),
});

getStaticPaths:

# position/[id].tsx

export async function getStaticPaths() {
  const { data } = await apolloClient.query({ query: ALL_POSITIONS_QUERY });

  const paths = data.allPositions.map((item: any) => ({
    params: { id: item._id },
  }));

  return {
    paths,
    fallback: false,
  };
}

getStaticProps:

# position/[id].tsx

export async function getStaticProps({ params }: any) {
  const { id } = params;

  const { data } = await apolloClient.query({
    query: POSITION_QUERY,
    variables: { id },
  });

  return {
    props: {
      position: data.position,
    },
  };
}

References: