|
| 1 | +package graphql.scalars.id; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.language.StringValue; |
| 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.time.DateTimeException; |
| 12 | +import java.time.format.DateTimeFormatter; |
| 13 | +import java.util.UUID; |
| 14 | + |
| 15 | +import static graphql.scalars.util.Kit.typeName; |
| 16 | + |
| 17 | +/** |
| 18 | + * Access this via {@link graphql.scalars.ExtendedScalars#UUID} |
| 19 | + */ |
| 20 | +@Internal |
| 21 | +public class UUIDScalar extends GraphQLScalarType { |
| 22 | + |
| 23 | + public UUIDScalar() { |
| 24 | + super("UUID", "A universally unique identifier compliant UUID Scalar", new Coercing<UUID, String>() { |
| 25 | + @Override |
| 26 | + public String serialize(Object input) throws CoercingSerializeException { |
| 27 | + if (input instanceof String) { |
| 28 | + try { |
| 29 | + return (UUID.fromString((String)input)).toString(); |
| 30 | + } catch ( IllegalArgumentException ex) { |
| 31 | + throw new CoercingSerializeException( |
| 32 | + "Expected a UUID value that can be converted : '" + ex.getMessage() + "'." |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | + else if(input instanceof UUID) { |
| 37 | + return input.toString(); |
| 38 | + } |
| 39 | + else { |
| 40 | + throw new CoercingSerializeException( |
| 41 | + "Expected something we can convert to 'java.util.UUID' but was '" + typeName(input) + "'." |
| 42 | + ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public UUID parseValue(Object input) throws CoercingParseValueException { |
| 48 | + if(input instanceof String) { |
| 49 | + try { |
| 50 | + return UUID.fromString((String) input); |
| 51 | + } catch (IllegalArgumentException ex) { |
| 52 | + throw new CoercingParseValueException( |
| 53 | + "Expected a 'String' of UUID type but was '" + typeName(input) + "'." |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + else if(input instanceof UUID) { |
| 58 | + return (UUID) input; |
| 59 | + } |
| 60 | + else { |
| 61 | + throw new CoercingParseValueException( |
| 62 | + "Expected a 'String' or 'UUID' type but was '" + typeName(input) + "'." |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public UUID parseLiteral(Object input) throws CoercingParseLiteralException { |
| 69 | + if (!(input instanceof StringValue)) { |
| 70 | + throw new CoercingParseLiteralException( |
| 71 | + "Expected a 'java.util.UUID' AST type object but was '" + typeName(input) + "'." |
| 72 | + ); |
| 73 | + } |
| 74 | + try { |
| 75 | + return UUID.fromString(((StringValue) input).getValue()); |
| 76 | + } catch (IllegalArgumentException ex) { |
| 77 | + throw new CoercingParseLiteralException( |
| 78 | + "Expected something that we can convert to a UUID but was invalid" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments