Add support for GraphQL#70
Conversation
cortinico
left a comment
There was a problem hiding this comment.
Would you be able to provide a sample public endpoint to include inside the Sample app?
| val content = io.readFromBuffer(buffer, charset, maxContentLength) | ||
| transaction.requestBody = content | ||
| transaction.operationName = | ||
| Gson().fromJson(content, HttpRequestBody::class.java).operationName |
There was a problem hiding this comment.
This will be really inefficient :(
You're going to recreate a Gson instance for every request.
Please use a JsonConvertor.getInstance().
| } | ||
|
|
||
| data class HttpRequestBody( | ||
| val operationName: String? |
There was a problem hiding this comment.
GSON annotation is missing here.
| private val UTF8 = Charset.forName("UTF-8") | ||
| } | ||
|
|
||
| data class HttpRequestBody( |
There was a problem hiding this comment.
Naming can be improved here:
| data class HttpRequestBody( | |
| data class GraphQLRequestBody( |
There was a problem hiding this comment.
Since the location of operation name was different depending on the library, it was absorbed using JsonParser
{
"operationName": "xxx"
}{
"queryContainer" : {
"operationName": "xxx"
}
}| Status.Failed -> " ! ! ! $method $path" | ||
| Status.Requested -> " . . . $method $path" | ||
| else -> responseCode.toString() + " " + method + " " + path | ||
| else -> responseCode.toString() + " " + method + " " + path + if (isGraphql) operationName else "" |
There was a problem hiding this comment.
Can you add a space between path and operation name?
6f5f74e to
e4f0fa5
Compare
a9e78bb to
151c570
Compare
|
@cortinico Thanks for your comments!
I added implementation using SWApi. http://graphql.org/swapi-graphql/ :) |
| val key = "operationName" | ||
| if (content.has(key)) return content.get(key).asString | ||
|
|
||
| return if (content.has("queryContainer")) { |
There was a problem hiding this comment.
Hey @rnitame
Apparently here there is a problem. GraphQL request should have the operationName at the top level and should not be wrapped inside a queryContainer object:
This is an invalid GraphQL request.
The problem is caused by the initialisation of the retrofit interface:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://graphql.org")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
Although we could fix it by using the Converter factory provided by retrofit-graphql, I think it's better if we change approach here.
Can I ask you to:
a) Don't use retrofit-graphql. Is yet another dependency that we probably don't need.
b) Just create a simple data class and do a GraphQL request without external library (as described here: https://graphql.org/learn/serving-over-http/#post-request)
c) Remove all the .graphql file?
|
Let me know when this is ready for review again :) |
dc949d9 to
f85fac9
Compare
ab298b1 to
5bbecf9
Compare
5bbecf9 to
b4814b8
Compare
| object : TypeToken<GraphQLRequestBody>() { | ||
| }.type |
There was a problem hiding this comment.
We can use this trick
// Somewhere smart in the lib
inline fun <reified T> genericType() = object: TypeToken<T>() {}.type// Usage
genericType<GraphQLRequestBody>()| operationName.setText(transaction.getOperationName()); | ||
| operationName.setVisibility(transaction.isGraphQL() ? View.VISIBLE : View.GONE); | ||
| graphql.setVisibility(transaction.isGraphQL() ? View.VISIBLE : View.GONE); |
There was a problem hiding this comment.
All those lines are about GraphQL, can do something to group them ?
if (transaction.isGraphQL()) {
operationName.setText(transaction.getOperationName());
operationName.setVisibility(View.VISIBLE);
graphql.setVisibility(View.VISIBLE);
}And set them to View.GONE by default.
| repositories { | ||
| maven { url 'https://jitpack.io' } | ||
| } | ||
|
|
There was a problem hiding this comment.
Can you add the repository in /build.gradle instead? Under allprojects.repositories.
There was a problem hiding this comment.
Actually, why do we need this repository? Can we just remove it?
There was a problem hiding this comment.
Correct, this can be removed entirely
| cache(30).enqueue(cb) | ||
| } | ||
|
|
||
| with(SampleApiService.getGraphQLInstance(getClient(this))) { |
There was a problem hiding this comment.
The method getClient doesn't only get the HTTP client, it also create it. It should be named buildHttpClient or something, it would explicitly show that something is built by this method. And maybe we shouldn't abuse of calls to this method.
| repositories { | ||
| maven { url 'https://jitpack.io' } | ||
| } | ||
|
|
There was a problem hiding this comment.
Correct, this can be removed entirely
| -dontwarn okio.** | ||
| -dontwarn retrofit2.** | ||
|
|
||
| # retrofit-graphql |
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
| <uses-sdk tools:overrideLibrary="io.github.wax911.library" /> |
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="#e535ab" |
| Call<Void> cache(@Path("seconds") int seconds); | ||
| } | ||
|
|
||
| interface GraphQLSWApi { |
There was a problem hiding this comment.
Can you move this to a separate class, like a SimpleGraphQLService or something similar
|
@rnitame I would love to proceed with Agree @cortinico @olivierperez ? |
Definitely, let's move this to |
|
@rnitame @cortinico |
Agree here. I generally like the approach in this PR by @rnitame. I believe we could reuse the UI part at least. On the other hand I believe GraphQL initialisation should be provided by the developer when creating the |
| transaction.requestContentLength = requestBody?.contentLength() ?: 0L | ||
|
|
||
| val encodingIsSupported = io.bodyHasSupportedEncoding(request.headers().get("Content-Encoding")) | ||
| val encodingIsSupported = |
There was a problem hiding this comment.
This looks like it didn't change content. Can be reverted to reduce the diff.
|
|
||
| if (requestBody != null && encodingIsSupported) { | ||
| val source = io.getNativeSource(Buffer(), io.bodyIsGzipped(request.headers().get("Content-Encoding"))) | ||
| val source = io.getNativeSource( |
|
This is such a nice feature! Anyway I can help getting that merge? I'm not the branch owner so I don't think I can push to it, but I could make a new branch with the desired changes if that's ok. Let me know! |
You can definitely send a new PR branching from this one :) |
|
@quentin41500 That is actually cool. I believe that you could cooperate with @rnitame to do a new version of PR. Also, I would like to join you in these efforts, since from what I see there were not much attention to this PR and it is quite old already, while the feature is really useful. So ping me here or in Chucker Slack channel and let's make it happen in |
|
Closing this one as we have two other PRs achieving the same 👍 Thanks for your time @rnitame |

fix #69