/** * Executes the graphql query using the provided input object builder * <p> * This will return a completed {@link ExecutionResult} * which is the result of executing the provided query. * * @param executionInputBuilder {@link ExecutionInput.Builder} * * @return an {@link ExecutionResult} which can include errors */ public ExecutionResult execute(ExecutionInput.Builder executionInputBuilder) { return executeAsync(executionInputBuilder.build()).join(); }
/** * Executes the graphql query using calling the builder function and giving it a new builder. * <p> * This allows a lambda style like : * <pre> * {@code * ExecutionResult result = graphql.execute(input -> input.query("{hello}").root(startingObj).context(contextObj)); * } * </pre> * * @param builderFunction a function that is given a {@link ExecutionInput.Builder} * * @return an {@link ExecutionResult} which can include errors */ public ExecutionResult execute(UnaryOperator<ExecutionInput.Builder> builderFunction) { return execute(builderFunction.apply(ExecutionInput.newExecutionInput()).build()); }
/** * Executes the graphql query using the provided input object builder * <p> * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult} * which is the result of executing the provided query. * <p> * This allows a lambda style like : * <pre> * {@code * ExecutionResult result = graphql.executeAsync(input -> input.query("{hello}").root(startingObj).context(contextObj)); * } * </pre> * * @param builderFunction a function that is given a {@link ExecutionInput.Builder} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture<ExecutionResult> executeAsync(UnaryOperator<ExecutionInput.Builder> builderFunction) { return executeAsync(builderFunction.apply(ExecutionInput.newExecutionInput()).build()); }
/** * Info: This sets context = root to be backwards compatible. * * @param query the query/mutation/subscription * @param operationName the name of the operation to execute * @param context custom object provided to each {@link graphql.schema.DataFetcher} * * @return an {@link ExecutionResult} which can include errors * * @deprecated Use {@link #execute(ExecutionInput)} */ @Deprecated public ExecutionResult execute(String query, String operationName, Object context) { ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query(query) .operationName(operationName) .context(context) .root(context) // This we are doing do be backwards compatible .build(); return execute(executionInput); }
public static void main(String[] args) { GraphQLSchema schema = buildSchema(); GraphQL graphql = GraphQL.newGraphQL(schema).build(); Map<String, Object> root = new HashMap<>(); root.put("dateField", LocalDateTime.of(1969, 10, 8, 0, 0)); String query = "" + "query {\n" + " default : dateField \n" + " usa : dateField(format : \"MM-dd-YYYY\") \n" + "}"; ExecutionInput executionInput = ExecutionInput.newExecutionInput() .root(root) .query(query) .build(); ExecutionResult executionResult = graphql.execute(executionInput); Map<String, Object> data = executionResult.getData(); // data['default'] == '08-10-1969' // data['usa'] == '10-08-1969' } }
private void contextHelper() { // // this could be code that authorises the user in some way and sets up enough context // that can be used later inside data fetchers allowing them // to do their job // UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser()); ExecutionInput executionInput = ExecutionInput.newExecutionInput() .context(contextForUser) .build(); ExecutionResult executionResult = graphQL.execute(executionInput); // ... // // later you are able to use this context object when a data fetcher is invoked // DataFetcher dataFetcher = new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { UserContext userCtx = environment.getContext(); Long businessObjId = environment.getArgument("businessObjId"); return invokeBusinessLayerMethod(userCtx, businessObjId); } }; }
/** * Info: This sets context = root to be backwards compatible. * * @param query the query/mutation/subscription * @param operationName name of the operation to execute * @param context custom object provided to each {@link graphql.schema.DataFetcher} * @param variables variable values uses as argument * * @return an {@link ExecutionResult} which can include errors * * @deprecated Use {@link #execute(ExecutionInput)} */ @Deprecated public ExecutionResult execute(String query, String operationName, Object context, Map<String, Object> variables) { ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query(query) .operationName(operationName) .context(context) .root(context) // This we are doing do be backwards compatible .variables(variables) .build(); return execute(executionInput); }
/** * Executes the graphql query using the provided input object builder * <p> * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult} * which is the result of executing the provided query. * <p> * This allows a lambda style like : * <pre> * {@code * ExecutionResult result = graphql.execute(input -> input.query("{hello}").root(startingObj).context(contextObj)); * } * </pre> * * @param builderFunction a function that is given a {@link ExecutionInput.Builder} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture<ExecutionResult> executeAsync(UnaryOperator<ExecutionInput.Builder> builderFunction) { return executeAsync(builderFunction.apply(ExecutionInput.newExecutionInput()).build()); }
/** * This helps you transform the current ExecutionInput object into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new ExecutionInput object based on calling build on that builder */ public ExecutionInput transform(Consumer<Builder> builderConsumer) { Builder builder = new Builder() .query(this.query) .operationName(this.operationName) .context(this.context) .root(this.root) .dataLoaderRegistry(this.dataLoaderRegistry) .variables(this.variables); builderConsumer.accept(builder); return builder.build(); }
/** * Executes the graphql query using the provided input object builder * <p> * This will return a completed {@link ExecutionResult} * which is the result of executing the provided query. * <p> * This allows a lambda style like : * <pre> * {@code * ExecutionResult result = graphql.execute(input -> input.query("{hello}").root(startingObj).context(contextObj)); * } * </pre> * * @param builderFunction a function that is given a {@link ExecutionInput.Builder} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture<ExecutionResult> execute(UnaryOperator<ExecutionInput.Builder> builderFunction) { return executeAsync(builderFunction.apply(ExecutionInput.newExecutionInput()).build()); }
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()) .build(); ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query("query { shops { id name departments { id name products { id name } } } }") .dataLoaderRegistry(dataLoaderRegistry) .build(); ExecutionResult result = graphQL.execute(executionInput); System.out.println("\nExecutionResult: " + result.toSpecification()); }
/** * Info: This sets context = root to be backwards compatible. * * @param query the query/mutation/subscription * @param context custom object provided to each {@link graphql.schema.DataFetcher} * * @return an {@link ExecutionResult} which can include errors * * @deprecated Use {@link #execute(ExecutionInput)} */ @Deprecated public ExecutionResult execute(String query, Object context) { ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query(query) .context(context) .root(context) // This we are doing do be backwards compatible .build(); return execute(executionInput); }
@SuppressWarnings({"Convert2MethodRef", "unused", "FutureReturnValueIgnored"}) private void simpleAsyncQueryExecution() throws Exception { //::FigureB GraphQL graphQL = buildSchema(); ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }") .build(); CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(executionInput); promise.thenAccept(executionResult -> { // here you might send back the results as JSON over HTTP encodeResultToJsonAndSendResponse(executionResult); }); promise.join(); //::/FigureB }
private void simpleQueryExecution() throws Exception { //::FigureA GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); GraphQL graphQL = GraphQL.newGraphQL(schema) .build(); ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("query { hero { name } }") .build(); ExecutionResult executionResult = graphQL.execute(executionInput); Object data = executionResult.getData(); List<GraphQLError> errors = executionResult.getErrors(); //::/FigureA }
void batchedRun() { System.out.println("=== BatchedExecutionStrategy ==="); GraphQLSchema schema = buildBatchedSchema(); GraphQL graphQL = GraphQL .newGraphQL(schema) .queryExecutionStrategy(new BatchedExecutionStrategy()) .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()); }
/** * Executes the graphql query using the provided input object builder * <p> * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult} * which is the result of executing the provided query. * * @param executionInputBuilder {@link ExecutionInput.Builder} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput.Builder executionInputBuilder) { return executeAsync(executionInputBuilder.build()); }
/** * Executes the specified graphql query/mutation/subscription * * @param query the query/mutation/subscription * * @return an {@link ExecutionResult} which can include errors */ public ExecutionResult execute(String query) { ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query(query) .build(); return execute(executionInput); }
/** * Executes the graphql query using the provided input object builder * <p> * This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult} * which is the result of executing the provided query. * * @param executionInputBuilder {@link ExecutionInput.Builder} * * @return a promise to an {@link ExecutionResult} which can include errors */ public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput.Builder executionInputBuilder) { return executeAsync(executionInputBuilder.build()); }
/** * Executes the graphql query using the provided input object builder * * @param executionInputBuilder {@link ExecutionInput.Builder} * * @return an {@link ExecutionResult} which can include errors */ public ExecutionResult execute(ExecutionInput.Builder executionInputBuilder) { return execute(executionInputBuilder.build()); }
void contextWiring() { AuthorisationCtx authCtx = AuthorisationCtx.obtain(); ExecutionInput executionInput = ExecutionInput.newExecutionInput() .query(query) .context(authCtx) .build(); }