Search code examples
reactjsgraphqlwp-graphql

Getting all post data even when specifying post id to get just one - WPGraphql


I am trying to get a cursor for a post given the postId but in return I am getting cursors of all posts. Note that this query works fine when I hardcode the id into the query.

query:

export const GET_POST_CURSOR = gql `
  query GetPostById($id: Int) {
    posts(where: {id: $id}) {
      edges {
        cursor
      }
      
    }
  }
`;

React

let postId = 138
const postCursor = useQuery(GET_POST_CURSOR, {variables: {postId}}) 
  
  if (!postCursor.loading) { 
    console.log("cursor: ", postCursor)
  }

Solution

  • React

    let postId = 138;
    const postCursor = useQuery(GET_POST_CURSOR, {variables: {id: postId}})