graphql.schema.DataFetchingFieldSelectionSet Java Examples

The following examples show how to use graphql.schema.DataFetchingFieldSelectionSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraphqlServlet.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
/**
 * Based on the GraphQL query, this method returns a Magento <code>Products</code> object
 * that "matches" the data expected by each CIF component. Each CIF component indeed expects a
 * specific JSON response. Luckily, each GraphQL query sent by each component is different so
 * we can "detect" what response should be returned.
 * 
 * @param env The metadata of the GraphQL query.
 * @return A Magento <code>Products</code> object.
 */
private Products readProductsResponse(DataFetchingEnvironment env) {

    DataFetchingFieldSelectionSet selectionSet = env.getSelectionSet();
    if (selectionSet.contains("items/related_products")) {
        return readProductsFrom(RELATED_PRODUCTS_JSON);
    } else if (selectionSet.contains("items/upsell_products")) {
        return readProductsFrom(UPSELL_PRODUCTS_JSON);
    } else if (selectionSet.contains("items/crosssell_products")) {
        return readProductsFrom(CROSSSELL_PRODUCTS_JSON);
    }

    Map<String, Object> args = env.getArguments();
    // We return different responses based on the products filter argument
    Object productsFilter = args.get(PRODUCTS_FILTER_ARG);
    if (productsFilter != null) {
        String filter = productsFilter.toString();
        Matcher skuInMatcher = SKU_IN_PATTERN.matcher(filter);
        Matcher urlKeyEqPattern = URL_KEY_EQ_PATTERN.matcher(filter);

        if (skuInMatcher.matches()) {
            // The filter {sku:{in:[...]}} can be a query for the carousel (3 skus) or a client-side query to fetch prices
            // on the product (1 sku), productlist (6 skus), or search pages (6 skus)
            String[] skus = skuInMatcher.group(1).split(",");
            LOGGER.debug("Got sku:in filter with {} sku(s): {}", skus.length, skuInMatcher.group(1));
            if (skus.length == 1) {
                if (GROUPED_PRODUCT_SKU.equals(skus[0])) {
                    return readProductsFrom(GROUPED_PRODUCT_JSON);
                }
                return readProductsFrom(PRODUCTS_JSON);
            } else if (skus.length == 6) {
                return readProductsFrom(PRODUCTS_COLLECTION_JSON);
            }
            return readProductsFrom(PRODUCT_CAROUSEL_JSON);
        } else if (filter.matches(SKU_EQ_REGEX)) {
            return readProductsFrom(PRODUCT_TEASER_JSON);
        } else if (filter.matches(CATEGORY_ID_REGEX)) {
            return readProductsFrom(PRODUCTS_COLLECTION_JSON);
        } else if (urlKeyEqPattern.matches() && GROUPED_PRODUCT_URL_KEY.equals(urlKeyEqPattern.group(1))) {
            return readProductsFrom(GROUPED_PRODUCT_JSON);
        }
    }

    if (args.containsKey(PRODUCTS_SEARCH_ARG)) {
        return readProductsFrom(PRODUCTS_COLLECTION_JSON);
    }

    return readProductsFrom(PRODUCTS_JSON);
}
 
Example #2
Source File: RelayDataFetchingEnvironmentDecorator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public DataFetchingFieldSelectionSet getSelectionSet() {
    return delegate.getSelectionSet();
}
 
Example #3
Source File: LookUpSubjectByUriFetcherWrapperTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public DataFetchingFieldSelectionSet getSelectionSet() {
  throw new IllegalStateException("Not implemented yet");
}