lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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<Long, String> {
 
    @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");
    }
}