1

Issue: We goto 'on' in the query, while build apollo is complaining about the on keyword in the query (*.graphql)

GraphQL query:

query TimeLine($inputData: InputData!) {
    getTimeLine(inputData: $inputData) {
        on
        updated
 }
}

Error: Unsupported token on (com.apollographql.apollo.compiler.parser.GraphQLDocumentParseException).

Env: Kotlin, apolloGraphQLVersion: "1.3.2"

Amit
  • 229
  • 5
  • 20
  • how are you providing the schema – Sajeetharan Sep 27 '22 at 16:15
  • I took schema from server and saved it in schema.json. Here is the schema for the `on` field. The schema is fine, as it's working fine for other fields. ```{ "name": "on", "args": [], "type": { "kind": "NON_NULL", "name": null, "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } }, "isDeprecated": false, "deprecationReason": null }``` – Amit Sep 27 '22 at 16:29

1 Answers1

0

This happens because the on keyword is a reserved keyword in GraphQL.

One of the Type Conditions is on NamedType, see the official spec file of GraphQL.

query FragmentTyping {
  profiles(handles: ["zuck", "cocacola"]) {
    handle
    ...userFragment
    ...pageFragment
  }
}

fragment userFragment on User {
  friends {
    count
  }
}

fragment pageFragment on Page {
  likers {
    count
  }
}

See the on used in fragment userFragment on User? Your GraphQL got confused because you are using on as a field within the query, while it expects to be a fragment. Read more about fragments here. Also, a fragment's name can be anything, except for on, see the official spec file.

One way to solve this issue might be to rename the field in your query, but I am not sure if GraphQL will complain about this approach as well:

query TimeLine($inputData: InputData!) {
    getTimeLine(inputData: $inputData) {
        dataOn: on
        updated
 }
}
Roy
  • 7,811
  • 4
  • 24
  • 47
  • I tried this, but it didn't work. It's giving same error, i.e., Unsupported token `on` ........ dataOn: on – Amit Sep 27 '22 at 16:20
  • @Amit Can you change your server part? – Roy Sep 27 '22 at 17:04
  • Unfortunately, changing this field name is not a viable option. We need to put some hacks on the client side only. We're a client and server sid eis owned by a different team. – Amit Sep 27 '22 at 17:29