graphql.GraphQL类的使用及代码示例

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

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

GraphQL介绍

[英]This class is where all graphql-java query execution begins. It combines the objects that are needed to make a successful graphql query, with the most important being the graphql.schema.GraphQLSchemaand the graphql.execution.ExecutionStrategyBuilding this object is very cheap and can be done on each execution if necessary. Building the schema is often not as cheap, especially if its parsed from graphql IDL schema format via graphql.schema.idl.SchemaParser. The data for a query is returned via ExecutionResult#getData() and any errors encountered as placed in ExecutionResult#getErrors(). Runtime Exceptions Runtime exceptions can be thrown by the graphql engine if certain situations are encountered. These are not errors in execution but rather totally unacceptable conditions in which to execute a graphql query.

  • graphql.schema.CoercingSerializeException - is thrown when a value cannot be serialised by a Scalar type, for example a String value being coerced as an Int.
  • graphql.execution.UnresolvedTypeException - is thrown if a graphql.schema.TypeResolver fails to provide a concrete object type given a interface or union type.
  • graphql.schema.validation.InvalidSchemaException - is thrown if the schema is not valid when built via graphql.schema.GraphQLSchema.Builder#build()
  • graphql.GraphQLException - is thrown as a general purpose runtime exception, for example if the code cant access a named field when examining a POJO.
  • graphql.AssertException - is thrown as a low level code assertion exception for truly unexpected code conditions
    [中]这个类是所有graphql java查询执行的开始。它结合了进行成功的graphql查询所需的对象,其中最重要的是graphql。模式。graphqlschema和graphql。处决ExecutionStrategyBuilding这个对象非常便宜,如果需要,可以在每次执行时完成。构建模式通常不那么便宜,尤其是通过graphql从graphql IDL模式格式解析模式时。模式。idl。SchemaParser。查询的数据通过ExecutionResult#getData()和ExecutionResult#getErrors()中遇到的任何错误返回。运行时异常如果遇到某些情况,graphql引擎可以引发运行时异常。这些不是执行中的错误,而是执行graphql查询时完全不可接受的条件。
    *图ql。模式。当一个值不能被标量类型序列化时,例如一个字符串值被强制为Int时,将引发强制序列化异常。
    *图ql。处决UnsolvedTypeException-在graphql发生错误时引发。模式。TypeResolver无法提供给定接口或联合类型的具体对象类型。
    *图ql。模式。验证。InvalidSchemaException-如果通过graphql生成的架构无效,则会引发该异常。模式。GraphQLSchema。建筑商#建筑()
    *图ql。GraphQLException-作为通用运行时异常抛出,例如,如果代码在检查POJO时无法访问命名字段。
    *图ql。AssertException-对于真正意外的代码条件,作为低级代码断言异常抛出

代码示例

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

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

代码示例来源: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

public GraphQL build() {
    assertNotNull(graphQLSchema, "graphQLSchema must be non null");
    assertNotNull(queryExecutionStrategy, "queryStrategy must be non null");
    assertNotNull(idProvider, "idProvider must be non null");
    return new GraphQL(graphQLSchema, queryExecutionStrategy, mutationExecutionStrategy, subscriptionExecutionStrategy, idProvider, instrumentation, preparsedDocumentProvider);
  }
}

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

private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
  AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput);
  PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(),
      transformedQuery -> {
        // if they change the original query in the pre-parser, then we want to see it downstream from then on
        executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery)));
        return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState);
      });
  if (preparsedDoc.hasErrors()) {
    return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors()));
  }
  return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState);
}

代码示例来源:origin: bpatters/schemagen-graphql

@SuppressWarnings("rawtypes")
private UserDTO getUserNode(String id) throws IOException {
  ExecutionResult result = new GraphQL(schema).execute(String.format("{ node(id:\"%s\") {...on User {id, name, email} } }", id));
  assertEquals(0, result.getErrors().size());
  return objectMapper.readValue(objectMapper.writeValueAsString(((Map) result.getData()).get("node")), UserDTO.class);
}

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

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

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

private GraphQL buildSchema() {
  return GraphQL.newGraphQL(schema)
      .build();
}

代码示例来源: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: bpatters/schemagen-graphql

@SuppressWarnings("rawtypes")
private GameDTO getGameNode(String id) throws IOException {
  ExecutionResult result = new GraphQL(schema).execute(String.format("{ node(id:\"%s\") {...on Game {id, name} } }", id));
  assertEquals(0, result.getErrors().size());
  return objectMapper.readValue(objectMapper.writeValueAsString(((Map) result.getData()).get("node")), GameDTO.class);
}

代码示例来源:origin: opentripplanner/OpenTripPlanner

public Response getGraphQLResponse(String query, Map<String, Object> variables, String operationName) {
  ExecutionResult executionResult = graphQL.execute(query, operationName, null, variables);
  Response.ResponseBuilder res = Response.status(Response.Status.OK);
  HashMap<String, Object> content = new HashMap<>();
  if (!executionResult.getErrors().isEmpty()) {
    res = Response.status(Response.Status.INTERNAL_SERVER_ERROR);
    content.put("errors", executionResult.getErrors());
  }
  if (executionResult.getData() != null) {
    content.put("data", executionResult.getData());
  }
  return res.entity(content).build();
}

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

private GraphQL buildSchema() {
  return GraphQL.newGraphQL(schema)
      .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: google/rejoiner

new DataLoaderDispatcherInstrumentation(dataLoaderRegistry),
      new TracingInstrumentation()));
GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
    .context(dataLoaderRegistry)
    .build();
ExecutionResult executionResult = graphql.execute(executionInput);
resp.setContentType("application/json");
resp.setStatus(HttpServletResponse.SC_OK);

代码示例来源:origin: bpatters/schemagen-graphql

private DeleteUserPayload deleteUser(String id, String clientMutationId) throws IOException {
  ExecutionResult result = new GraphQL(schema).execute(String
      .format("mutation M { Mutations { deleteUser(input: { id:\"%s\", clientMutationId:\"%s\"}) {clientMutationId} } }", id, clientMutationId));
  assertEquals(0, result.getErrors().size());
  DeleteUserPayload payload = objectMapper.readValue(objectMapper.writeValueAsString(getMutationResults(result, "deleteUser")), DeleteUserPayload.class);
  assertEquals(clientMutationId, payload.getClientMutationId());
  return payload;
}

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

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

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

private GraphQL buildSchema() {
  return GraphQL.newGraphQL(schema)
      .build();
}

代码示例来源: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: opentripplanner/OpenTripPlanner

serviceCodes = graph.serviceCodes;
this.graph = graph;
graphQL = new GraphQL(
    new IndexGraphQLSchema(this).indexSchema,
    new ExecutorServiceExecutionStrategy(Executors.newCachedThreadPool(

代码示例来源:origin: google/rejoiner

new DataLoaderDispatcherInstrumentation(dataLoaderRegistry),
      new TracingInstrumentation()));
GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
    .context(dataLoaderRegistry)
    .build();
ExecutionResult executionResult = graphql.execute(executionInput);
resp.setContentType("application/json");
resp.setStatus(HttpServletResponse.SC_OK);

代码示例来源:origin: bpatters/schemagen-graphql

private DeleteGamePayload deleteGame(String id, String clientMutationId) throws IOException {
  ExecutionResult result = new GraphQL(schema).execute(
      String.format("mutation M { Mutations {deleteGame(input:{ id:\"%s\", clientMutationId:\"%s\"}) {clientMutationId} } }", id, clientMutationId));
  assertEquals(0, result.getErrors().size());
  DeleteGamePayload payload = objectMapper.readValue(objectMapper.writeValueAsString(getMutationResults(result, "deleteGame")), DeleteGamePayload.class);
  assertEquals(clientMutationId, payload.getClientMutationId());
  return payload;
}

相关文章