Created
April 1, 2020 13:39
-
-
Save marcus-nl/9e9e7d660d99aad9f9eb53ab6ac5327f to your computer and use it in GitHub Desktop.
Implementation of JSON Scalar type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xlrit.gears.engine.graphql.scalar; | |
import graphql.Assert; | |
import graphql.language.ArrayValue; | |
import graphql.language.BooleanValue; | |
import graphql.language.EnumValue; | |
import graphql.language.FloatValue; | |
import graphql.language.IntValue; | |
import graphql.language.NullValue; | |
import graphql.language.ObjectField; | |
import graphql.language.ObjectValue; | |
import graphql.language.StringValue; | |
import graphql.language.Value; | |
import graphql.language.VariableReference; | |
import graphql.schema.Coercing; | |
import graphql.schema.CoercingParseLiteralException; | |
import graphql.schema.CoercingParseValueException; | |
import graphql.schema.CoercingSerializeException; | |
import graphql.schema.GraphQLScalarType; | |
import java.util.Collections; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class JsonScalar extends GraphQLScalarType { | |
public JsonScalar() { | |
this("JSON", "A JSON scalar"); | |
} | |
@SuppressWarnings("deprecation") | |
JsonScalar(String name, String description) { | |
super(name, description, new JsonScalarCoercing()); | |
} | |
private static String typeName(Object obj) { | |
return obj == null ? "null" : obj.getClass().getSimpleName(); | |
} | |
private static class JsonScalarCoercing implements Coercing<Object, Object> { | |
@Override | |
public Object serialize(Object input) throws CoercingSerializeException { | |
return input; | |
} | |
@Override | |
public Object parseValue(Object input) throws CoercingParseValueException { | |
return input; | |
} | |
@Override | |
public Object parseLiteral(Object input) throws CoercingParseLiteralException { | |
return parseLiteral(input, Collections.emptyMap()); | |
} | |
@Override | |
public Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException { | |
if (!(input instanceof Value)) { | |
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'."); | |
} | |
if (input instanceof NullValue) { | |
return null; | |
} | |
if (input instanceof FloatValue) { | |
return ((FloatValue) input).getValue(); | |
} | |
if (input instanceof StringValue) { | |
return ((StringValue) input).getValue(); | |
} | |
if (input instanceof IntValue) { | |
return ((IntValue) input).getValue(); | |
} | |
if (input instanceof BooleanValue) { | |
return ((BooleanValue) input).isValue(); | |
} | |
if (input instanceof EnumValue) { | |
return ((EnumValue) input).getName(); | |
} | |
if (input instanceof VariableReference) { | |
String varName = ((VariableReference) input).getName(); | |
return variables.get(varName); | |
} | |
if (input instanceof ArrayValue) { | |
List<Value> values = ((ArrayValue) input).getValues(); | |
return values.stream() | |
.map(v -> parseLiteral(v, variables)) | |
.collect(Collectors.toList()); | |
} | |
if (input instanceof ObjectValue) { | |
List<ObjectField> values = ((ObjectValue) input).getObjectFields(); | |
Map<String, Object> parsedValues = new LinkedHashMap<>(); | |
values.forEach(fld -> { | |
Object parsedValue = parseLiteral(fld.getValue(), variables); | |
parsedValues.put(fld.getName(), parsedValue); | |
}); | |
return parsedValues; | |
} | |
return Assert.assertShouldNeverHappen("We have covered all Value types"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment