https://kotlinlang.org logo
Title
s

Sam Michael

03/23/2022, 8:30 AM
Have a question about debugging query hash mismatches. I keep registering my client queries to be safe listed operations but when my client makes the call, they are getting treated as forbidden / 400 response. Tried updating to latest 3.1.0 , but so far no luck. According to our graphql platform team the iOS faced similar difficulties before upgrading their apollo codegen library, but android doesn't seem to have a separate codegen library, thanks in advance, been trying to get data back for my query in app but it only works in playground for a few weeks.🤕
:kotlin-intensifies: 1
b

bod

03/23/2022, 8:35 AM
Hi! To be sure, are you using the Gradle task to do the registration?
:thank-you: 1
s

Sam Michael

03/23/2022, 8:37 AM
No i don't have Apollo key, so i am using a homemade npm cli tool which wraps over the Apollo command line interface i believe
Our graphql team suggested to use this cli tool, long-term would like to look into setting up the Gradle task
Do you think the Gradle codegen version might produce different results from cli version?
b

bod

03/23/2022, 8:41 AM
That is probably the issue because the hashing used to create the operation id is done on the exact (or almost exact, if I remember correctly 😛) query. Since the Apollo Kotlin codegen sometimes manipulates your query text (e.g. to add
__typename
), they may differ.
1
:thank-you: 1
:kotlin-intensifies: 1
s

Sam Michael

03/23/2022, 8:49 AM
Big thanks 💗, also i may try preemptively adding __typename lol, at this point just having more stuff to try and troubleshoot is a godsend!
👍 1
b

bod

03/23/2022, 8:58 AM
maybe this option can also help you: it outputs in a single json file all the operation texts they are sent by the client
w

wasyl

03/23/2022, 2:40 PM
i may try preemptively adding __typename lol
In 3.x not every type has
__typename
though. What I tend to do is open the relevant
SomeOperationQuery
generated file, copy the entire string from
QUERY_DOCUMENT
(including
trimMargin()
but not including
QueryDocumentMinifier.minify
) and run
pbpaste | kotlinc-jvm | tail -n +3 | sed '$d' | sed 's/^.*kotlin.String = //g' | pbcopy
in the terminal. That will run the code as a Kotlin script, do some string magic to only get the output and copy it back to the clibpoard. Very useful, although that’s for mac obviously. You can also get the
QUERY_DOCUMENT
string and run it in a Kotlin scratch file in Android Studio and copy the output
:mind-blown: 3
:today-i-learned: 2
:thank-you: 1
e

ephemient

03/23/2022, 4:08 PM
generateOperationOutput should be exactly what you want to register with, as it matches what Apollo will be sending after inlining fragments and minimizing etc.
🙌 1
s

Sam Michael

03/24/2022, 1:37 PM
adding
__typename
field did the trick after all in this instance 😂 , but looking into all the suggestions above for more longterm solutions.
pbpaste | kotlinc-jvm | tail -n +3 | sed '$d' | sed 's/^.*kotlin.String = //g' | pbcopy
1. For this command, so the idea is to take the output it copies to the clipboard and register that as your operation? Sorry had a little trouble following 2. I tried turning on generateOperationOutput but haven't been able to locate the json file or where it is outputting to. 😣 Apologies for being a little dense
b

bod

03/24/2022, 1:54 PM
for 2. it should be in
build/generated/operationOutput/apollo/service/operationOutput.json
:thank-you: 1
w

wasyl

03/24/2022, 1:56 PM
For this command, so the idea is to take the output it copies to the clipboard and register that as your operation?
I don’t register the operations, I was just sharing my way of getting the exact query (functionally) that Apollo will make
🙌 1
s

Sam Michael

03/24/2022, 1:57 PM
Cool, thanks all, makes sense
Sorry one more think Łukasz, where do i put the string in relation to the command, do I replace a part of the command or put it after?
pbpaste | kotlinc-jvm | tail -n +3 | sed '$d' | sed 's/^.*kotlin.String = //g' | pbcopy "query SomeQuery ..."
like this?
w

wasyl

03/24/2022, 2:13 PM
the
pbpaste
will paste the string from the clipboard, and
pbcopy
will copy it back. So this works when you copy the query string from the
SomeQuery
file to the clipboard, run the command I pasted (without changes, and provided you have kotlinc-jvm in your path), and it’ll put the cleaned up query in your clipboard
If any of those are not convenient, then I suggest just pasting the query string into the Kotlin scratch file in the IDE
s

Sam Michael

03/24/2022, 2:14 PM
Ahh ok gotcha now, don't know why I was missing that before