-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from weaviate/support-for-generic-response
Support for GraphQL response with custom generic classes
- Loading branch information
Showing
21 changed files
with
1,008 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/io/weaviate/client/base/AsyncBaseGraphQLClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.weaviate.client.base; | ||
|
||
import io.weaviate.client.Config; | ||
import io.weaviate.client.base.http.async.WeaviateGraphQLTypedResponseConsumer; | ||
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse; | ||
import java.util.concurrent.Future; | ||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
|
||
public class AsyncBaseGraphQLClient<T> extends AsyncBaseClient<T> { | ||
public AsyncBaseGraphQLClient(CloseableHttpAsyncClient client, Config config) { | ||
super(client, config); | ||
} | ||
|
||
protected <C> Future<Result<GraphQLTypedResponse<C>>> sendGraphQLTypedRequest(Object payload, Class<C> classOfC, | ||
FutureCallback<Result<GraphQLTypedResponse<C>>> callback) { | ||
return client.execute(SimpleRequestProducer.create(getRequest("/graphql", payload, "POST")), new WeaviateGraphQLTypedResponseConsumer<>(classOfC), callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/io/weaviate/client/base/BaseGraphQLClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.weaviate.client.base; | ||
|
||
import io.weaviate.client.Config; | ||
import io.weaviate.client.base.http.HttpClient; | ||
import io.weaviate.client.base.http.HttpResponse; | ||
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse; | ||
|
||
public abstract class BaseGraphQLClient<T> extends BaseClient<T> { | ||
public BaseGraphQLClient(HttpClient client, Config config) { | ||
super(client, config); | ||
} | ||
|
||
private <C> GraphQLTypedResponse<C> toResponseTyped(String response, Class<C> classOfC) { | ||
return serializer.toGraphQLTypedResponse(response, classOfC); | ||
} | ||
|
||
protected <C> Response<GraphQLTypedResponse<C>> sendGraphQLTypedRequest(Object payload, Class<C> classOfC) { | ||
try { | ||
HttpResponse response = this.sendHttpRequest("/graphql", payload, "POST"); | ||
int statusCode = response.getStatusCode(); | ||
String responseBody = response.getBody(); | ||
|
||
if (statusCode < 399) { | ||
GraphQLTypedResponse<C> body = toResponseTyped(responseBody, classOfC); | ||
return new Response<>(statusCode, body, null); | ||
} | ||
|
||
WeaviateErrorResponse error = toResponse(responseBody, WeaviateErrorResponse.class); | ||
return new Response<>(statusCode, null, error); | ||
} catch (Exception e) { | ||
WeaviateErrorResponse errors = getWeaviateErrorResponse(e); | ||
return new Response<>(0, null, errors); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/io/weaviate/client/base/http/async/WeaviateGraphQLTypedResponseConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.weaviate.client.base.http.async; | ||
|
||
import io.weaviate.client.base.Result; | ||
import io.weaviate.client.base.Serializer; | ||
import io.weaviate.client.v1.graphql.model.GraphQLTypedResponse; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer; | ||
import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
|
||
public class WeaviateGraphQLTypedResponseConsumer<C> extends AbstractAsyncResponseConsumer<Result<GraphQLTypedResponse<C>>, byte[]> { | ||
private final Serializer serializer; | ||
private final Class<C> classOfT; | ||
|
||
public WeaviateGraphQLTypedResponseConsumer(Class<C> classOfT) { | ||
super(new BasicAsyncEntityConsumer()); | ||
this.serializer = new Serializer(); | ||
this.classOfT = classOfT; | ||
} | ||
|
||
@Override | ||
protected Result<GraphQLTypedResponse<C>> buildResult(HttpResponse response, byte[] entity, ContentType contentType) { | ||
String body = (entity != null) ? new String(entity, StandardCharsets.UTF_8) : ""; | ||
return serializer.toGraphQLTypedResult(response.getCode(), body, classOfT); | ||
} | ||
|
||
@Override | ||
public void informationResponse(HttpResponse response, HttpContext context) throws HttpException, IOException { | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/io/weaviate/client/base/util/GroupHitDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.weaviate.client.base.util; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.reflect.TypeToken; | ||
import io.weaviate.client.v1.graphql.model.GraphQLGetBaseObject; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
|
||
public class GroupHitDeserializer implements JsonDeserializer<GraphQLGetBaseObject.Additional.Group.GroupHit> { | ||
|
||
@Override | ||
public GraphQLGetBaseObject.Additional.Group.GroupHit deserialize(JsonElement json, Type typeOfT, | ||
JsonDeserializationContext context) throws JsonParseException { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
|
||
GraphQLGetBaseObject.Additional.Group.GroupHit.AdditionalGroupHit additional = | ||
context.deserialize(jsonObject.get("_additional"), GraphQLGetBaseObject.Additional.Group.GroupHit.AdditionalGroupHit.class); | ||
|
||
// Remove _additional from the JSON object | ||
jsonObject.remove("_additional"); | ||
|
||
// Deserialize the rest into a Map | ||
Type mapType = new TypeToken<Map<String, Object>>() {}.getType(); | ||
Map<String, Object> properties = context.deserialize(jsonObject, mapType); | ||
|
||
return new GraphQLGetBaseObject.Additional.Group.GroupHit(properties, additional); | ||
} | ||
} |
Oops, something went wrong.