Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

Correct method for passing "orderBy:" as a variable? #23

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

I'm trying to pass in the "orderBy:" parameter as a variable in the GraphiQL client, but I'm not sure how:

query getRegions ($order: String) {
  iwRegions (range: [0, 4], orderBy: $order) {
    name
  }
}

with variables:

{
  "order": "name"
}

but I get an error message:

{
  "errors": [
    {
      "message": "Variable \"$order\" of type \"String\" used in position expecting type \"IwRegionPropertiesEnum\".",
      "locations": [
        {
          "line": 1,
          "column": 19
        },
        {
          "line": 2,
          "column": 38
        }
      ]
    }
  ]
}

How should I do this?

Thanks, Adrian

koskimas commented 6 years ago

Maybe this https://stackoverflow.com/questions/35946313/how-to-set-a-relay-variable-as-an-enum-value/35987582#35987582 ?

koskimas commented 6 years ago

Does this work?

query getRegions ($order: IwRegionPropertiesEnum) {
  iwRegions (range: [0, 4], orderBy: $order) {
    name
  }
}
Trellian commented 6 years ago

I can't get it to work, but... GraphiQL is doing something odd. When I paste the above (with a few extra columns included in the query), and then try to set up the variables, the typeahead functionality in the variables pane forces the following:

{
  "order": "name"
}

or similar, for the other column names (if I add them to the query).

Executing the query then produces:

{
  "data": {
    "iwRegions": null
  },
  "errors": [
    {
      "message": "undefined passed as argument #0 for 'orderBy' operation. Call skipUndefined() method to ignore the undefined values.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "iwRegions"
      ]
    }
  ]
}
Trellian commented 6 years ago

I also get this log trace:

Error: undefined passed as argument #0 for 'orderBy' operation. Call skipUndefined() method to ignore the undefined values.
    at convertUndefined (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\operations\ObjectionToKnexConvertingOperation.js:65:11)
    at convertArgs (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\operations\ObjectionToKnexConvertingOperation.js:36:14)
    at KnexOperation.onAdd (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\operations\ObjectionToKnexConvertingOperation.js:22:17)
    at QueryBuilder.addOperation (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\QueryBuilderOperationSupport.js:147:33)
    at QueryBuilder.orderBy (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\QueryBuilderBase.js:297:17)
    at Object.query (C:\xampp\htdocs\iwallq\api\node_modules\objection-graphql\lib\argFactories.js:119:15)
    at QueryBuilder.<anonymous> (C:\xampp\htdocs\iwallq\api\node_modules\objection-graphql\lib\SchemaBuilder.js:361:34)
    at QueryBuilder.modify (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\QueryBuilderBase.js:48:12)
    at QueryBuilder.modify (C:\xampp\htdocs\iwallq\api\node_modules\objection\lib\queryBuilder\QueryBuilder.js:145:22)
    at C:\xampp\htdocs\iwallq\api\node_modules\objection-graphql\lib\SchemaBuilder.js:220:17
    at resolveFieldValueOrError (C:\xampp\htdocs\iwallq\api\node_modules\graphql\execution\execute.js:501:12)
    at resolveField (C:\xampp\htdocs\iwallq\api\node_modules\graphql\execution\execute.js:465:16)
    at C:\xampp\htdocs\iwallq\api\node_modules\graphql\execution\execute.js:314:18
    at Array.reduce (<anonymous>)
    at executeFields (C:\xampp\htdocs\iwallq\api\node_modules\graphql\execution\execute.js:311:42)
    at executeOperation (C:\xampp\htdocs\iwallq\api\node_modules\graphql\execution\execute.js:239:122)
Trellian commented 6 years ago

Note the line number at "node_modules\objection-graphql\lib\argFactories.js:119:15" should be 118, but I stuck a 'console.warn()' in at 118

koskimas commented 6 years ago

@Trellian I found a bug in this. It's now fixed in master

Trellian commented 6 years ago

Awesome! Thank you!