Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphQL] relatedTo variable type [Int] and [QueryArgument] conflict #5841

Closed
samput opened this issue Mar 25, 2020 · 6 comments
Closed

[GraphQL] relatedTo variable type [Int] and [QueryArgument] conflict #5841

samput opened this issue Mar 25, 2020 · 6 comments
Assignees

Comments

@samput
Copy link

samput commented Mar 25, 2020

Description

Querying an entry by ID requires that the variable type be [QueryArgument]. However related entries require the variable type to be [Int]

Query variable
{
  "id": 173
}
Query [QueryArgument]
query Series($id: [QueryArgument]) {
  entry(section: "series", id: $id) {
    id
    title
  }
  pipeline: entries(section:"pipeline", type: ["ongoing", "monthly"], relatedTo: $id) {
    id
    title
  }
}
Error [QueryArgument]
{
  "errors": [
    {
      "message": "Variable \"$id\" of type \"[QueryArgument]\" used in position expecting type \"[Int]\".",
      "category": "graphql",
      "locations": [
        {
          "line": 1,
          "column": 14
        },
        {
          "line": 6,
          "column": 82
        }
      ]
    }
  ]
}
Query [Int]
query Series($id: [Int]) {
  entry(section: "series", id: $id) {
    id
    title
  }
  pipeline: entries(section:"pipeline", type: ["ongoing", "monthly"], relatedTo: $id) {
    id
    title
  }
}
Error [Int]
{
  "errors": [
    {
      "message": "Variable \"$id\" of type \"[Int]\" used in position expecting type \"[QueryArgument]\".",
      "category": "graphql",
      "locations": [
        {
          "line": 1,
          "column": 14
        },
        {
          "line": 2,
          "column": 32
        }
      ]
    }
  ]
}

Additional info

  • Craft version: 3.4.10.1
  • PHP version: 7.3.15
  • Database driver & version: MySQL 5.7.24
  • Plugins & versions: Feed Me | 4.2.1.1, Redactor | 2.6.1
@andris-sevcenko
Copy link
Contributor

Yes, that's correct. This is because the id can be in the form of ["not", 1, 2, 3] while the relatedTo accepts only ids of elements that must be related.

If you know the field the relationship is used on, you can, however, use that field as the argument instead of using relatedTo, and that should also be expecting an array of QueryArgument values.

@samput
Copy link
Author

samput commented Mar 30, 2020

@andris-sevcenko ah ok that makes sense. Using the field as an argument works perfectly. Thanks for the help!

@mediabeastnz
Copy link

Can we have an example maybe in the docs. I'm having a similar issue and can't find a good solution / explanation.
For example i'm trying to do this.

query($id: [Int!], $entryID: [Int!]) {
    entries(section: "items", relatedTo: $id, id: ["not", $entryID]) {
    ...

@mediabeastnz
Copy link

mediabeastnz commented Aug 12, 2020

Never mind... figured it out 🤦‍♂️

I've now got ...

query($id: [Int!], $entryID: QueryArgument) {
    entries(section: "items", relatedTo: $id, id: ["not", $entryID]) {
    ...

@andris-sevcenko
Copy link
Contributor

@mediabeastnz glad you figured it out! I agree that there are some murky things we should clear up in GraphQL. I'll see how soon we can fill those gaps.

@nomi-invozone
Copy link

query($id: ID!, $entryID: QueryArgument) {
entries(section: "items", relatedTo: $id, id:$entryID) {
...

well like this also works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants