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

Update transaction list to differentiate GraphQL requests #848

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix HttpTransactionDaoTest.kt test
Code cleanup
  • Loading branch information
ArjanSM committed Jul 12, 2022
commit 8c581805855f0a3c24a2c2af48e473d566d1ecff
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public class ChuckerInterceptor private constructor(
this.cacheDirectoryProvider = provider
}

public fun graphQLEndpoint(graphQLEndpoint: String): Builder = apply {
/**
* Sets graphql endpoint/path to identify graphql requests.
*/
public fun registerGraphQLEndpoint(graphQLEndpoint: String): Builder = apply {
this.graphQLEndpoint = graphQLEndpoint
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class HttpTransactionDaoTest {
assertThat(stringValue("responseMessage")).isEqualTo(data.responseMessage)
assertThat(stringValue("responseBody")).isEqualTo(data.responseBody)
assertThat(stringValue("error")).isEqualTo(data.error)
assertThat(stringValue("isGraphQLRequest")).isEqualTo(data.isGraphQLRequest)
assertThat(longValue("isGraphQLRequest")).isEqualTo(if(data.isGraphQLRequest) 1 else 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.chuckerteam.chucker.sample

import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.Callback
Expand All @@ -13,7 +15,6 @@ import retrofit2.http.POST
import retrofit2.http.Query

const val GRAPHQL_BASE_URL = "https://rickandmortyapi.com/"
private const val CHARACTER_ID = 6L
class GraphQLTaskImpl(client: OkHttpClient):IGraphQLTask {
private val api = Retrofit.Builder()
.baseUrl(GRAPHQL_BASE_URL)
Expand All @@ -34,8 +35,10 @@ class GraphQLTaskImpl(client: OkHttpClient):IGraphQLTask {
getCharacterById(query, variables).enqueue(noOpCallback)
getCharacterByIdPost(
GraphQLQuery(
"query GetCharacter( \$id: ID! ){character(id:\$id) {id:id, name,status }}",
GraphQLVariables(CHARACTER_ID)
query,
variables ?. let {
JsonConverter.gsonInstance.fromJson(variables, GraphQLVariables::class.java)
} ?: GraphQLVariables()
)
).enqueue(noOpCallback)

Expand All @@ -49,6 +52,21 @@ class GraphQLTaskImpl(client: OkHttpClient):IGraphQLTask {
fun getCharacterByIdPost(@Body graphQLQuery: GraphQLQuery): Call<Any?>
}

data class GraphQLQuery(val query:String, val variables: GraphQLVariables)
data class GraphQLVariables(val id: Long)
data class GraphQLQuery(val query:String, val variables: GraphQLVariables?)
data class GraphQLVariables(
@SerializedName("id") val id: Long? = null
)
}

const val GRAPHQL_QUERY = "query GetCharacter( \$id: ID! ){\n" +
" character(id:\$id) {\n" +
" id:id, \n" +
" \tname,\n" +
" status \n" +
" \n" +
" }\n" +
"}"

object JsonConverter {
val gsonInstance by lazy { GsonBuilder().create() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ class MainActivity : AppCompatActivity() {

doGraphql?.setOnClickListener {
for(task in graphQLTasks) {
task.run("query GetCharacter( \$id: ID! ){\n" +
" character(id:\$id) {\n" +
" id:id, \n" +
" \tname,\n" +
" status \n" +
" \n" +
" }\n" +
"}",
task.run(
GRAPHQL_QUERY,
"{\"id\":1}"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun createOkHttpClient(
.redactHeaders(emptySet())
.alwaysReadResponseBody(false)
.addBodyDecoder(PokemonProtoBodyDecoder())
.graphQLEndpoint(GRAPHQL_BASE_URL)
.registerGraphQLEndpoint(GRAPHQL_BASE_URL)
.build()

return OkHttpClient.Builder()
Expand Down