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

Add custom scalar support to client value formatter #2083

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.smallrye.graphql.client.impl.core.utils;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.UUID;

import io.smallrye.graphql.api.CustomFloatScalar;
import io.smallrye.graphql.api.CustomIntScalar;
import io.smallrye.graphql.api.CustomStringScalar;
import io.smallrye.graphql.client.core.exceptions.BuildException;
import io.smallrye.graphql.client.impl.core.EnumImpl;
import io.smallrye.graphql.client.impl.core.InputObjectImpl;
import io.smallrye.graphql.client.impl.core.VariableImpl;
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.UUID;

public class ValueFormatter {

Expand Down Expand Up @@ -39,6 +41,15 @@ public static String format(Object value) throws BuildException {
return _processArray(value);
} else if (value instanceof Iterable) {
return _processIterable((Iterable<?>) value);
} else if (value instanceof CustomStringScalar) {
CustomStringScalar css = (CustomStringScalar) value;
return _getAsQuotedString(String.valueOf(css.stringValueForSerialization()));
} else if (value instanceof CustomIntScalar) {
CustomIntScalar cis = (CustomIntScalar) value;
return cis.intValueForSerialization().toString();
} else if (value instanceof CustomFloatScalar) {
CustomFloatScalar cfs = (CustomFloatScalar) value;
return cfs.floatValueForSerialization().toString();
} else {
if (assignableFrom(value.getClass(), QUOTED_VALUES)) {
return _getAsQuotedString(String.valueOf(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.smallrye.graphql.api.CustomFloatScalar;
import io.smallrye.graphql.api.CustomIntScalar;
import io.smallrye.graphql.api.CustomStringScalar;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

import org.junit.jupiter.api.Test;

class ValueFormatterTest {

@Test
public void testUnsupportedInput() {

assertThrows(IllegalStateException.class, () -> {
ValueFormatter.format(new Object());
});
assertThrows(IllegalStateException.class, () -> ValueFormatter.format(new Object()));

assertThrows(IllegalStateException.class, () -> {
ValueFormatter.format(Map.of("test", "value"));
});
assertThrows(IllegalStateException.class, () -> ValueFormatter.format(Map.of("test", "value")));

assertThrows(IllegalStateException.class, () -> {
ValueFormatter.format(new Date());
});
assertThrows(IllegalStateException.class, () -> ValueFormatter.format(new Date()));
}

@Test
Expand All @@ -34,9 +32,63 @@ public void testEnumInput() {
assertEquals(Order.ASC.toString(), value);
}

@Test
public void testCustomStringScalarInput() {

final var customString = new CustomString();
final var value = ValueFormatter.format(customString);

assertEquals("\"" + customString.stringValueForSerialization() + "\"", value);
}

@Test
public void testCustomIntScalarInput() {

final var customInt = new CustomInt();
final var value = ValueFormatter.format(customInt);

assertEquals(customInt.intValueForSerialization().toString(), value);
}

@Test
public void testCustomFloatScalarInput() {

final var customFloat = new CustomFloat();
final var value = ValueFormatter.format(customFloat);

assertEquals(customFloat.floatValueForSerialization().toString(), value);
}

enum Order {
ASC,
DESC
}

// custom string scalar
static class CustomString implements CustomStringScalar {

@Override
public String stringValueForSerialization() {
return "test value";
}
}

// custom int scalar
static class CustomInt implements CustomIntScalar {

@Override
public BigInteger intValueForSerialization() {
return BigInteger.valueOf(123);
}
}

// custom float scalar
static class CustomFloat implements CustomFloatScalar {

@Override
public BigDecimal floatValueForSerialization() {
return BigDecimal.valueOf(12.3f);
}
}

}
Loading