|
| 1 | +package org.aksw.rdf_processing_toolkit.cli.cmd.graphql; |
| 2 | + |
| 3 | +import java.io.OutputStreamWriter; |
| 4 | +import java.io.Writer; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.Callable; |
| 8 | + |
| 9 | +import org.aksw.commons.io.util.StdIo; |
| 10 | +import org.aksw.jena_sparql_api.rx.script.SparqlScriptProcessor; |
| 11 | +import org.aksw.jenax.dataaccess.sparql.datasource.RdfDataSource; |
| 12 | +import org.aksw.jenax.dataaccess.sparql.factory.datasource.RdfDataSources; |
| 13 | +import org.aksw.jenax.graphql.schema.generator.GraphQlSchemaGenerator; |
| 14 | +import org.aksw.jenax.graphql.schema.generator.GraphQlSchemaGenerator.TypeInfo; |
| 15 | +import org.aksw.jenax.stmt.core.SparqlStmt; |
| 16 | +import org.aksw.jenax.stmt.util.SparqlStmtUtils; |
| 17 | +import org.aksw.rdf_processing_toolkit.cli.cmd.CmdMixinSparqlDataset; |
| 18 | +import org.apache.jena.query.Dataset; |
| 19 | +import org.apache.jena.query.DatasetFactory; |
| 20 | +import org.apache.jena.rdfconnection.RDFConnection; |
| 21 | + |
| 22 | +import graphql.language.AstPrinter; |
| 23 | +import graphql.language.Document; |
| 24 | +import graphql.parser.Parser; |
| 25 | +import picocli.CommandLine.Command; |
| 26 | +import picocli.CommandLine.Mixin; |
| 27 | +import picocli.CommandLine.Option; |
| 28 | +import picocli.CommandLine.Parameters; |
| 29 | + |
| 30 | +@Command(name = "schemagen", description = "Generate a schema GraphQL Schema over RDF data in files or in a SPARQL endpoinst.") |
| 31 | +public class CmdGraphQlSchemaGen |
| 32 | + implements Callable<Integer> { |
| 33 | + |
| 34 | + @Option(names = { "-h", "--help" }, usageHelp = true) |
| 35 | + public boolean help = false; |
| 36 | + |
| 37 | + @Mixin |
| 38 | + public CmdMixinSparqlDataset sparqlDataset = new CmdMixinSparqlDataset(); |
| 39 | + |
| 40 | + @Parameters(arity = "0..*", description = "Input files") |
| 41 | + public List<String> nonOptionArgs = new ArrayList<>(); |
| 42 | + |
| 43 | + @Override |
| 44 | + public Integer call() throws Exception { |
| 45 | + SparqlScriptProcessor processor = SparqlScriptProcessor.createWithEnvSubstitution(null); |
| 46 | + processor.process(nonOptionArgs); |
| 47 | + |
| 48 | + Dataset dataset = DatasetFactory.create(); |
| 49 | + try (RDFConnection conn = RDFConnection.connect(dataset)) { |
| 50 | + for (SparqlStmt stmt : processor.getPlainSparqlStmts()) { |
| 51 | + SparqlStmtUtils.execAny(conn, stmt, null); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + RdfDataSource dataSource = RdfDataSources.of(dataset); |
| 56 | + List<TypeInfo> types = GraphQlSchemaGenerator.summarize(dataSource); |
| 57 | + |
| 58 | + GraphQlSchemaGenerator generator = new GraphQlSchemaGenerator(); |
| 59 | + Document doc = generator.process(types); |
| 60 | + String str = AstPrinter.printAst(doc); |
| 61 | + |
| 62 | + try (Writer writer = new OutputStreamWriter(StdIo.openStdOutWithCloseShield())) { |
| 63 | + writer.write(str); |
| 64 | + } |
| 65 | + |
| 66 | + boolean validateOutput = true; |
| 67 | + if (validateOutput) { |
| 68 | + Parser parser = new Parser(); |
| 69 | + parser.parse(str); |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | + } |
| 74 | +} |
0 commit comments