Search in sources :

Example 1 with Environment

use of com.yahoo.elide.graphql.Environment in project elide by yahoo.

the class SubscriptionDataFetcher method get.

@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
    OperationDefinition.Operation op = environment.getOperationDefinition().getOperation();
    if (op != OperationDefinition.Operation.SUBSCRIPTION) {
        throw new InvalidEntityBodyException(String.format("%s not supported for subscription models.", op));
    }
    /* build environment object, extracts required fields */
    Environment context = new Environment(environment, nonEntityDictionary);
    /* safe enable debugging */
    if (log.isDebugEnabled()) {
        logContext(log, RelationshipOp.FETCH, context);
    }
    if (context.isRoot()) {
        String entityName = context.field.getName();
        String aliasName = context.field.getAlias();
        EntityProjection projection = context.requestScope.getProjectionInfo().getProjection(aliasName, entityName);
        Flowable<PersistentResource> recordPublisher = PersistentResource.loadRecords(projection, new ArrayList<>(), context.requestScope).toFlowable(BackpressureStrategy.BUFFER).onBackpressureBuffer(bufferSize, true, false);
        return recordPublisher.map(SubscriptionNodeContainer::new);
    }
    // as the PersistentResourceFetcher.
    return context.container.processFetch(context);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Environment(com.yahoo.elide.graphql.Environment) SubscriptionNodeContainer(com.yahoo.elide.graphql.subscriptions.containers.SubscriptionNodeContainer) OperationDefinition(graphql.language.OperationDefinition)

Example 2 with Environment

use of com.yahoo.elide.graphql.Environment in project elide by yahoo.

the class PageInfoContainer method processFetch.

@Override
public Object processFetch(Environment context) {
    String fieldName = context.field.getName();
    ConnectionContainer connectionContainer = getConnectionContainer();
    Optional<Pagination> pagination = connectionContainer.getPagination();
    List<String> ids = connectionContainer.getPersistentResources().stream().map(PersistentResource::getId).sorted().collect(Collectors.toList());
    return pagination.map(pageValue -> {
        switch(KeyWord.byName(fieldName)) {
            case PAGE_INFO_HAS_NEXT_PAGE:
                {
                    int numResults = ids.size();
                    int nextOffset = numResults + pageValue.getOffset();
                    return nextOffset < pageValue.getPageTotals();
                }
            case PAGE_INFO_START_CURSOR:
                return pageValue.getOffset();
            case PAGE_INFO_END_CURSOR:
                return pageValue.getOffset() + ids.size();
            case PAGE_INFO_TOTAL_RECORDS:
                return pageValue.getPageTotals();
            default:
                break;
        }
        throw new BadRequestException("Invalid request. Looking for field: " + fieldName + " in an pageInfo object.");
    }).orElseThrow(() -> new BadRequestException("Could not generate pagination information for type: " + connectionContainer.getTypeName()));
}
Also used : KeyWord(com.yahoo.elide.graphql.KeyWord) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Pagination(com.yahoo.elide.core.request.Pagination) Getter(lombok.Getter) Environment(com.yahoo.elide.graphql.Environment) PersistentResource(com.yahoo.elide.core.PersistentResource) Optional(java.util.Optional) Collectors(java.util.stream.Collectors) Pagination(com.yahoo.elide.core.request.Pagination) PersistentResource(com.yahoo.elide.core.PersistentResource) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)2 Environment (com.yahoo.elide.graphql.Environment)2 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1 Pagination (com.yahoo.elide.core.request.Pagination)1 KeyWord (com.yahoo.elide.graphql.KeyWord)1 SubscriptionNodeContainer (com.yahoo.elide.graphql.subscriptions.containers.SubscriptionNodeContainer)1 OperationDefinition (graphql.language.OperationDefinition)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1