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

Variable "$id_0" of type "ID" used in position expecting type "ID!" error when refetching. #839

Closed
F21 opened this issue Feb 15, 2016 · 5 comments

Comments

@F21
Copy link

F21 commented Feb 15, 2016

Relay generates queries like this when fetching:

query Overview_ResourcesRelayQL($id_0:ID){
  node(id:$id_0){
    id,__typename,
    ...F0
  }
} 

fragment F0 on Faction{
  id,
  name
}

The type for ID should be ID! rather than ID, otherwise we get this error:

Variable "$id_0" of type "ID" used in position expecting type "ID!"
@josephsavona
Copy link
Contributor

Thanks for reporting this. Is this a query that you wrote directly (i.e. in a route), or is this a query that Relay generated (i.e. from a setVariables call)? If the latter, how was the node originally fetched, and what was the type of its id field?

@F21
Copy link
Author

F21 commented Feb 15, 2016

That query is generated by relay.

This is the original query (generated using routes and fragments by relay):

{
   "query":"query App{resources{id,...F0}} fragment F0 on Resources{memory{total,used,when,id},cpu{total,used,when,id},disk{total,used,when,id},id}",
   "variables":{

   }
}

I then use this.props.relay.forceRefetch(...) to force relay to poll the server for an update, which results in this query being generated:

{
   "query":"query Overview($id_0:ID){node(id:$id_0){id,__typename,...F0}} fragment F0 on Resources{memory{total,used,when,id},cpu{total,used,when,id},disk{total,used,when,id},id}",
   "variables":{
      "id_0":"cmVzb3VyY2VzOnJlc291cmNlcw=="
   }
}

The type (I am using the golang version of graphql + relay) is a string.

@albertstill
Copy link
Contributor

Had the exact same problem today at my company for Innovation Day. Relay was generating the query when I called setVariables to increase the count of a first argument in a connection field. The original query that worked fine and was using a custom non-node ID as it's argument property(id: "XY-12341-GA") that returned the Property type which implements the Node interface. The subsequent query used the node field with it's GUID. I used the graphql-relay helper functions and it's a graphql-js server.

Hack I did to get it to work for demo day tomoz was to remove the non null wrapping on the ID argument of node:

nodeField.args.id.type = GraphQLID;

And here is the query that errored:

query Properties_PropertyIDRelayQL($id_0: ID, $category_1: PropertyIDCategoryEnum!) {
  node(id: $id_0) {
    ...F1
  }
}

fragment F0 on PropertyID {
  bedrooms
  bathrooms
  id
}

fragment F1 on PropertyID {
  _similarListings442kDu: similarListings(category: $category_1, after: "YXJyYXljb25uZWN0aW9uOjQ=", first: 5) {
    edges {
      node {
        id
        ...F0
      }
      cursor
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
  }
  id
}

Adding a ! to the end of $id_0: ID in GraphiQL meant the query worked.

Side note: Loving GraphQL and Relay the more I use it, keep up the good work!

@josephsavona
Copy link
Contributor

@F21 @albertstill thanks for the extra information, I see what's going on. This is an inadvertent side-effect of d55b04e, which explicitly set all query metadata including the node field's argument type. The printer sees this type and prints it as-is - i'll change the type to ID!.

@josephsavona
Copy link
Contributor

This is fixed and master and will go out in the next release. Thanks everyone for the examples and repro steps!

This issue was closed.
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

3 participants