@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller == null) { jsonWriter.nullValue(); } else { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } }
@SuppressWarnings("unchecked") @Override public void writeCustom(@NotNull String fieldName, ScalarType scalarType, Object value) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (value != null) { CustomTypeAdapter customTypeAdapter = scalarTypeAdapters.adapterFor(scalarType); CustomTypeValue customTypeValue = customTypeAdapter.encode(value); if (customTypeValue instanceof CustomTypeValue.GraphQLString) { writeString(fieldName, ((CustomTypeValue.GraphQLString) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLBoolean) { writeBoolean(fieldName, ((CustomTypeValue.GraphQLBoolean) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLNumber) { writeNumber(fieldName, ((CustomTypeValue.GraphQLNumber) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLJsonString) { writeString(fieldName, ((CustomTypeValue.GraphQLJsonString) customTypeValue).value); } else if (customTypeValue instanceof CustomTypeValue.GraphQLJson) { writeMap(fieldName, ((CustomTypeValue.GraphQLJson) customTypeValue).value); } else { throw new IllegalArgumentException("Unsupported custom value type: " + customTypeValue); } } else { jsonWriter.name(fieldName).nullValue(); } }
@Test public void writeCustomNumber() throws IOException { Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>(); customTypeAdapters.put(new MockCustomScalarType(CustomTypeValue.GraphQLNumber.class), new MockCustomTypeAdapter() { @NotNull @Override public CustomTypeValue encode(@NotNull Object value) { return new CustomTypeValue.GraphQLNumber((Number) value); } }); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(customTypeAdapters)); inputFieldJsonWriter.writeCustom("someField", new MockCustomScalarType(CustomTypeValue.GraphQLNumber.class), BigDecimal.valueOf(100.1)); inputFieldJsonWriter.writeCustom("someNullField", new MockCustomScalarType(CustomTypeValue.GraphQLNumber.class), null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":100.1,\"someNullField\":null"); }
@Test public void writeString() throws IOException { inputFieldJsonWriter.writeString("someField", "someValue"); inputFieldJsonWriter.writeString("someNullField", null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":\"someValue\",\"someNullField\":null"); }
@Test public void writeNumber() throws IOException { inputFieldJsonWriter.writeNumber("someField", BigDecimal.valueOf(1.001)); inputFieldJsonWriter.writeNumber("someNullField", null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":1.001,\"someNullField\":null"); }
@Test public void writeBoolean() throws IOException { inputFieldJsonWriter.writeBoolean("someField", true); inputFieldJsonWriter.writeBoolean("someNullField", null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":true,\"someNullField\":null"); }
@Test public void writeDouble() throws IOException { inputFieldJsonWriter.writeDouble("someField", 1.01); inputFieldJsonWriter.writeDouble("someNullField", null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":1.01,\"someNullField\":null"); }
@Override public void writeCustom(@Nonnull String fieldName, ScalarType scalarType, Object value) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (value != null) { CustomTypeAdapter customTypeAdapter = scalarTypeAdapters.adapterFor(scalarType); writeString(fieldName, customTypeAdapter.encode(value)); } else { writeString(fieldName, null); } }
@Before public void setUp() throws IOException { jsonBuffer = new Buffer(); jsonWriter = JsonWriter.of(jsonBuffer); jsonWriter.setSerializeNulls(true); jsonWriter.beginObject(); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(Collections.<ScalarType, CustomTypeAdapter>emptyMap())); }
@Test public void writeCustomString() throws IOException { Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>(); customTypeAdapters.put(new MockCustomScalarType(CustomTypeValue.GraphQLString.class), new MockCustomTypeAdapter() { @NotNull @Override public CustomTypeValue encode(@NotNull Object value) { return new CustomTypeValue.GraphQLString((String) value); } }); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(customTypeAdapters)); inputFieldJsonWriter.writeCustom("someField", new MockCustomScalarType(CustomTypeValue.GraphQLString.class), "someValue"); inputFieldJsonWriter.writeCustom("someNullField", new MockCustomScalarType(CustomTypeValue.GraphQLString.class), null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":\"someValue\",\"someNullField\":null"); }
@Override public void writeCustom(@Nonnull String fieldName, ScalarType scalarType, Object value) throws IOException { checkNotNull(fieldName, "fieldName == null"); if (value != null) { CustomTypeAdapter customTypeAdapter = scalarTypeAdapters.adapterFor(scalarType); writeString(fieldName, customTypeAdapter.encode(value)); } else { writeString(fieldName, null); } }
@Override public void writeToJson(@NotNull JsonWriter writer) throws IOException { checkNotNull(writer, "writer == null"); writer.name(JSON_KEY_ID).value(subscriptionId); writer.name(JSON_KEY_TYPE).value(TYPE); writer.name(JSON_KEY_PAYLOAD).beginObject(); writer.name(JSON_KEY_QUERY).value(subscription.queryDocument().replaceAll("\\n", "")); writer.name(JSON_KEY_VARIABLES).beginObject(); subscription.variables().marshaller().marshal(new InputFieldJsonWriter(writer, scalarTypeAdapters)); writer.endObject(); writer.name(JSON_KEY_OPERATION_NAME).value(subscription.name().name()); writer.endObject(); } }
@Test public void writeCustomBoolean() throws IOException { Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>(); customTypeAdapters.put(new MockCustomScalarType(CustomTypeValue.GraphQLBoolean.class), new MockCustomTypeAdapter() { @NotNull @Override public CustomTypeValue encode(@NotNull Object value) { return new CustomTypeValue.GraphQLBoolean((Boolean) value); } }); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(customTypeAdapters)); inputFieldJsonWriter.writeCustom("someField", new MockCustomScalarType(CustomTypeValue.GraphQLBoolean.class), true); inputFieldJsonWriter.writeCustom("someNullField", new MockCustomScalarType(CustomTypeValue.GraphQLBoolean.class), null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":true,\"someNullField\":null"); }
static ByteString httpRequestBody(Operation operation, ScalarTypeAdapters scalarTypeAdapters, boolean writeQueryDocument) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.setSerializeNulls(true); jsonWriter.beginObject(); jsonWriter.name("operationName").value(operation.name().name()); jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.name("extensions") .beginObject() .name("persistedQuery") .beginObject() .name("version").value(1) .name("sha256Hash").value(operation.operationId()) .endObject() .endObject(); if (writeQueryDocument) { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.endObject(); jsonWriter.close(); return buffer.readByteString(); } }
@Test public void writeCustomJsonString() throws IOException { Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>(); customTypeAdapters.put(new MockCustomScalarType(CustomTypeValue.GraphQLJsonString.class), new MockCustomTypeAdapter() { @NotNull @Override public CustomTypeValue encode(@NotNull Object value) { return new CustomTypeValue.GraphQLJsonString((String) value); } }); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(customTypeAdapters)); inputFieldJsonWriter.writeCustom("someField", new MockCustomScalarType(CustomTypeValue.GraphQLJsonString.class), "{\"someField\": \"someValue\"}"); inputFieldJsonWriter.writeCustom("someNullField", new MockCustomScalarType(CustomTypeValue.GraphQLJsonString.class), null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":\"{\\\"someField\\\": \\\"someValue\\\"}\",\"someNullField\":null"); }
@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller != null) { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } } }
@Test public void writeCustomJson() throws IOException { Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>(); customTypeAdapters.put(new MockCustomScalarType(CustomTypeValue.GraphQLJson.class), new MockCustomTypeAdapter() { @NotNull @Override public CustomTypeValue encode(@NotNull Object value) { return new CustomTypeValue.GraphQLJson((Map<String, Object>) value); } }); inputFieldJsonWriter = new InputFieldJsonWriter(jsonWriter, new ScalarTypeAdapters(customTypeAdapters)); Map<String, Object> objectMap = new LinkedHashMap<>(); objectMap.put("booleanField", true); objectMap.put("stringField", "someValue"); objectMap.put("numberField", 100); objectMap.put("objectField", new UnmodifiableMapBuilder().put("someField", "someValue").build()); inputFieldJsonWriter.writeCustom("someField", new MockCustomScalarType(CustomTypeValue.GraphQLJson.class), objectMap); inputFieldJsonWriter.writeCustom("someNullField", new MockCustomScalarType(CustomTypeValue.GraphQLJson.class), null); assertThat(jsonBuffer.readUtf8()).isEqualTo("{\"someField\":{\"booleanField\":true,\"stringField\":\"someValue\",\"numberField\":100,\"objectField\":{\"someField\":\"someValue\"}},\"someNullField\":null"); }
@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller != null) { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } } }
private String httpRequestBody(Operation operation) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.beginObject(); jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.endObject(); jsonWriter.close(); return buffer.readUtf8(); }
private RequestBody httpRequestBody(Operation operation) throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); jsonWriter.beginObject(); if (sendOperationIdentifiers) { jsonWriter.name("id").value(operation.operationId()); } else { jsonWriter.name("query").value(operation.queryDocument().replaceAll("\\n", "")); } jsonWriter.name("variables").beginObject(); operation.variables().marshaller().marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); jsonWriter.endObject(); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); }