All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.demographql.helloworld.TestQueries Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package org.demographql.helloworld;

import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.kickstart.spring.error.ErrorContext;
import graphql.kickstart.tools.GraphQLQueryResolver;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static graphql.ErrorClassification.errorClassification;

@Service
public class TestQueries implements GraphQLQueryResolver {
    public Integer intValue(Integer val) {
        return val;
    }
    public Long longValue(Long val) {
        return val;
    }
    public Float floatValue(Float val) { return val; }
    public Double doubleValue(Double val) { return val; }
    public BigDecimal bigDecimalValue(BigDecimal val) { return val; }
    public String stringValue(String val) {
        return val;
    }
    public List stringList(List val) {
        return val;
    }
    public String throwException(String message) {
        throw new IllegalStateException(message);
    }

    public List myValues(int range) {
        return IntStream.range(0,range)
                .mapToObj(i -> MyValue.builder().a(i).b(i*100).build())
                .collect(Collectors.toList());
    }
    @ExceptionHandler(value = IllegalStateException.class)
    public GraphQLError toCustomError(IllegalStateException e, ErrorContext errorContext) {
        Map extensions =
                Optional.ofNullable(errorContext.getExtensions()).orElseGet(HashMap::new);
        extensions.put("my-custom-code", "some-custom-error");
        return GraphqlErrorBuilder.newError()
                .message(e.getMessage())
                .extensions(extensions)
                .errorType(errorClassification(e.getClass().getSimpleName()))
                .locations(errorContext.getLocations())
                .path(errorContext.getPath())
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy