graphql.GraphQL.executeAsync()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中graphql.GraphQL.executeAsync()方法的一些代码示例,展示了GraphQL.executeAsync()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GraphQL.executeAsync()方法的具体详情如下:
包路径:graphql.GraphQL
类名称:GraphQL
方法名:executeAsync

GraphQL.executeAsync介绍

[英]Executes the graphql query using the provided input object builder

This will return a promise (aka CompletableFuture) to provide a ExecutionResultwhich is the result of executing the provided query.
[中]使用提供的输入对象生成器执行graphql查询
这将返回一个承诺(也称为CompletableFuture),以提供ExecutionResultW,它是执行所提供查询的结果。

代码示例

代码示例来源:origin: graphql-java/graphql-java

/**
 * 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());
}

代码示例来源:origin: graphql-java/graphql-java

/**
 * Executes the graphql query using the provided input object
 *
 * @param executionInput {@link ExecutionInput}
 *
 * @return an {@link ExecutionResult} which can include errors
 */
public ExecutionResult execute(ExecutionInput executionInput) {
  try {
    return executeAsync(executionInput).join();
  } catch (CompletionException e) {
    if (e.getCause() instanceof RuntimeException) {
      throw (RuntimeException) e.getCause();
    } else {
      throw e;
    }
  }
}

代码示例来源:origin: graphql-java/graphql-java

/**
 * 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());
}

代码示例来源:origin: graphql-java/graphql-java

private void equivalentSerialAndAsyncQueryExecution() throws Exception {
  //::FigureC
  ExecutionResult executionResult = graphQL.execute(executionInput);
  // the above is equivalent to the following code (in long hand)
  CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(executionInput);
  ExecutionResult executionResult2 = promise.join();
  //::/FigureC
}

代码示例来源:origin: graphql-java/graphql-java

@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
}

代码示例来源:origin: com.graphql-java/graphql-java

/**
 * 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());
}

代码示例来源:origin: com.graphql-java/graphql-java

/**
 * Executes the graphql query using the provided input object
 *
 * @param executionInput {@link ExecutionInput}
 *
 * @return an {@link ExecutionResult} which can include errors
 */
public ExecutionResult execute(ExecutionInput executionInput) {
  try {
    return executeAsync(executionInput).join();
  } catch (CompletionException e) {
    if (e.getCause() instanceof RuntimeException) {
      throw (RuntimeException) e.getCause();
    } else {
      throw e;
    }
  }
}

代码示例来源:origin: com.graphql-java/graphql-java

/**
 * 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());
}

代码示例来源:origin: leangen/graphql-spqr

@Override
public CompletableFuture<ExecutionResult> executeAsync(ExecutionInput executionInput) {
  return delegate.executeAsync(executionInput.transform(builder -> builder.context(new ContextWrapper(executionInput.getContext()))));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void endpointIsAvailable() {
  String query = "{foo}";
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  String body = this.restTemplate.getForObject("/graphql?query={query}", String.class, query);
  assertThat(body, is("{\"data\":\"bar\"}"));
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void endpointIsAvailableWithDifferentUrl() {
  String query = "{foo}";
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  String body = this.restTemplate.getForObject("/otherUrl/?query={query}", String.class, query);
  assertThat(body, is("{\"data\":\"bar\"}"));
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: com.graphql-java/graphql-java-spring-webmvc

@Override
public CompletableFuture<ExecutionResult> invoke(GraphQLInvocationData invocationData, WebRequest webRequest) {
  ExecutionInput executionInput = ExecutionInput.newExecutionInput()
      .query(invocationData.getQuery())
      .operationName(invocationData.getOperationName())
      .variables(invocationData.getVariables())
      .build();
  return graphQL.executeAsync(executionInput);
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Override
public CompletableFuture<ExecutionResult> invoke(GraphQLInvocationData invocationData, WebRequest webRequest) {
  ExecutionInput executionInput = ExecutionInput.newExecutionInput()
      .query(invocationData.getQuery())
      .operationName(invocationData.getOperationName())
      .variables(invocationData.getVariables())
      .build();
  return graphQL.executeAsync(executionInput);
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void endpointIsAvailableWithDifferentUrl() {
  String query = "{foo}";
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  this.webClient.get().uri("/otherUrl?query={query}", query).exchange().expectStatus().isOk()
      .expectBody(String.class).isEqualTo("{\"data\":\"bar\"}");
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Override
public Mono<ExecutionResult> invoke(GraphQLInvocationData invocationData, ServerWebExchange serverWebExchange) {
  ExecutionInput executionInput = ExecutionInput.newExecutionInput()
      .query(invocationData.getQuery())
      .operationName(invocationData.getOperationName())
      .variables(invocationData.getVariables())
      .build();
  return Mono.fromCompletionStage(graphQL.executeAsync(executionInput));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void endpointIsAvailable() {
  String query = "{foo}";
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  this.webClient.get().uri("/graphql?query={query}", query).exchange().expectStatus().isOk()
      .expectBody(String.class).isEqualTo("{\"data\":\"bar\"}");
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: com.graphql-java/graphql-java-spring-webflux

@Override
public Mono<ExecutionResult> invoke(GraphQLInvocationData invocationData, ServerWebExchange serverWebExchange) {
  ExecutionInput executionInput = ExecutionInput.newExecutionInput()
      .query(invocationData.getQuery())
      .operationName(invocationData.getOperationName())
      .variables(invocationData.getVariables())
      .build();
  return Mono.fromCompletionStage(graphQL.executeAsync(executionInput));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void testSimplePostRequest() throws Exception {
  Map<String, Object> request = new LinkedHashMap<>();
  String query = "{foo}";
  request.put("query", query);
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  client.post().uri("/graphql")
      .body(Mono.just(request), Map.class)
      .accept(MediaType.APPLICATION_JSON_UTF8)
      .exchange()
      .expectStatus().isOk()
      .expectBody()
      .jsonPath("data").isEqualTo("bar");
  assertThat(captor.getAllValues().size(), is(1));
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void testSimpleGetRequest() throws Exception {
  String query = "{foo}";
  String queryString = URLEncoder.encode(query, "UTF-8");
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  client.get().uri(uriBuilder -> uriBuilder.path("/graphql")
      .queryParam("query", queryString)
      .build())
      .accept(MediaType.APPLICATION_JSON_UTF8)
      .exchange()
      .expectStatus().isOk()
      .expectBody()
      .jsonPath("data").isEqualTo("bar");
  assertThat(captor.getAllValues().size(), is(1));
  assertThat(captor.getValue().getQuery(), is(query));
}

代码示例来源:origin: graphql-java/graphql-java-spring

@Test
public void testDifferentUrl() throws Exception {
  Map<String, Object> request = new LinkedHashMap<>();
  String query = "{foo}";
  request.put("query", query);
  ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
      .data("bar")
      .build();
  CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
  ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
  Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
  MvcResult mvcResult = this.mockMvc.perform(post("/otherUrl")
      .content(toJson(request))
      .contentType(MediaType.APPLICATION_JSON_UTF8))
      .andExpect(status().isOk())
      .andExpect(request().asyncStarted())
      .andReturn();
  this.mockMvc.perform(asyncDispatch(mvcResult))
      .andDo(print()).andExpect(status().isOk())
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
      .andExpect(jsonPath("data", is("bar")))
      .andReturn();
  assertThat(captor.getAllValues().size(), is(1));
  assertThat(captor.getValue().getQuery(), is(query));
}

相关文章