@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller == null) { jsonWriter.nullValue(); } else { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } }
@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 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"); }
@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 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"); }
@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"); }
@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 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(); } }
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(); } }
@Override public void writeObject(InputFieldMarshaller marshaller) throws IOException { if (marshaller != null) { jsonWriter.beginObject(); marshaller.marshal(new InputFieldJsonWriter(jsonWriter, scalarTypeAdapters)); jsonWriter.endObject(); } } }
@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()); }
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()); }