new DataLoaderDispatcherInstrumentation(dataLoaderRegistry), new TracingInstrumentation())); GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
new DataLoaderDispatcherInstrumentation(dataLoaderRegistry), new TracingInstrumentation())); GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();
private void exampleExecutorServiceExecutionStrategy() { //::FigureH ExecutorService executorService = new ThreadPoolExecutor( 2, /* core pool size 2 thread */ 2, /* max pool size 2 thread */ 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy()); GraphQL graphQL = GraphQL.newGraphQL(StarWarsSchema.starWarsSchema) .queryExecutionStrategy(new ExecutorServiceExecutionStrategy(executorService)) .mutationExecutionStrategy(new AsyncSerialExecutionStrategy()) .build(); //::/FigureH }
private void fieldValidation() { ExecutionPath fieldPath = ExecutionPath.parse("/user"); FieldValidation fieldValidation = new SimpleFieldValidation() .addRule(fieldPath, new BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>>() { @Override public Optional<GraphQLError> apply(FieldAndArguments fieldAndArguments, FieldValidationEnvironment environment) { String nameArg = fieldAndArguments.getArgumentValue("name"); if (nameArg.length() > 255) { return Optional.of(environment.mkError("Invalid user name", fieldAndArguments)); } return Optional.empty(); } }); FieldValidationInstrumentation instrumentation = new FieldValidationInstrumentation( fieldValidation ); GraphQL.newGraphQL(schema) .instrumentation(instrumentation) .build(); }
/** * This helps you transform the current GraphQL 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 GraphQL object based on calling build on that builder */ public GraphQL transform(Consumer<GraphQL.Builder> builderConsumer) { Builder builder = new Builder(this.graphQLSchema); builder .queryExecutionStrategy(nvl(this.queryStrategy, builder.queryExecutionStrategy)) .mutationExecutionStrategy(nvl(this.mutationStrategy, builder.mutationExecutionStrategy)) .subscriptionExecutionStrategy(nvl(this.subscriptionStrategy, builder.subscriptionExecutionStrategy)) .executionIdProvider(nvl(this.idProvider, builder.idProvider)) .instrumentation(nvl(this.instrumentation, builder.instrumentation)) .preparsedDocumentProvider(nvl(this.preparsedDocumentProvider, builder.preparsedDocumentProvider)); builderConsumer.accept(builder); return builder.build(); }
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' } }
@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")); } }
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()); }
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} }
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} } }
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 }
private GraphQL buildSchema() { return GraphQL.newGraphQL(schema) .build(); }
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()); }
private GraphQL buildSchema() { return GraphQL.newGraphQL(schema) .build(); }
void executionStrategies() { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( 2, /* core pool size 2 thread */ 2, /* max pool size 2 thread */ 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy()); GraphQL graphQL = GraphQL.newGraphQL(StarWarsSchema.starWarsSchema) .queryExecutionStrategy(new ExecutorServiceExecutionStrategy(threadPoolExecutor)) .mutationExecutionStrategy(new AsyncExecutionStrategy()) .subscriptionExecutionStrategy(new AsyncExecutionStrategy()) .build(); }
private GraphQL buildSchema() { return GraphQL.newGraphQL(schema) .build(); }
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(); } }
private void wireInExecutionStrategies() { //::FigureG GraphQL.newGraphQL(schema) .queryExecutionStrategy(new AsyncExecutionStrategy()) .mutationExecutionStrategy(new AsyncSerialExecutionStrategy()) .build(); //::/FigureG }
private void chained() { List<Instrumentation> chainedList = new ArrayList<>(); chainedList.add(new FooInstrumentation()); chainedList.add(new BarInstrumentation()); ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(chainedList); GraphQL.newGraphQL(schema) .instrumentation(chainedInstrumentation) .build(); }
private void specifyInstrumentation() { GraphQL.newGraphQL(schema) .instrumentation(new TracingInstrumentation()) .build(); }