Skip to content

Commit b5f9ab7

Browse files
committed
Added regex support and positive and negative numbers
1 parent c548f50 commit b5f9ab7

18 files changed

+789
-0
lines changed

src/main/java/graphql/scalars/ExtendedScalars.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
import graphql.scalars.datetime.DateTimeScalar;
55
import graphql.scalars.datetime.FullDateScalar;
66
import graphql.scalars.datetime.FullTimeScalar;
7+
import graphql.scalars.numbers.NegativeFloatScalar;
8+
import graphql.scalars.numbers.NegativeIntScalar;
9+
import graphql.scalars.numbers.NonNegativeFloatScalar;
10+
import graphql.scalars.numbers.NonNegativeIntScalar;
11+
import graphql.scalars.numbers.NonPositiveFloatScalar;
12+
import graphql.scalars.numbers.NonPositiveIntScalar;
13+
import graphql.scalars.numbers.PositiveFloatScalar;
14+
import graphql.scalars.numbers.PositiveIntScalar;
715
import graphql.scalars.object.JsonScalar;
816
import graphql.scalars.object.ObjectScalar;
917
import graphql.scalars.url.UrlScalar;
@@ -58,4 +66,16 @@ public class ExtendedScalars {
5866
public static GraphQLScalarType Json = new JsonScalar();
5967

6068
public static GraphQLScalarType Url = new UrlScalar();
69+
70+
public static GraphQLScalarType PositiveInt = new PositiveIntScalar();
71+
public static GraphQLScalarType NegativeInt = new NegativeIntScalar();
72+
public static GraphQLScalarType NonPositiveInt = new NonPositiveIntScalar();
73+
public static GraphQLScalarType NonNegativeInt = new NonNegativeIntScalar();
74+
75+
public static GraphQLScalarType PositiveFloat = new PositiveFloatScalar();
76+
public static GraphQLScalarType NegativeFloat = new NegativeFloatScalar();
77+
public static GraphQLScalarType NonPositiveFloat = new NonPositiveFloatScalar();
78+
public static GraphQLScalarType NonNegativeFloat = new NonNegativeFloatScalar();
79+
80+
6181
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.Coercing;
4+
import graphql.schema.CoercingParseLiteralException;
5+
import graphql.schema.CoercingParseValueException;
6+
import graphql.schema.CoercingSerializeException;
7+
8+
import java.util.function.Function;
9+
10+
import static graphql.Scalars.GraphQLFloat;
11+
12+
abstract class FloatCoercing implements Coercing<Double, Double> {
13+
14+
abstract protected Double check(Double d, Function<String, RuntimeException> exceptionMaker);
15+
16+
@Override
17+
public Double serialize(Object input) throws CoercingSerializeException {
18+
Double d = (Double) GraphQLFloat.getCoercing().serialize(input);
19+
return check(d, CoercingSerializeException::new);
20+
}
21+
22+
@Override
23+
public Double parseValue(Object input) throws CoercingParseValueException {
24+
Double d = (Double) GraphQLFloat.getCoercing().parseValue(input);
25+
return check(d, CoercingParseValueException::new);
26+
}
27+
28+
@Override
29+
public Double parseLiteral(Object input) throws CoercingParseLiteralException {
30+
Double d = (Double) GraphQLFloat.getCoercing().parseLiteral(input);
31+
return check(d, CoercingParseLiteralException::new);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.Coercing;
4+
import graphql.schema.CoercingParseLiteralException;
5+
import graphql.schema.CoercingParseValueException;
6+
import graphql.schema.CoercingSerializeException;
7+
8+
import java.util.function.Function;
9+
10+
import static graphql.Scalars.GraphQLInt;
11+
12+
abstract class IntCoercing implements Coercing<Integer, Integer> {
13+
14+
abstract protected Integer check(Integer i, Function<String, RuntimeException> exceptionMaker);
15+
16+
@Override
17+
public Integer serialize(Object input) throws CoercingSerializeException {
18+
Integer i = (Integer) GraphQLInt.getCoercing().serialize(input);
19+
return check(i, CoercingSerializeException::new);
20+
}
21+
22+
@Override
23+
public Integer parseValue(Object input) throws CoercingParseValueException {
24+
Integer i = (Integer) GraphQLInt.getCoercing().parseValue(input);
25+
return check(i, CoercingParseValueException::new);
26+
}
27+
28+
@Override
29+
public Integer parseLiteral(Object input) throws CoercingParseLiteralException {
30+
Integer i = (Integer) GraphQLInt.getCoercing().parseLiteral(input);
31+
return check(i, CoercingParseLiteralException::new);
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NegativeFloatScalar extends GraphQLScalarType {
8+
public NegativeFloatScalar() {
9+
super("NegativeFloat", "An Float scalar that must be a negative value", new FloatCoercing() {
10+
@Override
11+
protected Double check(Double d, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(d < 0)) {
13+
throw exceptionMaker.apply("The value must be a negative value");
14+
}
15+
return d;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NegativeIntScalar extends GraphQLScalarType {
8+
public NegativeIntScalar() {
9+
super("NegativeInt", "An Int scalar that must be a negative value", new IntCoercing() {
10+
@Override
11+
protected Integer check(Integer i, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(i < 0)) {
13+
throw exceptionMaker.apply("The value must be a negative integer");
14+
}
15+
return i;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NonNegativeFloatScalar extends GraphQLScalarType {
8+
public NonNegativeFloatScalar() {
9+
super("NonNegativeFloat", "An Float scalar that must be greater than or equal to zero", new FloatCoercing() {
10+
@Override
11+
protected Double check(Double d, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(d >= 0)) {
13+
throw exceptionMaker.apply("The value must be greater than or equal to zero");
14+
}
15+
return d;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NonNegativeIntScalar extends GraphQLScalarType {
8+
public NonNegativeIntScalar() {
9+
super("NonNegativeInt", "An Int scalar that must be greater than or equal to zero", new IntCoercing() {
10+
@Override
11+
protected Integer check(Integer i, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(i >= 0)) {
13+
throw exceptionMaker.apply("The value must be greater than or equal to zero");
14+
}
15+
return i;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NonPositiveFloatScalar extends GraphQLScalarType {
8+
public NonPositiveFloatScalar() {
9+
super("NonPositiveFloat", "An Float scalar that must be less than or equal to zero", new FloatCoercing() {
10+
@Override
11+
protected Double check(Double d, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(d <= 0)) {
13+
throw exceptionMaker.apply("The value must be less than or equal to zero");
14+
}
15+
return d;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class NonPositiveIntScalar extends GraphQLScalarType {
8+
public NonPositiveIntScalar() {
9+
super("NonPositiveInt", "An Int scalar that must be less than or equal to zero", new IntCoercing() {
10+
@Override
11+
protected Integer check(Integer i, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(i <= 0)) {
13+
throw exceptionMaker.apply("The value must be less than or equal to zero");
14+
}
15+
return i;
16+
}
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.scalars.numbers;
2+
3+
import graphql.schema.GraphQLScalarType;
4+
5+
import java.util.function.Function;
6+
7+
public class PositiveFloatScalar extends GraphQLScalarType {
8+
public PositiveFloatScalar() {
9+
super("PositiveFloat", "An Float scalar that must be a positive value", new FloatCoercing() {
10+
@Override
11+
protected Double check(Double d, Function<String, RuntimeException> exceptionMaker) {
12+
if (!(d > 0)) {
13+
throw exceptionMaker.apply("The value must be a positive value");
14+
}
15+
return d;
16+
}
17+
});
18+
}
19+
}

0 commit comments

Comments
 (0)