Search in sources :

Example 1 with ResourceResolver

use of io.micronaut.core.io.ResourceResolver in project micronaut-graphql by micronaut-projects.

the class GraphQLFactory method graphQL.

@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();
}
Also used : Bean(io.micronaut.context.annotation.Bean) ResourceResolver(io.micronaut.core.io.ResourceResolver) GraphQL(graphql.GraphQL) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Factory(io.micronaut.context.annotation.Factory) GraphQLSchema(graphql.schema.GraphQLSchema) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Singleton(jakarta.inject.Singleton) SchemaGenerator(graphql.schema.idl.SchemaGenerator) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) InputStreamReader(java.io.InputStreamReader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) BufferedReader(java.io.BufferedReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Example 2 with ResourceResolver

use of io.micronaut.core.io.ResourceResolver in project micronaut-test by micronaut-projects.

the class AbstractMicronautExtension method beforeClass.

/**
 * Executed before tests within a class are run.
 *
 * @param context The test context
 * @param testClass The test class
 * @param testAnnotationValue The test annotation values
 */
protected void beforeClass(C context, Class<?> testClass, @Nullable MicronautTestValue testAnnotationValue) {
    if (testAnnotationValue != null) {
        Class<? extends ApplicationContextBuilder>[] cb = testAnnotationValue.contextBuilder();
        if (ArrayUtils.isNotEmpty(cb)) {
            this.builder = InstantiationUtils.instantiate(cb[0]);
        }
        this.testAnnotationValue = testAnnotationValue;
        final Package aPackage = testClass.getPackage();
        builder.packages(aPackage.getName());
        final List<Property> ps = AnnotationUtils.findRepeatableAnnotations(testClass, Property.class);
        for (Property property : ps) {
            testProperties.put(property.name(), property.value());
        }
        String[] propertySources = testAnnotationValue.propertySources();
        if (ArrayUtils.isNotEmpty(propertySources)) {
            Map<String, PropertySourceLoader> loaderMap = readPropertySourceLoaderMap();
            ResourceResolver resourceResolver = new ResourceResolver();
            for (String propertySource : propertySources) {
                String ext = NameUtils.extension(propertySource);
                if (StringUtils.isNotEmpty(ext)) {
                    String filename = NameUtils.filename(propertySource);
                    PropertySourceLoader loader = loaderMap.get(ext);
                    if (loader != null) {
                        Optional<InputStream> resourceAsStream = resourceResolver.getResourceAsStream(propertySource);
                        InputStream inputStream = resourceAsStream.orElse(testClass.getResourceAsStream(propertySource));
                        if (inputStream != null) {
                            Map<String, Object> properties;
                            try {
                                properties = loader.read(filename, inputStream);
                                builder.propertySources(PropertySource.of(filename, properties));
                            } catch (IOException e) {
                                throw new RuntimeException("Error loading property source reference for @MicronautTest: " + filename);
                            } finally {
                                try {
                                    inputStream.close();
                                } catch (IOException e) {
                                // ignore
                                }
                            }
                        }
                    }
                }
            }
        }
        if (TestPropertyProvider.class.isAssignableFrom(testClass)) {
            resolveTestProperties(context, testAnnotationValue, testProperties);
        }
        testProperties.put(TestActiveCondition.ACTIVE_SPEC_CLAZZ, testClass);
        testProperties.put(TEST_ROLLBACK, String.valueOf(testAnnotationValue.rollback()));
        testProperties.put(TEST_TRANSACTIONAL, String.valueOf(testAnnotationValue.transactional()));
        testProperties.put(TEST_TRANSACTION_MODE, String.valueOf(testAnnotationValue.transactionMode()));
        final Class<?> application = testAnnotationValue.application();
        if (application != void.class) {
            builder.mainClass(application);
        }
        String[] environments = testAnnotationValue.environments();
        if (environments.length == 0) {
            environments = new String[] { "test" };
        }
        builder.packages(testAnnotationValue.packages()).environments(environments);
        PropertySource testPropertySource = PropertySource.of(TEST_PROPERTY_SOURCE, testProperties);
        builder.propertySources(testPropertySource);
        postProcessBuilder(builder);
        this.applicationContext = builder.build();
        startApplicationContext();
        specDefinition = applicationContext.findBeanDefinition(testClass).orElse(null);
        if (testAnnotationValue.startApplication() && applicationContext.containsBean(EmbeddedApplication.class)) {
            embeddedApplication = applicationContext.getBean(EmbeddedApplication.class);
            embeddedApplication.start();
        }
        refreshScope = applicationContext.findBean(RefreshScope.class).orElse(null);
    }
}
Also used : InputStream(java.io.InputStream) PropertySourceLoader(io.micronaut.context.env.PropertySourceLoader) IOException(java.io.IOException) PropertySource(io.micronaut.context.env.PropertySource) ResourceResolver(io.micronaut.core.io.ResourceResolver) Property(io.micronaut.context.annotation.Property) ApplicationContextBuilder(io.micronaut.context.ApplicationContextBuilder) EmbeddedApplication(io.micronaut.runtime.EmbeddedApplication)

Example 3 with ResourceResolver

use of io.micronaut.core.io.ResourceResolver in project micronaut-graphql by micronaut-projects.

the class GraphQLFactory method graphQL.

@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver, ToDosDataFetcher toDosDataFetcher, CreateToDoDataFetcher createToDoDataFetcher, CompleteToDoDataFetcher completeToDoDataFetcher, DeleteToDoDataFetcher deleteToDoDataFetcher, AuthorDataFetcher authorDataFetcher) {
    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)).type("ToDo", typeWiring -> typeWiring.dataFetcher("author", authorDataFetcher)).build();
    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : Bean(io.micronaut.context.annotation.Bean) ResourceResolver(io.micronaut.core.io.ResourceResolver) GraphQL(graphql.GraphQL) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Factory(io.micronaut.context.annotation.Factory) GraphQLSchema(graphql.schema.GraphQLSchema) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Singleton(jakarta.inject.Singleton) SchemaGenerator(graphql.schema.idl.SchemaGenerator) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) InputStreamReader(java.io.InputStreamReader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) BufferedReader(java.io.BufferedReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Example 4 with ResourceResolver

use of io.micronaut.core.io.ResourceResolver in project micronaut-graphql by micronaut-projects.

the class GraphQLFactory method graphQL.

@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver, CurrentUserDataFetcher currentUserDataFetcher, LoginDataFetcher loginDataFetcher) {
    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("currentUser", currentUserDataFetcher)).type("Mutation", typeWiring -> typeWiring.dataFetcher("login", loginDataFetcher)).build();
    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : Bean(io.micronaut.context.annotation.Bean) ResourceResolver(io.micronaut.core.io.ResourceResolver) GraphQL(graphql.GraphQL) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Factory(io.micronaut.context.annotation.Factory) GraphQLSchema(graphql.schema.GraphQLSchema) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Singleton(jakarta.inject.Singleton) SchemaGenerator(graphql.schema.idl.SchemaGenerator) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) InputStreamReader(java.io.InputStreamReader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) BufferedReader(java.io.BufferedReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Example 5 with ResourceResolver

use of io.micronaut.core.io.ResourceResolver in project micronaut-graphql by micronaut-projects.

the class GraphQLFactory method graphQL.

@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();
}
Also used : ResourceResolver(io.micronaut.core.io.ResourceResolver) GraphQL(graphql.GraphQL) ExtendedScalars(graphql.scalars.ExtendedScalars) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Singleton(jakarta.inject.Singleton) InputStreamReader(java.io.InputStreamReader) Bean(io.micronaut.context.annotation.Bean) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Factory(io.micronaut.context.annotation.Factory) GraphQLSchema(graphql.schema.GraphQLSchema) SchemaGenerator(graphql.schema.idl.SchemaGenerator) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) BufferedReader(java.io.BufferedReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Aggregations

ResourceResolver (io.micronaut.core.io.ResourceResolver)5 GraphQL (graphql.GraphQL)4 GraphQLSchema (graphql.schema.GraphQLSchema)4 RuntimeWiring (graphql.schema.idl.RuntimeWiring)4 SchemaGenerator (graphql.schema.idl.SchemaGenerator)4 SchemaParser (graphql.schema.idl.SchemaParser)4 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)4 Bean (io.micronaut.context.annotation.Bean)4 Factory (io.micronaut.context.annotation.Factory)4 Singleton (jakarta.inject.Singleton)4 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 ExtendedScalars (graphql.scalars.ExtendedScalars)1 ApplicationContextBuilder (io.micronaut.context.ApplicationContextBuilder)1 Property (io.micronaut.context.annotation.Property)1 PropertySource (io.micronaut.context.env.PropertySource)1 PropertySourceLoader (io.micronaut.context.env.PropertySourceLoader)1 EmbeddedApplication (io.micronaut.runtime.EmbeddedApplication)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1