package com.rongyichuang.config; import graphql.language.StringValue; import graphql.schema.Coercing; import graphql.schema.CoercingParseLiteralException; import graphql.schema.CoercingParseValueException; import graphql.schema.CoercingSerializeException; import java.util.Objects; public class LongCoercing implements Coercing { @Override public String serialize(Object dataFetcherResult) throws CoercingSerializeException { if (dataFetcherResult instanceof Long) { return dataFetcherResult.toString(); } throw new CoercingSerializeException("Not a valid Long"); } @Override public Long parseValue(Object input) throws CoercingParseValueException { try { if (input instanceof String) { return Long.parseLong((String) input); } if (input instanceof Number) { return ((Number) input).longValue(); } throw new CoercingParseValueException("Not a valid Long"); } catch (NumberFormatException e) { throw new CoercingParseValueException("Not a valid Long", e); } } @Override public Long parseLiteral(Object input) throws CoercingParseLiteralException { if (input instanceof StringValue) { try { return Long.parseLong(((StringValue) input).getValue()); } catch (NumberFormatException e) { throw new CoercingParseLiteralException("Not a valid Long", e); } } throw new CoercingParseLiteralException("Not a valid Long"); } }