-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of handling custom scalar types to OperationRequest (#165)
- Loading branch information
Showing
15 changed files
with
269 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
public interface ScalarType { | ||
String typeName(); | ||
|
||
Class javaType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
apollo-compiler/src/test/graphql/com/example/custom_scalar_type/CustomTypeExpected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
package com.example.custom_scalar_type.type; | ||
|
||
import com.apollographql.android.api.graphql.ScalarType; | ||
import java.lang.Class; | ||
import java.lang.Override; | ||
import java.lang.String; | ||
import java.util.Date; | ||
import javax.annotation.Generated; | ||
|
||
@Generated("Apollo GraphQL") | ||
public enum CustomType implements ScalarType { | ||
DATE { | ||
@Override | ||
public String typeName() { | ||
return "Date"; | ||
} | ||
|
||
@Override | ||
public Class javaType() { | ||
return Date.class; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
apollo-compiler/src/test/graphql/com/example/pojo_custom_scalar_type/CustomTypeExpected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
package com.example.pojo_custom_scalar_type.type; | ||
|
||
import com.apollographql.android.api.graphql.ScalarType; | ||
import java.lang.Class; | ||
import java.lang.Override; | ||
import java.lang.String; | ||
import java.util.Date; | ||
import javax.annotation.Generated; | ||
|
||
@Generated("Apollo GraphQL") | ||
public enum CustomType implements ScalarType { | ||
DATE { | ||
@Override | ||
public String typeName() { | ||
return "Date"; | ||
} | ||
|
||
@Override | ||
public Class javaType() { | ||
return Date.class; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...jo/src/main/java/com/apollographql/android/converter/pojo/ApolloRequestBodyConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.apollographql.android.converter.pojo; | ||
|
||
import com.apollographql.android.api.graphql.Operation; | ||
import com.squareup.moshi.JsonAdapter; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.Buffer; | ||
import retrofit2.Converter; | ||
|
||
final class ApolloRequestBodyConverter implements Converter<Operation, RequestBody> { | ||
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); | ||
private final JsonAdapter<Operation> adapter; | ||
|
||
ApolloRequestBodyConverter(JsonAdapter<Operation> adapter) { | ||
this.adapter = adapter; | ||
} | ||
|
||
@Override public RequestBody convert(Operation value) throws IOException { | ||
Buffer buffer = new Buffer(); | ||
adapter.toJson(buffer, value); | ||
return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ers/pojo/src/main/java/com/apollographql/android/converter/pojo/OperationJsonAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.apollographql.android.converter.pojo; | ||
|
||
import com.apollographql.android.api.graphql.Operation; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.JsonReader; | ||
import com.squareup.moshi.JsonWriter; | ||
import com.squareup.moshi.Moshi; | ||
|
||
import java.io.IOException; | ||
|
||
final class OperationJsonAdapter extends JsonAdapter<Operation> { | ||
private final Moshi moshi; | ||
|
||
OperationJsonAdapter(Moshi moshi) { | ||
this.moshi = moshi; | ||
} | ||
|
||
@Override public Operation fromJson(JsonReader reader) throws IOException { | ||
throw new IllegalStateException("This should not be called ever."); | ||
} | ||
|
||
@Override public void toJson(JsonWriter writer, Operation value) throws IOException { | ||
writer.beginObject(); | ||
writer.name("query").value(value.queryDocument().replaceAll("\\n", "")); | ||
Operation.Variables variables = value.variables(); | ||
if (variables != null) { | ||
//noinspection unchecked | ||
JsonAdapter<Operation.Variables> adapter = | ||
(JsonAdapter<Operation.Variables>) moshi.adapter(variables.getClass()); | ||
writer.name("variables"); | ||
adapter.toJson(writer, variables); | ||
} | ||
writer.endObject(); | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
...verters/pojo/src/main/java/com/apollographql/android/converter/pojo/OperationRequest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.