graphql.schema.idl.SchemaGenerator Java Examples

The following examples show how to use graphql.schema.idl.SchemaGenerator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocaleTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("locale.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("locale", this::getLocale))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #2
Source File: VertxGraphqlResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public void setupRouter(@Observes Router router) {
    String schema = "type Query{hello: String}";

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

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

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

    GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

    router.route("/graphql").handler(ApolloWSHandler.create(graphQL));
    router.route("/graphql").handler(GraphQLHandler.create(graphQL));
}
 
Example #3
Source File: GraphQLIntegrationTest.java    From rsocket-rpc-java with Apache License 2.0 6 votes vote down vote up
private static GraphQLSchema getGraphQLSchema() throws Exception {
  SchemaParser schemaParser = new SchemaParser();
  SchemaGenerator schemaGenerator = new SchemaGenerator();

  URL resource = Thread.currentThread().getContextClassLoader().getResource("schema.graphqls");
  Path path = Paths.get(resource.toURI());
  String s = read(path);

  TypeDefinitionRegistry registry = schemaParser.parse(s);

  RuntimeWiring wiring =
      RuntimeWiring.newRuntimeWiring()
          .type(
              newTypeWiring("Query")
                  .dataFetcher("bookById", GraphQLDataFetchers.getBookByIdDataFetcher()))
          .type(
              newTypeWiring("Book")
                  .dataFetcher("author", GraphQLDataFetchers.getAuthorDataFetcher()))
          .build();

  return schemaGenerator.makeExecutableSchema(registry, wiring);
}
 
Example #4
Source File: GraphQLFactory.java    From dropwizard-graphql with Apache License 2.0 6 votes vote down vote up
public GraphQLSchema build() throws SchemaProblem {
  if (graphQLSchema.isPresent()) {
    return graphQLSchema.get();
  }

  final SchemaParser parser = new SchemaParser();
  final TypeDefinitionRegistry registry = new TypeDefinitionRegistry();

  if (!schemaFiles.isEmpty()) {
    schemaFiles.stream()
        .filter(f -> !Strings.isNullOrEmpty(f))
        .map(f -> getResourceAsReader(f))
        .map(r -> parser.parse(r))
        .forEach(p -> registry.merge(p));
  }

  final SchemaGenerator generator = new SchemaGenerator();
  final GraphQLSchema schema = generator.makeExecutableSchema(registry, runtimeWiring);
  return schema;
}
 
Example #5
Source File: ApolloWSHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("counter.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("staticCounter", this::getStaticCounter))
    .type("Subscription", builder -> builder.dataFetcher("counter", this::getCounter))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #6
Source File: GraphQLFactory.java    From micronaut-graphql with Apache License 2.0 6 votes vote down vote up
@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver, HelloDataFetcher helloDataFetcher) { // <2>

    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();

    // Parse the schema.
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    typeRegistry.merge(schemaParser.parse(new BufferedReader(new InputStreamReader(
            resourceResolver.getResourceAsStream("classpath:schema.graphqls").get()))));

    // Create the runtime wiring.
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Query", typeWiring -> typeWiring
                    .dataFetcher("hello", helloDataFetcher))
            .build();

    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);

    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #7
Source File: MultipartRequestTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private GraphQL graphQL() {
  final String schema = vertx.fileSystem().readFileBlocking("upload.graphqls").toString();
  final String emptyQueryschema = vertx.fileSystem().readFileBlocking("emptyQuery.graphqls").toString();

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

  final RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
    .scalar(UploadScalar.build())
    .type("Mutation", builder -> {
      builder.dataFetcher("singleUpload", this::singleUpload);
      builder.dataFetcher("multipleUpload", this::multipleUpload);
      return builder;
    }).build();

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

  return GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #8
Source File: GraphQLTestBase.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #9
Source File: VertxMappedBatchLoaderTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .type("Link", builder -> builder.dataFetcher("postedBy", this::getLinkPostedBy))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .instrumentation(dispatcherInstrumentation)
    .build();
}
 
Example #10
Source File: VertxDataFetcherTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> {
      VertxDataFetcher<Object> dataFetcher = VertxDataFetcher.create((env, fut) -> fut.complete(getAllLinks(env)));
      return builder.dataFetcher("allLinks", dataFetcher);
    })
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #11
Source File: VertxBatchLoaderTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
protected GraphQL graphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .type("Link", builder -> builder.dataFetcher("postedBy", this::getLinkPostedBy))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .instrumentation(dispatcherInstrumentation)
    .build();
}
 
Example #12
Source File: ApolloTestsServer.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private GraphQL setupGraphQL() {
  String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();
  String uploadSchema = vertx.fileSystem().readFileBlocking("upload.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .scalar(UploadScalar.build())
    .type("Query", builder -> builder.dataFetcher("allLinks", this::getAllLinks))
    .type("Mutation", builder -> builder.dataFetcher("singleUpload", this::singleUpload))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #13
Source File: FederationTest.java    From federation-jvm with MIT License 6 votes vote down vote up
@Test
void testPrinterEmpty() {
    TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(printerEmptySDL);
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Interface1", typeWiring -> typeWiring
                    .typeResolver(env -> null)
            )
            .type("Interface2", typeWiring -> typeWiring
                    .typeResolver(env -> null)
            )
            .build();
    GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(
            typeDefinitionRegistry,
            runtimeWiring
    );
    Assertions.assertEquals(
            printerEmptySDL.trim(),
            new FederationSdlPrinter(FederationSdlPrinter.Options.defaultOptions()
                    .includeDirectiveDefinitions(def -> !standardDirectives.contains(def.getName()))
            ).print(graphQLSchema).trim()
    );
}
 
Example #14
Source File: ApolloTestsServer.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private GraphQL setupWsGraphQL() {
  String schema = vertx.fileSystem().readFileBlocking("counter.graphqls").toString();

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

  RuntimeWiring runtimeWiring = newRuntimeWiring()
    .type("Query", builder -> builder.dataFetcher("staticCounter", this::staticCounter))
    .type("Subscription", builder -> builder.dataFetcher("counter", this::counter))
    .build();

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

  return GraphQL.newGraphQL(graphQLSchema)
    .build();
}
 
Example #15
Source File: GraphqlHandler.java    From selenium with Apache License 2.0 6 votes vote down vote up
public GraphqlHandler(Distributor distributor, URI publicUri) {
  this.distributor = Objects.requireNonNull(distributor);
  this.publicUri = Objects.requireNonNull(publicUri);

  GraphQLSchema schema = new SchemaGenerator()
    .makeExecutableSchema(buildTypeDefinitionRegistry(), buildRuntimeWiring());

  Cache<String, PreparsedDocumentEntry> cache = CacheBuilder.newBuilder()
    .maximumSize(1024)
    .build();

  graphQl = GraphQL.newGraphQL(schema)
    .preparsedDocumentProvider((executionInput, computeFunction) -> {
      try {
        return cache.get(executionInput.getQuery(), () -> computeFunction.apply(executionInput));
      } catch (ExecutionException e) {
        if (e.getCause() instanceof RuntimeException) {
          throw (RuntimeException) e.getCause();
        } else if (e.getCause() != null) {
          throw new RuntimeException(e.getCause());
        }
        throw new RuntimeException(e);
      }
    })
    .build();
}
 
Example #16
Source File: GraphQLProvider.java    From graphql-java-examples with MIT License 5 votes vote down vote up
private GraphQLSchema buildSchema(String sdl) {
    TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
    RuntimeWiring runtimeWiring = buildWiring();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    return graphQLSchema;
}
 
Example #17
Source File: BarleyGraphQLSchema.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public BarleyGraphQLSchema(SpecRegistry specRegistry, Environment env, String namespace, CustomQueries customQueries) {
	this.specRegistry = specRegistry;
	this.env = env;
	this.namespace = namespace;
	this.queryCustomizations = new GraphQLQueryCustomizations();
	this.queryCustomizations.setShouldBreakPredicate(new DefaultQueryBreaker(env, namespace, 3, 4));

       GenerateGrapqlSDL graphSdl = new GenerateGrapqlSDL(specRegistry, customQueries);
       
       SchemaParser schemaParser = new SchemaParser();
       sdlString = graphSdl.createSdl();
       LOG.info(sdlString);
       System.out.println(sdlString);
       
       TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(sdlString);
       RuntimeWiring.Builder wiringBuilder = newRuntimeWiring()
               .type("Query", builder -> builder.defaultDataFetcher(new QueryDataFetcher(env, namespace, customQueries)));
       
       specRegistry.getDefinitions().stream()
       .map(DefinitionsSpec::getEntitySpecs)
       .flatMap(Collection::stream)
       .forEach(eSpec -> wiringBuilder.type(getSimpleName(eSpec.getClassName()), builder ->  builder.defaultDataFetcher(new EntityDataFetcher(env, namespace))));
       
        RuntimeWiring runtimeWiring = wiringBuilder.build();        
       
       SchemaGenerator schemaGenerator = new SchemaGenerator();
       this.graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
}
 
Example #18
Source File: StockTickerGraphqlPublisher.java    From graphql-java-subscription-example with MIT License 5 votes vote down vote up
private GraphQLSchema buildSchema() {
    //
    // reads a file that provides the schema types
    //
    Reader streamReader = loadSchemaFile("stocks.graphqls");
    TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);

    RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring()
            .type(newTypeWiring("Subscription")
                    .dataFetcher("stockQuotes", stockQuotesSubscriptionFetcher())
            )
            .build();

    return new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
}
 
Example #19
Source File: GraphQLFactory.java    From micronaut-graphql with Apache License 2.0 5 votes vote down vote up
@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver,
        MessagesDataFetcher messagesDataFetcher,
        ChatDataFetcher chatDataFetcher,
        StreamDataFetcher streamDataFetcher) {

    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();

    // Parse the schema.
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();

    resourceResolver
            .getResourceAsStream("classpath:schema.graphqls")
            .ifPresent(s -> typeRegistry.merge(schemaParser.parse(new BufferedReader(new InputStreamReader(s)))));

    // Create the runtime wiring.
    RuntimeWiring runtimeWiring = RuntimeWiring
            .newRuntimeWiring()
            .scalar(ExtendedScalars.DateTime)
            .type("QueryRoot", typeWiring -> typeWiring
                    .dataFetcher("messages", messagesDataFetcher))
            .type("MutationRoot", typeWiring -> typeWiring
                    .dataFetcher("chat", chatDataFetcher))
            .type("SubscriptionRoot", typeWiring -> typeWiring
                    .dataFetcher("stream", streamDataFetcher))
            .build();

    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);

    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #20
Source File: Federation.java    From federation-jvm with MIT License 5 votes vote down vote up
public static SchemaTransformer transform(final TypeDefinitionRegistry typeRegistry, final RuntimeWiring runtimeWiring) {
    ensureQueryTypeExists(typeRegistry);
    RuntimeWiring newRuntimeWiring = ensureFederationDirectiveDefinitionsExist(typeRegistry, runtimeWiring);
    final GraphQLSchema original = new SchemaGenerator().makeExecutableSchema(
            generatorOptions,
            typeRegistry,
            newRuntimeWiring);
    return transform(original);
}
 
Example #21
Source File: FederatedTracingInstrumentationTest.java    From federation-jvm with MIT License 5 votes vote down vote up
@BeforeEach
void setupSchema() {
    TypeDefinitionRegistry typeDefs = new SchemaParser().parse(tracingSDL);
    RuntimeWiring resolvers = RuntimeWiring.newRuntimeWiring()
            .type("Query", builder ->
                    // return two items
                    builder.dataFetcher("widgets", env -> {
                        ArrayList<Object> objects = new ArrayList<>(2);
                        objects.add(new Object());
                        objects.add(new Object());
                        return objects;
                    }).dataFetcher("listOfLists", env -> {
                        ArrayList<ArrayList<Object>> lists = new ArrayList<>(2);
                        lists.add(new ArrayList<>(2));
                        lists.add(new ArrayList<>(2));
                        lists.get(0).add(new Object());
                        lists.get(0).add(new Object());
                        lists.get(1).add(new Object());
                        lists.get(1).add(new Object());
                        return lists;
                    })
                            .dataFetcher("listOfScalars", env -> new String[]{"one", "two", "three"}))
            .type("Widget", builder ->
                    // Widget.foo works normally, Widget.bar always throws an error
                    builder.dataFetcher("foo", env -> "hello world")
                            .dataFetcher("bar", env -> {
                                throw new GraphQLException("whoops");
                            }))
            .build();

    GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefs, resolvers);
    graphql = GraphQL.newGraphQL(graphQLSchema)
            .instrumentation(new FederatedTracingInstrumentation())
            .build();
}
 
Example #22
Source File: FederationTest.java    From federation-jvm with MIT License 5 votes vote down vote up
@Test
void testPrinterFilter() {
    TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(printerFilterSDL);
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Interface1", typeWiring -> typeWiring
                    .typeResolver(env -> null)
            )
            .type("Interface2", typeWiring -> typeWiring
                    .typeResolver(env -> null)
            )
            .scalar(GraphQLScalarType.newScalar()
                    .name("Scalar1")
                    .coercing(Scalars.GraphQLString.getCoercing())
                    .build()
            )
            .scalar(GraphQLScalarType.newScalar()
                    .name("Scalar2")
                    .coercing(Scalars.GraphQLString.getCoercing())
                    .build()
            )
            .build();
    GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(
            typeDefinitionRegistry,
            runtimeWiring
    );
    Assertions.assertEquals(
            printerFilterExpectedSDL.trim(),
            new FederationSdlPrinter(FederationSdlPrinter.Options.defaultOptions()
                    .includeScalarTypes(true)
                    .includeDirectiveDefinitions(def ->
                            !def.getName().endsWith("1") && !standardDirectives.contains(def.getName())
                    )
                    .includeTypeDefinitions(def -> !def.getName().endsWith("1"))
            ).print(graphQLSchema).trim()
    );
}
 
Example #23
Source File: GraphQLResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
public void setupRouter(@Observes Router router) {
    SchemaParser schemaParser = new SchemaParser();
    final TypeDefinitionRegistry typeDefinitionRegistry;
    try (Reader r = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("graphql/schema.graphql"),
            StandardCharsets.UTF_8)) {
        typeDefinitionRegistry = schemaParser.parse(r);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    DataFetcher<CompletionStage<Book>> dataFetcher = environment -> {
        CompletableFuture<Book> completableFuture = new CompletableFuture<>();
        Book book = getBookById(environment);
        completableFuture.complete(book);
        return completableFuture;
    };

    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Query", builder -> builder.dataFetcher("bookById", dataFetcher))
            .build();

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

    GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

    router.route("/graphql/server").handler(GraphQLHandler.create(graphQL));
}
 
Example #24
Source File: GraphQLService.java    From sfg-blog-posts with GNU General Public License v3.0 5 votes vote down vote up
@PostConstruct
private void loadSchema() throws IOException {
    logger.info("Entering loadSchema@GraphQLService");
    loadDataIntoHSQL();

    //Get the graphql file
    File file = resource.getFile();

    //Parse SchemaF
    TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(file);
    RuntimeWiring runtimeWiring = buildRuntimeWiring();
    GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
    graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #25
Source File: GraphqlServlet.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Override
public void init() throws ServletException {
    gson = new Gson();
    TypeDefinitionRegistry typeRegistry;
    RuntimeWiring wiring;
    try {
        typeRegistry = buildTypeDefinitionRegistry();
        wiring = buildRuntimeWiring();
    } catch (IOException e) {
        throw new RuntimeException("Failed to initialize GraphQL schema", e);
    }
    GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
    graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #26
Source File: GraphQLIntrospectionFileGenerator.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
public void generate(String sourceSdlResource, String targetIntrospectionFile) {
  String schema = readResource(sourceSdlResource);
  TypeDefinitionRegistry registry = new SchemaParser().parse(schema);
  RuntimeWiring wiring = RuntimeWiring
      .newRuntimeWiring()
      .scalar(objectNodeScalar())
      .build();
  GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
  GraphQL graphql = GraphQL.newGraphQL(graphQLSchema).build();
  String introspectionQuery = readResource("introspection.query");
  ExecutionResult result = graphql.execute(introspectionQuery);
  String introspectionJson = serializeToJson(result);
  writeFile(targetIntrospectionFile, introspectionJson);
}
 
Example #27
Source File: GraphQLFactory.java    From micronaut-graphql with Apache License 2.0 5 votes vote down vote up
@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver,
        ToDosDataFetcher toDosDataFetcher,
        CreateToDoDataFetcher createToDoDataFetcher,
        CompleteToDoDataFetcher completeToDoDataFetcher,
        DeleteToDoDataFetcher deleteToDoDataFetcher) {

    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();

    // Parse the schema.
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    typeRegistry.merge(schemaParser.parse(new BufferedReader(new InputStreamReader(
            resourceResolver.getResourceAsStream("classpath:schema.graphqls").get()))));

    // Create the runtime wiring.
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
            .type("Query", typeWiring -> typeWiring
                    .dataFetcher("toDos", toDosDataFetcher))
            .type("Mutation", typeWiring -> typeWiring
                    .dataFetcher("createToDo", createToDoDataFetcher)
                    .dataFetcher("completeToDo", completeToDoDataFetcher)
                    .dataFetcher("deleteToDo", deleteToDoDataFetcher))
            .build();

    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);

    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
 
Example #28
Source File: GraphQLProvider.java    From besu with Apache License 2.0 5 votes vote down vote up
private static GraphQLSchema buildSchema(
    final String sdl, final GraphQLDataFetchers graphQLDataFetchers) {
  final TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
  final RuntimeWiring runtimeWiring = buildWiring(graphQLDataFetchers);
  final SchemaGenerator schemaGenerator = new SchemaGenerator();
  return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
}
 
Example #29
Source File: GraphQLConfigurationProvider.java    From samples with MIT License 5 votes vote down vote up
private GraphQLSchema createSchema() {
  TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(loadSchemaFile());

  RuntimeWiring runtimeWiring = newRuntimeWiring()
      .type(newTypeWiring("Query").dataFetcher("hello", new StaticDataFetcher("world")))
      .type(newTypeWiring("Subscription").dataFetcher("ping", pingFetcher()))
      .build();

  SchemaGenerator schemaGenerator = new SchemaGenerator();
  return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
}
 
Example #30
Source File: StockTickerGraphqlPublisher.java    From graphql-java-examples with MIT License 5 votes vote down vote up
private GraphQLSchema buildSchema() {
    //
    // reads a file that provides the schema types
    //
    Reader streamReader = loadSchemaFile("stocks.graphqls");
    TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);

    RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring()
            .type(newTypeWiring("Subscription")
                    .dataFetcher("stockQuotes", stockQuotesSubscriptionFetcher())
            )
            .build();

    return new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
}