Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for GraphQL response with custom generic classes #330

Merged
merged 2 commits into from
Nov 26, 2024

Conversation

antas-marcin
Copy link
Collaborator

@antas-marcin antas-marcin commented Nov 16, 2024

The idea is to have a better way of GraphQL response serialization.

For example we have a collection in Weaviate called Pizza that has 4 properties defined:

  • name
  • description
  • bestBefore
  • price

We can define a class that will automatically the GraphQL response for Pizza collection:

@Getter
public static class Pizzas {
  @SerializedName(value = "Pizza")
  List<Pizza> pizzas;

  @Getter
  public static class Pizza extends GraphQLGetBaseObject {
    String name;
    String description;
    String bestBefore;
    Float price;
  }
}

invocation of the GraphQL query would look like this:

try (WeaviateAsyncClient asyncClient = client.async()) {
  return asyncClient.graphQL().get()
    .withClassName("Pizza")
    .withFields(Field.builder().name("name").build(), Field.builder().name("description").build())
    .run(Pizzas.class)
    .get();
}

as a response we would get GraphQLTypedResponse<Pizzas> with Pizza objects:

Result<GraphQLTypedResponse<Pizzas>> result = getResults();

List<Pizzas.Pizza> pizzas = result.getResult().getData().getObjects().getPizzas();

Pizzas.Pizza> pizza = pizzas.get(0);

// access PIzza specific properties
Float price = pizza.getPrice();

// access _additional values like vector (and others)
Float[] vector = pizza.getAdditional().getVector();
Float certainty = pizza.getAdditional().getCertainty();

@antas-marcin antas-marcin force-pushed the support-for-generic-response branch from 77fa3fb to 24447f1 Compare November 18, 2024 08:58
@antas-marcin antas-marcin force-pushed the support-for-generic-response branch from 24447f1 to 5a04c7b Compare November 26, 2024 09:22
Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@antas-marcin antas-marcin force-pushed the support-for-generic-response branch from 5a04c7b to d18a048 Compare November 26, 2024 10:31
@antas-marcin antas-marcin merged commit e536dc8 into main Nov 26, 2024
4 checks passed
@antas-marcin antas-marcin deleted the support-for-generic-response branch November 26, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants