Search in sources :

Example 1 with TypeInfo

use of io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo in project smallrye-graphql by smallrye.

the class QueryBuilder method field.

private String field(FieldInfo field) {
    TypeInfo type = field.getType();
    StringBuilder expression = new StringBuilder();
    field.getAlias().ifPresent(alias -> expression.append(alias).append(":"));
    expression.append(field.getName());
    String path = nestedExpressionPrefix() + field.getRawName();
    List<ParameterInfo> nestedParameters = method.nestedParameters(path);
    if (!nestedParameters.isEmpty())
        expression.append(nestedParameters.stream().map(this::bind).collect(joining(", ", "(", ")")));
    expressionStack.push(path);
    // appends the empty string, if the type is scalar, etc.
    expression.append(fields(type));
    expressionStack.pop();
    return expression.toString();
}
Also used : ParameterInfo(io.smallrye.graphql.client.impl.typesafe.reflection.ParameterInfo) TypeInfo(io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo)

Example 2 with TypeInfo

use of io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo in project smallrye-graphql by smallrye.

the class ResponseImpl method getList.

public <T> List<T> getList(Class<T> dataType, String rootField) {
    if (data == null || data.equals(JsonValue.NULL) || data.keySet().isEmpty()) {
        throw SmallRyeGraphQLClientMessages.msg.noDataInResponse();
    }
    JsonValue item = data.get(rootField);
    if (item == null) {
        throw SmallRyeGraphQLClientMessages.msg.fieldNotFoundInResponse(rootField, data.keySet());
    }
    if (item.getValueType().equals(JsonValue.ValueType.NULL)) {
        // field is present in the response, but is null
        return null;
    }
    if (item instanceof JsonObject) {
        throw SmallRyeGraphQLClientMessages.msg.responseContainsSingleObject(rootField);
    }
    if (item instanceof JsonArray) {
        List<T> result = new ArrayList<T>();
        JsonArray jsonArray = (JsonArray) item;
        TypeInfo type = TypeInfo.of(dataType);
        jsonArray.forEach(o -> {
            if (o.getValueType().equals(JsonValue.ValueType.OBJECT)) {
                result.add((T) JsonReader.readJson(rootField, type, o, null));
            } else {
                result.add((T) getScalarValue(o));
            }
        });
        return result;
    }
    throw SmallRyeGraphQLClientMessages.msg.unexpectedValueInResponse(rootField, item.getValueType().toString());
}
Also used : JsonArray(javax.json.JsonArray) JsonValue(javax.json.JsonValue) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) TypeInfo(io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo)

Example 3 with TypeInfo

use of io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo in project smallrye-graphql by smallrye.

the class JsonArrayReader method readItem.

private Object readItem(IndexedLocationBuilder locationBuilder, JsonValue itemValue) {
    Location itemLocation = locationBuilder.nextLocation();
    TypeInfo itemType = getItemType();
    if (itemValue.getValueType() == ValueType.NULL && itemType.isNonNull())
        throw new InvalidResponseException("invalid null " + itemLocation);
    return JsonReader.readJson(itemLocation, itemType, itemValue, field);
}
Also used : TypeInfo(io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo) InvalidResponseException(io.smallrye.graphql.client.InvalidResponseException)

Example 4 with TypeInfo

use of io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo in project smallrye-graphql by smallrye.

the class HeaderBuilder method resolveHeaderMethod.

private HeaderDescriptor resolveHeaderMethod(Header header) {
    TypeInfo declaringType = method.getDeclaringType();
    MethodInvocation method = new MethodResolver(declaringType, header.method()).resolve();
    if (!method.isStatic())
        throw new RuntimeException("referenced header method '" + header.method() + "'" + " in " + declaringType.getTypeName() + " is not static");
    String value = callMethod(method);
    return new HeaderDescriptor(value, header.name(), toHeaderName(method));
}
Also used : MethodResolver(io.smallrye.graphql.client.impl.typesafe.reflection.MethodResolver) MethodInvocation(io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation) TypeInfo(io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo)

Aggregations

TypeInfo (io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo)4 InvalidResponseException (io.smallrye.graphql.client.InvalidResponseException)1 MethodInvocation (io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation)1 MethodResolver (io.smallrye.graphql.client.impl.typesafe.reflection.MethodResolver)1 ParameterInfo (io.smallrye.graphql.client.impl.typesafe.reflection.ParameterInfo)1 ArrayList (java.util.ArrayList)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonValue (javax.json.JsonValue)1