Relay: Query working in graphiql but not in relay

Created on 2 Jun 2017  路  11Comments  路  Source: facebook/relay

This is the query:

query TreeTaxonomyLevel2Query($taxonomyId: Uuid) {
  allDataTypes {
    nodes {
      nameGerman
      name
      categoriesByDataType {
        totalCount
        nodes {
          id
          name
          taxonomiesByCategory(condition: {id: $taxonomyId}) {
            totalCount
            nodes {
              id
              name
            }
          }
        }
      }
    }
  }
}

It works in http://localhost:5000/graphiql with

{
  "taxonomyId": "24fa87fd-6d9e-499f-9ce0-a271fa0ce72b"
}

But errors out when relay-compiler builds this component:

<QueryRenderer
  environment={environment}
  query={graphql`
    query TreeTaxonomyLevel2Query($taxonomyId: Uuid) {
      allDataTypes {
        nodes {
          nameGerman
          name
          categoriesByDataType {
            totalCount
            nodes {
              id
              name
              taxonomiesByCategory(condition: {id: $taxonomyId}) {
                totalCount
                nodes {
                  id
                  name
                }
              }
            }
          }
        }
      }
    }
  `}
  variables={{ taxonomyId: store.activeNodeArray[2] }}
  render={({ error, props }) => (
    <Tree
      nodes={sort([
        ...level0FromProps(props),
        ...taxonomyLevel1FromProps(store, props),
        ...taxonomyLevel2FromProps(store, props),
      ])}
    />
  )}
/>

With this error:

> relay-compiler --src ./src --schema ./schema/schema.graphql "--watch"

Parsed default in 0.10s

Writing default
Invariant Violation: RelayCodeGenerator: Complex argument values (Lists or InputObjects with nested variables) are not supported, argument `condition` had value `{
  "kind": "ObjectValue",
  "metadata": null,
  "fields": [
    {
      "kind": "ObjectFieldValue",
      "metadata": null,
      "name": "id",
      "value": {
        "kind": "Variable",
        "metadata": null,
        "variableName": "taxonomyId"
      }
    }
  ]
}`. Source: document undefined.
    at invariant (C:\Users\alex\ae2\node_modules\fbjs\lib\invariant.js:44:15)
    at Object.Argument (C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:1353:30)
    at visit (C:\Users\alex\ae2\node_modules\graphql\language\visitor.js:229:26)
    at Object.visitIR [as visit] (C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:2007:11)
    at Object.generate (C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:1228:35)
    at C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:5485:56
    at Array.forEach (native)
    at RelayCompiler.compile (C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:5472:31)
    at RelayFileWriter.<anonymous> (C:\Users\alex\ae2\node_modules\relay-compiler\bin\relay-compiler:5764:43)
    at Generator.next (<anonymous>)

Am I getting something wrong or is this not possible?

Most helpful comment

Yeah, I ran into the same issue. Don't think it's supported but what you can do is basically to put in your entire condition as a variable so instead of doing

variables={{ taxonomyId: store.activeNodeArray[2] }}

you would do

variables={{ condition: { taxonomyId: store.activeNodeArray[2] } }}

and then you'll have to update your

query TreeTaxonomyLevel2Query($taxonomyId: Uuid) {

line with the type of your condition i.e. something like

query TreeTaxonomyLevel2Query($condition: TaxonomyCondition!) {

and finally change your

taxonomiesByCategory(condition: {id: $taxonomyId}) {

to use passed in condition as

taxonomiesByCategory(condition: $condition) {

All 11 comments

Yeah, I ran into the same issue. Don't think it's supported but what you can do is basically to put in your entire condition as a variable so instead of doing

variables={{ taxonomyId: store.activeNodeArray[2] }}

you would do

variables={{ condition: { taxonomyId: store.activeNodeArray[2] } }}

and then you'll have to update your

query TreeTaxonomyLevel2Query($taxonomyId: Uuid) {

line with the type of your condition i.e. something like

query TreeTaxonomyLevel2Query($condition: TaxonomyCondition!) {

and finally change your

taxonomiesByCategory(condition: {id: $taxonomyId}) {

to use passed in condition as

taxonomiesByCategory(condition: $condition) {

Unfortunately this is a current limitation of the relay compiler, something we plan on improving. Sorry for the trouble, but happy your question was answered.

@theill Hi, I am trying on the solution provided by you but GraphQL server throws error when running through Relay but it works with the same query and same variables in GraphiQL. Can you please provide any alternative solution to send a conditional query from relay.

@Kenam18 what's the error you see from the relay-compiler?

relay-compiler does not throw any error. It shows:
Writing default
Writer time: 0.02s [0.02s compiling, 0.00s generating, 0.00s extra]
Unchanged: 6 files
Written default in 0.05s
Watching for changes to default...
Parsed default in 0.30s

But Graphql server shows this error and therefore, the query is not rendered in UI :
1 error(s) in 3.1ms :: query PersonQuery($condition: PersonCondition!) { allPersons(orderBy: PNAME_ASC, condition: $condition) { nodes { pcode pname } } }

Snippet of QueryRenderer code:
query={graphqlquery PersonQuery($condition: PersonCondition!) { allPersons(orderBy: PNAME_ASC, condition: $condition) { nodes { pcode pname } } }}
variables={{condition:{pcode:18}} }

It's hard to know what could be causing this when you're not seeing the actual error returned from GraphQL. I usually run my queries through a proxy such as Charles which will allow you to explicitly see what's being sent to and received from your GraphQL endpoint. Try to do that. Then you'll probably see a payload from GraphQL with an error json object specifying the real issue.

Okay. Thank you ,will try and let you know.

Error:
{"errors":[{"message":"Variable \"$condition\" of required type \"PersonCondition!\" was not provided.","locations":[{"line":2,"column":3}]}]}

I doubt if it is able to get the condition variable.Can you suggest?

what's the payload being sent to your GraphQL endpoint? I think your client might be sending something you're not expecting.

Is this limitation still present?

Unfortunately this is a current limitation of the relay compiler, something we plan on improving. Sorry for the trouble, but happy your question was answered.

@AO19 can you open a new issue with more information of your use case?

can you try with latest relay version?

Was this page helpful?
0 / 5 - 0 ratings