Search in sources :

Example 21 with GraphQL

use of graphql.GraphQL 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 22 with GraphQL

use of graphql.GraphQL 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 23 with GraphQL

use of graphql.GraphQL 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)

Example 24 with GraphQL

use of graphql.GraphQL in project structr by structr.

the class GraphQLCommand method createResult.

private List<GraphObject> createResult(final SecurityContext securityContext, final GraphQLRequest request) throws IOException, FrameworkException {
    final List<GraphObject> resultList = new LinkedList<>();
    if (request.hasSchemaQuery()) {
        // schema query is serialized from GraphQL execution result, doesn't need enclosing JSON object
        for (final GraphQLQuery query : request.getQueries()) {
            if (query.isSchemaQuery()) {
                // use graphql-java schema response
                final String originalQuery = request.getOriginalQuery();
                final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
                final ExecutionResult result = graphQL.execute(originalQuery);
                if (result != null) {
                    resultList.add(GraphObjectMap.fromMap(result.getData()));
                }
            }
        }
    } else {
        for (final GraphQLQuery query : request.getQueries()) {
            if (query.isSchemaQuery()) {
                // use graphql-java schema response
                final String originalQuery = request.getOriginalQuery();
                final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
                final ExecutionResult result = graphQL.execute(originalQuery);
                if (result != null) {
                    resultList.add(GraphObjectMap.fromMap(result.getData()));
                }
            } else {
                for (final GraphObject object : query.getEntities(securityContext)) {
                    resultList.add(object);
                }
            }
        }
    }
    return resultList;
}
Also used : GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) GraphQLQuery(org.structr.core.graphql.GraphQLQuery)

Example 25 with GraphQL

use of graphql.GraphQL in project vertx-examples by vert-x3.

the class SubscriptionServer method createGraphQL.

private GraphQL createGraphQL() {
    String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();
    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
    RuntimeWiring runtimeWiring = newRuntimeWiring().type("Subscription", builder -> builder.dataFetcher("links", this::linksFetcher)).build();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
    return GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) Publisher(org.reactivestreams.Publisher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Router(io.vertx.ext.web.Router) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Launcher(io.vertx.core.Launcher) RxHelper(io.vertx.reactivex.RxHelper) List(java.util.List) Flowable(io.reactivex.Flowable) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) AbstractVerticle(io.vertx.core.AbstractVerticle) GraphQLSchema(graphql.schema.GraphQLSchema) HttpServerOptions(io.vertx.core.http.HttpServerOptions) SchemaGenerator(graphql.schema.idl.SchemaGenerator) ApolloWSHandler(io.vertx.ext.web.handler.graphql.ApolloWSHandler) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) RuntimeWiring(graphql.schema.idl.RuntimeWiring) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema)

Aggregations

GraphQL (graphql.GraphQL)25 ExecutionResult (graphql.ExecutionResult)18 GraphQLSchema (graphql.schema.GraphQLSchema)13 ExecutionInput (graphql.ExecutionInput)6 RuntimeWiring (graphql.schema.idl.RuntimeWiring)5 RuntimeWiring.newRuntimeWiring (graphql.schema.idl.RuntimeWiring.newRuntimeWiring)5 SchemaGenerator (graphql.schema.idl.SchemaGenerator)5 SchemaParser (graphql.schema.idl.SchemaParser)5 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)5 GraphQLError (graphql.GraphQLError)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)4 List (java.util.List)4 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)4 DataLoaderDispatcherInstrumentation (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)2 ExecutorServiceExecutionStrategy (graphql.execution.ExecutorServiceExecutionStrategy)2