简体   繁体   中英

How do I run GraphQL queries after router.query?

Started learning React and Next just some time ago and having trouble making this work. Also tried using useLazyQuery but could not figure out how to return properly.

...
const TaskSingle = () => {

    const { category } = router.query;

    const { loading, error, data } = useQuery(GET_TASKS, {
        variables: {
            taskFilter: {
                taskCategory: 5, // works
                // taskCategory: category // doesn't work
            },
        },
    });
}

const { categories } = data;

return (
  <div>
  {categories.map((cat) => {
      <div key={cat.id}>
          {cat.name}
      </div>
  })}
 </div>
)

When I try to use "category" instead of a number I'm getting this error: http://hidden-url:3000/graphql:1 Failed to load resource: the server responded with a status of 400 (Bad Request)

In the end I used

const category = Number(router.query.category);

instead of

const { category} = router.query;

because I needed a number and not a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM