|
| 1 | +package graphql.scalars.alias; |
| 2 | + |
| 3 | +import graphql.Assert; |
| 4 | +import graphql.Internal; |
| 5 | +import graphql.schema.Coercing; |
| 6 | +import graphql.schema.CoercingParseLiteralException; |
| 7 | +import graphql.schema.CoercingParseValueException; |
| 8 | +import graphql.schema.CoercingSerializeException; |
| 9 | +import graphql.schema.GraphQLScalarType; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * Access this via {@link graphql.scalars.ExtendedScalars#newAliasedScalar(String)} |
| 15 | + */ |
| 16 | +@Internal |
| 17 | +public class AliasedScalar extends GraphQLScalarType { |
| 18 | + |
| 19 | + /** |
| 20 | + * A builder for {@link graphql.scalars.alias.AliasedScalar} |
| 21 | + */ |
| 22 | + public static class Builder { |
| 23 | + private String name; |
| 24 | + private String description; |
| 25 | + private GraphQLScalarType aliasedScalar; |
| 26 | + |
| 27 | + /** |
| 28 | + * Sets the name of the aliased scalar |
| 29 | + * |
| 30 | + * @param name the name of the aliased scalar |
| 31 | + * |
| 32 | + * @return this builder |
| 33 | + */ |
| 34 | + public Builder name(String name) { |
| 35 | + this.name = name; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Sets the description of the aliased scalar |
| 41 | + * |
| 42 | + * @param description the description of the aliased scalar |
| 43 | + * |
| 44 | + * @return this builder |
| 45 | + */ |
| 46 | + public Builder description(String description) { |
| 47 | + this.description = description; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sets the scalar that is to be aliased |
| 53 | + * |
| 54 | + * @param aliasedScalar the scalar that is to be aliased |
| 55 | + * |
| 56 | + * @return this builder |
| 57 | + */ |
| 58 | + public Builder aliasedScalar(GraphQLScalarType aliasedScalar) { |
| 59 | + this.aliasedScalar = aliasedScalar; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return the built {@link AliasedScalar} |
| 65 | + */ |
| 66 | + public AliasedScalar build() { |
| 67 | + Assert.assertNotNull(name); |
| 68 | + return aliasedScalarImpl(name, description, aliasedScalar); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private AliasedScalar(String name, String description, Coercing coercing) { |
| 74 | + super(name, description, coercing); |
| 75 | + } |
| 76 | + |
| 77 | + private static AliasedScalar aliasedScalarImpl(String name, String description, GraphQLScalarType aliasedScalar) { |
| 78 | + Assert.assertNotNull(aliasedScalar); |
| 79 | + return new AliasedScalar(name, description, new Coercing<Object, Object>() { |
| 80 | + @Override |
| 81 | + public Object serialize(Object input) throws CoercingSerializeException { |
| 82 | + return aliasedScalar.getCoercing().serialize(input); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Object parseValue(Object input) throws CoercingParseValueException { |
| 87 | + return aliasedScalar.getCoercing().parseValue(input); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Object parseLiteral(Object input) throws CoercingParseLiteralException { |
| 92 | + return aliasedScalar.getCoercing().parseLiteral(input); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException { |
| 97 | + return aliasedScalar.getCoercing().parseLiteral(input, variables); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
0 commit comments