Search in sources :

Example 36 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class BreadthFirstExecutionTestStrategy method completeValue.

private void completeValue(ExecutionContext executionContext, Map<String, Object> results, String fieldName, Object fetchedValue, ExecutionStrategyParameters newParameters) {
    ExecutionResult resolvedResult = completeField(executionContext, newParameters, fetchedValue).join();
    results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null);
}
Also used : ExecutionResult(graphql.ExecutionResult)

Example 37 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class BreadthFirstTestStrategy method completeFields.

private CompletableFuture<ExecutionResult> completeFields(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Map<String, Object> fetchedValues) {
    Map<String, List<Field>> fields = parameters.getFields();
    // then for every fetched value, complete it, breath first
    Map<String, Object> results = new LinkedHashMap<>();
    for (String fieldName : fetchedValues.keySet()) {
        List<Field> fieldList = fields.get(fieldName);
        ExecutionStrategyParameters newParameters = newParameters(parameters, fields, fieldName);
        Object fetchedValue = fetchedValues.get(fieldName);
        try {
            ExecutionResult resolvedResult = completeField(executionContext, newParameters, fetchedValue).join();
            results.put(fieldName, resolvedResult != null ? resolvedResult.getData() : null);
        } catch (NonNullableFieldWasNullException e) {
            assertNonNullFieldPrecondition(e);
            results = null;
            break;
        }
    }
    return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors()));
}
Also used : Field(graphql.language.Field) ExecutionResultImpl(graphql.ExecutionResultImpl) List(java.util.List) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap)

Example 38 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class BatchCompare method dataLoaderRun.

void dataLoaderRun() {
    System.out.println("=== AsyncExecutionStrategy with DataLoader ===");
    GraphQLSchema schema = buildDataLoaderSchema();
    DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
    dataLoaderRegistry.register("departments", BatchCompareDataFetchers.departmentsForShopDataLoader);
    dataLoaderRegistry.register("products", BatchCompareDataFetchers.productsForDepartmentDataLoader);
    GraphQL graphQL = GraphQL.newGraphQL(schema).instrumentation(new DataLoaderDispatcherInstrumentation(dataLoaderRegistry)).build();
    ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { shops { id name departments { id name products { id name } } } }").build();
    ExecutionResult result = graphQL.execute(executionInput);
    System.out.println("\nExecutionResult: " + result.toSpecification());
}
Also used : DataLoaderRegistry(org.dataloader.DataLoaderRegistry) GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 39 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class SubscriptionExamples method basicSubscriptionExample.

void basicSubscriptionExample() {
    GraphQL graphQL = GraphQL.newGraphQL(schema).subscriptionExecutionStrategy(new SubscriptionExecutionStrategy()).build();
    String query = "" + "    subscription StockCodeSubscription {\n" + "        stockQuotes(stockCode:\"IBM') {\n" + "            dateTime\n" + "            stockCode\n" + "            stockPrice\n" + "            stockPriceChange\n" + "        }\n" + "    }\n";
    ExecutionResult executionResult = graphQL.execute(query);
    Publisher<ExecutionResult> stockPriceStream = executionResult.getData();
    AtomicReference<Subscription> subscriptionRef = new AtomicReference<>();
    stockPriceStream.subscribe(new Subscriber<ExecutionResult>() {

        @Override
        public void onSubscribe(Subscription s) {
            subscriptionRef.set(s);
            s.request(1);
        }

        @Override
        public void onNext(ExecutionResult er) {
            // 
            // process the next stock price
            // 
            processStockPriceChange(er.getData());
            // 
            // ask the publisher for one more item please
            // 
            subscriptionRef.get().request(1);
        }

        @Override
        public void onError(Throwable t) {
        // 
        // The upstream publishing data source has encountered an error
        // and the subscription is now terminated.  Real production code needs
        // to decide on a error handling strategy.
        // 
        }

        @Override
        public void onComplete() {
        // 
        // the subscription has completed.  There is not more data
        // 
        }
    });
}
Also used : GraphQL(graphql.GraphQL) SubscriptionExecutionStrategy(graphql.execution.SubscriptionExecutionStrategy) ExecutionResult(graphql.ExecutionResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription)

Example 40 with ExecutionResult

use of graphql.ExecutionResult in project graphql-java by graphql-java.

the class DiffSet method introspect.

private static Map<String, Object> introspect(GraphQLSchema schema) {
    GraphQL gql = GraphQL.newGraphQL(schema).build();
    ExecutionResult result = gql.execute(IntrospectionQuery.INTROSPECTION_QUERY);
    Assert.assertTrue(result.getErrors().size() == 0, "The schema has errors during Introspection");
    return result.getData();
}
Also used : GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult)

Aggregations

ExecutionResult (graphql.ExecutionResult)42 GraphQL (graphql.GraphQL)17 List (java.util.List)10 GraphQLSchema (graphql.schema.GraphQLSchema)9 ExecutionInput (graphql.ExecutionInput)8 Instrumentation (graphql.execution.instrumentation.Instrumentation)8 Field (graphql.language.Field)8 ExecutionResultImpl (graphql.ExecutionResultImpl)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 GraphQLError (graphql.GraphQLError)6 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)6 LinkedHashMap (java.util.LinkedHashMap)6 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 InstrumentationExecutionStrategyParameters (graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters)4 HashMap (java.util.HashMap)4 Collectors.toList (java.util.stream.Collectors.toList)3 InstrumentationFieldCompleteParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldCompleteParameters)2 GraphQLList (graphql.schema.GraphQLList)2