Search in sources :

Example 6 with SchemaParser

use of graphql.schema.idl.SchemaParser in project incubator-skywalking by apache.

the class GraphQLScriptTest method assertScriptFormat.

@Test
public void assertScriptFormat() {
    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    typeRegistry.merge(schemaParser.parse(loadSchema("common.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("trace.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("overview-layer.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("application-layer.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("server-layer.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("service-layer.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("alarm.graphqls")));
    typeRegistry.merge(schemaParser.parse(loadSchema("config.graphqls")));
    RuntimeWiring wiring = buildRuntimeWiring();
    assertTrue(schemaGenerator.makeExecutableSchema(typeRegistry, wiring).getAllTypesAsList().size() > 0);
}
Also used : RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaParser(graphql.schema.idl.SchemaParser) Test(org.junit.Test)

Example 7 with SchemaParser

use of graphql.schema.idl.SchemaParser in project graphql-java by graphql-java.

the class HttpMain method buildStarWarsSchema.

private GraphQLSchema buildStarWarsSchema() {
    // 
    if (starWarsSchema == null) {
        // 
        // 
        // the fetcher of friends uses java-dataloader to make the circular friends fetching
        // more efficient by batching and caching the calls to load Character friends
        // 
        DataFetcher friendsFetcher = environment -> {
            DataLoaderRegistry dataloaderRegistry = asMapGet(environment.getContext(), "dataloaderRegistry");
            DataLoader friendsDataLoader = dataloaderRegistry.getDataLoader("friends");
            List<String> friendIds = asMapGet(environment.getSource(), "friends");
            return friendsDataLoader.loadMany(friendIds);
        };
        // 
        // reads a file that provides the schema types
        // 
        Reader streamReader = loadSchemaFile("starWarsSchemaAnnotated.graphqls");
        TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);
        // 
        // the runtime wiring is used to provide the code that backs the
        // logical schema
        // 
        TypeResolver characterTypeResolver = env -> {
            Map<String, Object> obj = (Map<String, Object>) env.getObject();
            String id = (String) obj.get("id");
            GraphQLSchema schema = env.getSchema();
            if (StarWarsData.isHuman(id)) {
                return (GraphQLObjectType) schema.getType("Human");
            } else {
                return (GraphQLObjectType) schema.getType("Droid");
            }
        };
        RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring().type(newTypeWiring("Query").dataFetcher("hero", StarWarsData.getHeroDataFetcher()).dataFetcher("human", StarWarsData.getHumanDataFetcher()).dataFetcher("droid", StarWarsData.getDroidDataFetcher())).type(newTypeWiring("Human").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Droid").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Character").typeResolver(characterTypeResolver)).type(newTypeWiring("Episode").enumValues(StarWarsData.getEpisodeResolver())).build();
        // finally combine the logical schema with the physical runtime
        starWarsSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
    }
    return starWarsSchema;
}
Also used : Request(org.eclipse.jetty.server.Request) ExecutionInput.newExecutionInput(graphql.ExecutionInput.newExecutionInput) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) GraphQL(graphql.GraphQL) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) HttpServletRequest(javax.servlet.http.HttpServletRequest) BatchLoader(org.dataloader.BatchLoader) DataLoaderDispatcherInstrumentationOptions.newOptions(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions.newOptions) SchemaParser(graphql.schema.idl.SchemaParser) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) TypeResolver(graphql.schema.TypeResolver) Server(org.eclipse.jetty.server.Server) StarWarsData(graphql.StarWarsData) GraphQLObjectType(graphql.schema.GraphQLObjectType) HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletResponse(javax.servlet.http.HttpServletResponse) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) IOException(java.io.IOException) DataLoader(org.dataloader.DataLoader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TypeRuntimeWiring.newTypeWiring(graphql.schema.idl.TypeRuntimeWiring.newTypeWiring) ExecutionInput(graphql.ExecutionInput) ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) List(java.util.List) Instrumentation(graphql.execution.instrumentation.Instrumentation) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) InputStream(java.io.InputStream) TypeResolver(graphql.schema.TypeResolver) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) DataLoader(org.dataloader.DataLoader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) HandlerList(org.eclipse.jetty.server.handler.HandlerList) List(java.util.List) DataFetcher(graphql.schema.DataFetcher) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with SchemaParser

use of graphql.schema.idl.SchemaParser in project graphql-java by graphql-java.

the class ReadmeExamples method parsedSplitSchemaExample.

void parsedSplitSchemaExample() {
    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    File schemaFile1 = loadSchema("starWarsSchemaPart1.graphqls");
    File schemaFile2 = loadSchema("starWarsSchemaPart2.graphqls");
    File schemaFile3 = loadSchema("starWarsSchemaPart3.graphqls");
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    // each compiled registry is merged into the main registry
    typeRegistry.merge(schemaParser.parse(schemaFile1));
    typeRegistry.merge(schemaParser.parse(schemaFile2));
    typeRegistry.merge(schemaParser.parse(schemaFile3));
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, buildRuntimeWiring());
}
Also used : SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaParser(graphql.schema.idl.SchemaParser) File(java.io.File) GraphQLSchema(graphql.schema.GraphQLSchema)

Example 9 with SchemaParser

use of graphql.schema.idl.SchemaParser in project graphql-java by graphql-java.

the class BatchCompare method buildDataLoaderSchema.

GraphQLSchema buildDataLoaderSchema() {
    Reader streamReader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("storesanddepartments.graphqls"));
    TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(streamReader);
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().type(TypeRuntimeWiring.newTypeWiring("Query").dataFetcher("shops", BatchCompareDataFetchers.shopsDataFetcher)).type(TypeRuntimeWiring.newTypeWiring("Shop").dataFetcher("departments", BatchCompareDataFetchers.departmentsForShopDataLoaderDataFetcher)).type(TypeRuntimeWiring.newTypeWiring("Department").dataFetcher("products", BatchCompareDataFetchers.productsForDepartmentDataLoaderDataFetcher)).build();
    return new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
}
Also used : InputStreamReader(java.io.InputStreamReader) TypeRuntimeWiring(graphql.schema.idl.TypeRuntimeWiring) RuntimeWiring(graphql.schema.idl.RuntimeWiring) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SchemaParser(graphql.schema.idl.SchemaParser)

Aggregations

SchemaGenerator (graphql.schema.idl.SchemaGenerator)9 SchemaParser (graphql.schema.idl.SchemaParser)9 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)9 RuntimeWiring (graphql.schema.idl.RuntimeWiring)8 GraphQLSchema (graphql.schema.GraphQLSchema)6 ExecutionResult (graphql.ExecutionResult)4 GraphQL (graphql.GraphQL)4 RuntimeWiring.newRuntimeWiring (graphql.schema.idl.RuntimeWiring.newRuntimeWiring)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 DataFetcher (graphql.schema.DataFetcher)2 StaticDataFetcher (graphql.schema.StaticDataFetcher)2 TypeRuntimeWiring (graphql.schema.idl.TypeRuntimeWiring)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 GraphQLQueryResolver (com.coxautodev.graphql.tools.GraphQLQueryResolver)1