1

I'm trying to get a content by the slug param in the url inside a next js app. When using getServerSideProps, I can pass a context.query.id, but here I'm trying to use getStaticProps. This is what I have so far:

export async function getStaticProps() {
  const apolloClient = initializeApollo();
  const { data } = await apolloClient.query({
    query: SLUG_QUERY,
  });

  return addApolloState(apolloClient, {
    props: {
      data,
    },
    revalidate: 60,
  });
}

const SLUG_QUERY = gql`
  query ($id: String!) {
    postCollection(slug: { eq: $id }) {
      items {
        title
        shortDescription
        heroImg {
          url
        }
        createdAt
        slug
      }
    }
  }
`;

I'm struggling to understand How can I pass the conbtext.query.id as a parameter in the gql query. Thanks for the help

2
  • Where is the $id coming from? Are you using this in combination with getStaticPaths? Is this a dynamic page? Commented Jun 1, 2022 at 1:58
  • yeah this is a [id].tsx file, and get static path is mapping const paths = data.postCollection.items.map((post) => ({ params: { id: post.slug }, })) Commented Jun 1, 2022 at 7:19

1 Answer 1

3

pass context into your getStaticProps function

export const getStaticProps = async (context) => {

then move your query inside the getStaticProps to use it

postCollection(slug: { eq: $context.params.id }) {

Alternatively, if you prefer to keep your query outside of the function (if you are reusing it elsewhere for example), then just wrap it in a function that takes the id as a parameter

const getPostCollectionQuery = (id) => {
  const SLUG_QUERY = gql`
    query ($id: String!) {
      postCollection(slug: { eq: $id }) {
        items {
          title
          shortDescription
          heroImg {
            url
          }
          createdAt
          slug
        }
      }
    }
  `;

  return SLUG_QUERY;
};

and then you can call that from your getStaticProps function

const { data } = await apolloClient.query({
  query: getPostCollectionQuery(context.params.id),
});
Sign up to request clarification or add additional context in comments.

4 Comments

but the gql literal query is outside the getStaticProps, I've tried passing it down but doesn't work: export const SLUG_QUERY = (id) => gql` query($id: String!) { postCollection(slug: { eq: $id }){ items{ title, shortDescription, heroImg{ url } createdAt, slug } } } `;
export const getStaticProps = async (context) => { const apolloClient = initializeApollo(); const { data } = await apolloClient.query({ query: SLUG_QUERY(context.params.id) });
@RobertoFosso I missed the fact that you had defined the query outside the function. I've updated my answer. Either move the query inside the function or wrap the query in a helper function
Sorry to come back on this, but the id param after the function declaration is grey out, and is not being passed inside the gql query. I'm getting now the error: ' Variable "$id" of required type "String!" was not provided.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.