commercetools/commercetools-sdk-java-v2

How to best use GraphQL to query Products with SDK v2

jon-wong-dawn-foods opened this issue · 1 comments

Hi, I current have the following GraphQLRequest that seems to be grabbing the correct data I need.

GraphQLRequest request = GraphQLRequest.builder()
                .query("query($productFilter:String) { products(where: $productFilter) { " +
                        "results { " +
                        "masterData { staged { allVariants { key sku attributesRaw(includeNames: [\"color\", \"flavor\", \"consistency\", \"coverage\", \"theme\", \"size\", \"weight\", \"box_size\", \"bag_size\", \"storage_bag_size\", \"pack_size\", \"variant_group_id\", \"option_type\"]) { name value } } } }" +
                        "} " +
                        "}} ")
                .variables(builder -> builder.addValue("productFilter", "masterData(current(masterVariant(attributes(name=\"variant_group_id\" and value=\"" + variantGroupId + "\"))))"))
                .build();

        final ApiHttpResponse<GraphQLResponse> response = getApiRoot().graphql()
                .post(request)
                .executeBlocking();

        return response.getBody();

My concern is about its performance. After speaking to some support engineers I was told that ProductProjectionSearch in GraphQL would be more performant. Is there anyway to convert the query I have above into a ProductProjectionSearch? Or is there any other way to optimize my GraphQL query above?

With the latest release (14.3.0) you now have the possibility to create type safe queries.

I can't make a statement about the performance it depends heavily on the size of the projection and if you also use expanded values. But using the search instead of query is the favorable solution for customer facing applications.