1

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

3

So 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
  }
}
| improve this answer | |
1

@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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.