2022-01-23

Getting the categoryId of a post in Graphql

This is my Graphql query for getting posts from headless wordpress:

export const GET_POSTS = gql`
 query GET_POSTS( $uri: String, $perPage: Int, $offset: Int, $categoryId: Int ) {
 
  posts: posts(where: { categoryId: $categoryId, offsetPagination: { size: $perPage, offset: $offset }}) {
    edges {
      node {
        id
        title
        excerpt
        slug
        featuredImage {
          node {
            ...ImageFragment
          }
        }
        categories {
          edges {
            node {
              categoryId
              name
            }
          }
        }
      }
    }
    pageInfo {
      offsetPagination {
        total
      }
    }
  }
 }
 
 ${ImageFragment}
 
 `;

When i do this: console.log("DATAAAA", data.posts.edges);

i get:

    DATAAAA [
  {
    node: {
      id: 'cG9zdDo0MA==',
      title: 'postttt',
      excerpt: '<p>dlkfjdsflkdslkdjfkldsf</p>\n',
      slug: 'postttt',
      featuredImage: null,
      categories: [Object],
      __typename: 'Post'
    },
    __typename: 'RootQueryToPostConnectionEdge'
  },
  {
    node: {
      id: 'cG9zdDox',
      title: 'Hello world!',
      excerpt: '<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n',
      slug: 'hello-world',
      featuredImage: null,
      categories: [Object],
      __typename: 'Post'
    },
    __typename: 'RootQueryToPostConnectionEdge'
  }
]

But when try to go further, inside node, like this: console.log("DATAAAA", data.posts.edges.node); in order to get the categoryId which is inside categories: [Object], i get undefined.

How to get categoryId based on this query?

What i want to do is to get only the posts by a given category in getStaticProps like this, but i dont know how to get that categoryId dinamically. This is what my getStaticProps function looks like:

export async function getStaticProps(context) {
  console.log("sfsdfdsfdsf", context);
  const { data, errors } = await client.query({
    
    query: GET_POSTS,
    variables: {
      uri: context.params?.slug ?? "/",
      perPage: PER_PAGE_FIRST,
      offset: null,
      categoryId: <===== How to get this dinamically?
    },
  });

  const defaultProps = {
    props: {
      data: data || {},
    },
    
    revalidate: 1,
  };

  return handleRedirectsAndReturnData(defaultProps, data, errors, "posts");
}

This is my getStaticPaths function:

export async function getStaticPaths() {
  const { data } = await client.query({
    query: GET_CATEGORY_SLUGS_ID,
  });

  const pathsData = [];

  data?.categories?.edges.node &&
    data?.categories?.edges.node.map((category) => {
      if (!isEmpty(category?.slug)) {
        pathsData.push({ params: { slug: category?.slug } });
      }
    });

  return {
    paths: pathsData,
    fallback: FALLBACK,
  };
}

and this is what i get from context console.log("THE CONTEXT", context);:

THE CONTEXT {
  params: { slug: 'uncategorized' },
  locales: undefined,
  locale: undefined,
  defaultLocale: undefined
}

Any help would be appreciated.



from Recent Questions - Stack Overflow https://ift.tt/3IxUQoJ
https://ift.tt/eA8V8J

No comments:

Post a Comment