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

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

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

GraphQL.execute介绍

[英]Executes the graphql query using the provided input object builder
[中]使用提供的输入对象生成器执行graphql查询

代码示例

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

/**
 * 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

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

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

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

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

public void toSpec() throws Exception {
  //::FigureM
  ExecutionResult executionResult = graphQL.execute(executionInput);
  Map<String, Object> toSpecificationResult = executionResult.toSpecification();
  sendAsJson(toSpecificationResult);
  //::/FigureM
}

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

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

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

/**
 * 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}
 * @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, Object context, Map<String, Object> variables) {
  ExecutionInput executionInput = ExecutionInput.newExecutionInput()
      .query(query)
      .context(context)
      .root(context) // This we are doing do be backwards compatible
      .variables(variables)
      .build();
  return execute(executionInput);
}

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

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

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

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

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

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

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

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

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

public static void main(String[] args) {
    String schema = "type Query{hello: String}";

    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

    RuntimeWiring runtimeWiring = newRuntimeWiring()
        .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
        .build();

    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);

    GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
    ExecutionResult executionResult = build.execute("{hello}");

    System.out.println(executionResult.getData().toString());
    // Prints: {hello=world}
  }
}

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

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

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

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
}

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

public static void main(String[] args) {
  GraphQLObjectType queryType = newObject()
      .name("helloWorldQuery")
      .field(newFieldDefinition()
          .type(GraphQLString)
          .name("hello")
          .staticValue("world"))
      .build();
  GraphQLSchema schema = GraphQLSchema.newSchema()
      .query(queryType)
      .build();
  GraphQL graphQL = GraphQL.newGraphQL(schema).build();
  Map<String, Object> result = graphQL.execute("{hello}").getData();
  System.out.println(result);
  // Prints: {hello=world}
}

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

@Test
  public void helloWorldTest() {
    GraphQLObjectType queryType = newObject()
        .name("helloWorldQuery")
        .field(newFieldDefinition()
            .type(GraphQLString)
            .name("hello")
            .staticValue("world"))
        .build();

    GraphQLSchema schema = GraphQLSchema.newSchema()
        .query(queryType)
        .build();
    GraphQL graphQL = GraphQL.newGraphQL(schema).build();
    Map<String, Object> result = graphQL.execute("{hello}").getData();
    assertEquals("world", result.get("hello"));
  }
}

相关文章