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

org.rapidgraphql.client.GraphQLHttpClient Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package org.rapidgraphql.client;

import kong.unirest.core.*;
import lombok.Builder;
import org.rapidgraphql.client.exceptions.GraphQLHttpErrorException;
import org.rapidgraphql.client.extractor.ResultExtractor;

@Builder
public class GraphQLHttpClient {
    private final String url;
    private final Config requestConfig;
    public Object exchange(GraphQLRequestBody graphQLRequestBody, ResultExtractor extractor) {
        HttpRequestWithBody requestWithBody = Unirest.post(url)
                .accept("application/json");
        if (requestConfig != null) {
            requestWithBody = requestWithBody.connectTimeout(requestConfig.getConnectionTimeout());
        }
        HttpResponse jsonNodeHttpResponse = requestWithBody
                .body(graphQLRequestBody)
                .asJson();
        if (!jsonNodeHttpResponse.isSuccess()) {
            throw new GraphQLHttpErrorException(jsonNodeHttpResponse);
        }
        return jsonNodeHttpResponse.mapBody(extractor::extract);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy