本文整理汇总了Java中graphql.schema.GraphQLSchema类的典型用法代码示例。如果您正苦于以下问题:Java GraphQLSchema类的具体用法?Java GraphQLSchema怎么用?Java GraphQLSchema使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GraphQLSchema类属于graphql.schema包,在下文中一共展示了GraphQLSchema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_helloWorld
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void test_helloWorld(TestContext context) {
Async async = context.async();
GraphQLObjectType query = GraphQLObjectType.newObject()
.name("query")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("hello")
.type(Scalars.GraphQLString)
.dataFetcher(environment -> {
return "world";
}))
.build();
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(query)
.build();
AsyncGraphQLExec asyncGraphQL = AsyncGraphQLExec.create(schema);
Future<JsonObject> queryResult = asyncGraphQL.executeQuery("query { hello }", null, null, null);
queryResult.setHandler(res -> {
JsonObject json = res.result();
context.assertEquals(new JsonObject().put("hello", "world"), json);
async.complete();
});
}
示例2: getAllTypes
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@SuppressWarnings("JdkObsolete")
static Set<GraphQLType> getAllTypes(GraphQLSchema schema) {
LinkedHashSet<GraphQLType> types = new LinkedHashSet<>();
LinkedList<GraphQLObjectType> loop = new LinkedList<>();
loop.add(schema.getQueryType());
types.add(schema.getQueryType());
while (!loop.isEmpty()) {
for (GraphQLFieldDefinition field : loop.pop().getFieldDefinitions()) {
GraphQLType type = field.getType();
if (type instanceof GraphQLList) {
type = ((GraphQLList) type).getWrappedType();
}
if (!types.contains(type)) {
if (type instanceof GraphQLEnumType) {
types.add(field.getType());
}
if (type instanceof GraphQLObjectType) {
types.add(type);
loop.add((GraphQLObjectType) type);
}
}
}
}
return types;
}
示例3: emptySchemaShouldGenerateCorrectProto
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void emptySchemaShouldGenerateCorrectProto() {
assertThat(
SchemaToProto.toProto(
GraphQLSchema.newSchema()
.query(GraphQLObjectType.newObject().name("queryType"))
.build()))
.isEqualTo(
"// LINT: LEGACY_NAMES\n"
+ "// Autogenerated. Do not edit.\n"
+ "syntax = \"proto3\";\n"
+ "\n"
+ "package google.api.graphql.rejoiner.proto;\n"
+ "\n"
+ "option java_package = \"com.google.api.graphql.rejoiner.proto\";\n"
+ "option (jspb.js_namespace) = \"com.google.api.graphql.rejoiner.proto\";\n"
+ "import \"java/com/google/apps/jspb/jspb.proto\";\n"
+ "\n"
+ "message queryType {\n"
+ "option (jspb.message_id) = \"graphql.queryType\";\n"
+ "}");
}
示例4: schemaModuleShouldProvideQueryType
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void schemaModuleShouldProvideQueryType() {
Injector injector =
Guice.createInjector(
new SchemaProviderModule(),
new SchemaModule() {
@Query
GraphQLFieldDefinition greeting =
GraphQLFieldDefinition.newFieldDefinition()
.name("greeting")
.type(Scalars.GraphQLString)
.staticValue("hello world")
.build();
});
assertThat(
injector
.getInstance(Key.get(GraphQLSchema.class, Schema.class))
.getQueryType()
.getFieldDefinition("greeting"))
.isNotNull();
}
示例5: testGraphQl
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testGraphQl() {
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}
}
示例6: testCompletableFuture
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testCompletableFuture() throws Exception {
ReactiveExecutionStrategy strategy = new ReactiveExecutionStrategy();
GraphQLSchema schema = newQuerySchema(it -> it
.field(newStringField("a").dataFetcher(env -> CompletableFuture.completedFuture("static")))
);
ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a }");
Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber);
subscriber
.assertChanges(it -> it.containsExactly(
tuple("00:000", "", ImmutableMap.of("a", "static"))
))
.assertComplete();
}
示例7: testPlainField
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testPlainField() throws Exception {
GraphQLSchema schema = newQuerySchema(it -> it
.field(newLongField("a").dataFetcher(env -> Flowable.interval(1, SECONDS, scheduler).take(2)))
);
ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a }");
assertThat(executionResult)
.isNotNull()
.satisfies(it -> assertThat(it.<Publisher<Change>>getData()).isNotNull().isInstanceOf(Publisher.class));
Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber);
scheduler.advanceTimeBy(2, SECONDS);
subscriber
.assertChanges(it -> it.containsExactly(
tuple("01:000", "", ImmutableMap.of("a", 0L)),
tuple("02:000", "a", 1L)
))
.assertComplete();
}
示例8: testStartWith
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testStartWith() throws Exception {
GraphQLSchema schema = newQuerySchema(it -> it
.field(newLongField("a").dataFetcher(env -> Flowable.interval(1, SECONDS, scheduler).take(2).startWith(-42L)))
);
ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a }");
Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber);
scheduler.advanceTimeBy(2, SECONDS);
subscriber
.assertChanges(it -> it.containsExactly(
tuple("00:000", "", ImmutableMap.of("a", -42L)),
tuple("01:000", "a", 0L),
tuple("02:000", "a", 1L)
))
.assertComplete();
}
示例9: testAnyPublisher
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testAnyPublisher() throws Exception {
Duration tick = Duration.ofSeconds(1);
GraphQLSchema schema = newQuerySchema(it -> it
// use Flux from Project Reactor
.field(newLongField("a").dataFetcher(env -> Flux.interval(tick).take(2)))
);
ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a }");
// Use Reactor's StepVerifier
StepVerifier.withVirtualTime(() -> (Publisher<Change>) executionResult.getData())
.expectSubscription()
.expectNoEvent(tick)
.assertNext(matchChange("", ImmutableMap.of("a", 0L)))
.expectNoEvent(tick)
.assertNext(matchChange("a", 1L))
.verifyComplete();
}
示例10: testStaticValues
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testStaticValues() throws Exception {
GraphQLSchema schema = newQuerySchema(it -> it
.field(newStringField("a").dataFetcher(env -> "staticA"))
.field(newStringField("b").dataFetcher(env -> "staticB"))
);
ExecutionResult executionResult = new GraphQL(schema, strategy).execute("{ a, b }");
Flowable.fromPublisher((Publisher<Change>) executionResult.getData()).timestamp(scheduler).subscribe(subscriber);
subscriber
.assertChanges(it -> it.containsExactly(
tuple("00:000", "", ImmutableMap.of("a", "staticA", "b", "staticB"))
))
.assertComplete();
}
示例11: GraphQlController
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Autowired
public GraphQlController(PersonQuery personQuery) {
//Schema generated from query classes
GraphQLSchema schemaFromAnnotated = new GraphQLSchemaGenerator()
.withResolverBuilders(
//Resolve by annotations
new AnnotatedResolverBuilder(),
//Resolve public methods inside root package
new PublicResolverBuilder("application"))
.withOperationsFromSingleton(personQuery)
.withValueMapperFactory(new JacksonValueMapperFactory())
.generate();
graphQlFromAnnotated = GraphQL.newGraphQL(schemaFromAnnotated).build();
LOGGER.info("Generated GraphQL schema using SPQR");
}
示例12: testMutation
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testMutation() {
GraphQLSchema schema = GraphQLSchemaBuilder.newBuilder()
.registerTypeFactory(new JacksonTypeFactory(new ObjectMapper()))
.registerGraphQLControllerObjects(ImmutableList.<Object> of(new TestController()))
.build();
GraphQLFieldDefinition mutationType = schema.getMutationType().getFieldDefinition("setName");
assertEquals("setName", mutationType.getName());
assertEquals(Scalars.GraphQLString, mutationType.getArgument("name").getType());
ExecutionResult result = new GraphQL(schema).execute("mutation M { setName(name: \"The new name\") }");
String newName = ((Map<String, String>) result.getData()).get("setName");
assertEquals(0, result.getErrors().size());
assertEquals("The new name", newName);
result = new GraphQL(schema).execute("query Q { getName }");
newName = ((Map<String, String>) result.getData()).get("getName");
assertEquals(0, result.getErrors().size());
assertEquals("The new name", newName);
}
示例13: testDocumentedControllerAndMethods
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testDocumentedControllerAndMethods() {
GraphQLSchema schema = GraphQLSchemaBuilder.newBuilder()
.registerTypeFactory(new JacksonTypeFactory(new ObjectMapper()))
.registerGraphQLControllerObjects(ImmutableList.<Object>of(new DocumentedMethodsTest()))
.build();
GraphQLObjectType mutationType = (GraphQLObjectType) schema.getMutationType().getFieldDefinition("Mutations").getType();
GraphQLObjectType queryType = (GraphQLObjectType) schema.getQueryType().getFieldDefinition("Queries").getType();;
assertEquals("Mutation Description", mutationType.getDescription());
assertEquals("Query Description", queryType.getDescription());
GraphQLFieldDefinition someQuery = queryType.getFieldDefinition("getSomeStrings");
GraphQLFieldDefinition someMutation = mutationType.getFieldDefinition("setSomeStrings");
assertEquals("getSomeStrings description", someQuery.getDescription());
assertEquals("setSomeStrings description", someMutation.getDescription());
}
示例14: buildSchema
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
private void buildSchema(GraphQLObjectType.Builder queryBuilder, GraphQLObjectType.Builder mutationBuilder, boolean foundQueryDefinitions, boolean foundMutationDefinitions) {
log.debug("Start building graphql schema");
GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();
if (foundQueryDefinitions) {
schemaBuilder = schemaBuilder.query(queryBuilder.build());
}
if (foundMutationDefinitions) {
schemaBuilder = schemaBuilder.mutation(mutationBuilder.build());
}
if (foundQueryDefinitions || foundMutationDefinitions) {
schema = schemaBuilder.build();
} else {
schema = generateGettingStartedGraphQlSchema();
}
}
示例15: testVariableBoundGenerics
import graphql.schema.GraphQLSchema; //导入依赖的package包/类
@Test
public void testVariableBoundGenerics() {
Type type = TypeFactory.parameterizedClass(EchoService.class, Number.class);
GraphQLSchema schema = new TestSchemaGenerator()
.withResolverBuilders(new PublicResolverBuilder("io.leangen"))
.withOperationsFromSingleton(new EchoService(), type)
.generate();
GraphQLFieldDefinition query = schema.getQueryType().getFieldDefinition("echo");
assertEquals(Scalars.GraphQLBigDecimal, query.getType());
assertEquals(Scalars.GraphQLBigDecimal, query.getArgument("in").getType());
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQL.execute("{ echo (in: 3) }");
assertTrue(ERRORS, result.getErrors().isEmpty());
assertValueAtPathEquals(new BigDecimal(3), result, "echo");
}