How do I turn these Craft queries into a GraphQL one?
// entry.twig
entry.getPrev({ section: "contentHub" })
entry.getNext({ section: "contentHub" })
I want to get the previous and next entry using the current entry
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communitySo I'm wondering the same thing, I'm using CraftQL as I need to support mutations, and I am close with this - but I get an error if I try to use $id instead of a hard-wired Int (e.g. 99):
query getEntryData($id: [Int]) {
prevEntry: entry(prevSiblingOf: 99) {
id
title
slug
uri
}
nextEntry: entry(nextSiblingOf: 99) {
id
title
slug
uri
}
entryData: entry(section: investigations, id: $id) {
id
title
slug
uri
}
}
I get the following error:
"message": "Variable \"$id\" of type \"[Int]\" used in position expecting type \"Int\".",
BUT then if I pass the $id as Int (and not [Int]) it works. Can't explain the typing difference.
query getEntryData($id: [Int], $sameId: Int) {
prev:entry(prevSiblingOf: $sameId) {
id
title
slug
uri
}
next: entry(nextSiblingOf: $sameId) {
id
title
slug
uri
}
investigation: entry(section: investigations, id: $id) {
id
title
slug
uri
}
}
@littered
your example is working when you unwrap the Int param in your query. This should work:
query getEntryData($id: Int) {
prevEntry: entry(prevSiblingOf: $id) {
id
title
slug
uri
}
nextEntry: entry(nextSiblingOf: $id) {
id
title
slug
uri
}
entryData: entry(section: investigations, id: [$id]) {
id
title
slug
uri
}
}
the id field in entryData
is expecting a QueryArgument
of type [Int] but prevSiblingOf/nextSiblingOf expect an Int. With the above fix you submit a integer and wrap it for the entryData
in an Array.