0

Im trying to build a graphql schema during runtime without any static schema definitions. I have the following classes

@Component
public class GraphQLProvider {

    private GraphQL graphQL;

    private final ConnectionManager connectionManager;

    public GraphQLProvider(ConnectionManager connectionManager) {this.connectionManager = connectionManager;}

    @PostConstruct
    public void init() {
        GraphQLSchema schema = buildSchema();

        System.out.println( new SchemaPrinter().print( schema ) );

        this.graphQL = GraphQL.newGraphQL( schema ).build();
    }

    public GraphQL getGraphQL() {
        return graphQL;
    }

 private GraphQLSchema buildSchema() {
        GraphQLObjectType queryType = GraphQLObjectType.newObject().name( "Query" )
            .field( field -> field
                .name( "hello" )
                .type( Scalars.GraphQLString )
                .dataFetcher( environment -> "world" ) )
            .build();

        return GraphQLSchema.newSchema().query( queryType ).build();
    }

}
@Configuration
public class GraphQLConfig {

    @Bean
    @Primary
    public GraphQlSource graphQlSource(GraphQLProvider graphQLProvider) {
        GraphQLSchema schema = graphQLProvider.getGraphQL().getGraphQLSchema();
        return GraphQlSource.builder( schema ).build();
    }
}

The schema printed by schemaprinter is expected and its correct.

I experience 404 when I try to query with this setup in postman but if i run with predefined graphql schemas without the dynamic generation i get the result as expected.

The error

2025-02-14 09:51:41,945+0530 DEBUG [org.springframework.security.web.FilterChainProxy] (http-nio-9900-exec-1) [-] [] Secured POST /graphql
2025-02-14 09:51:41,950+0530 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-nio-9900-exec-1) [-] [] POST "/graphql", parameters={}
2025-02-14 09:51:41,953+0530 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (http-nio-9900-exec-1) [-] [] Mapped to ResourceHttpRequestHandler [classpath [public/], classpath [static/]]
2025-02-14 09:51:41,956+0530 DEBUG [org.springframework.web.servlet.resource.ResourceHttpRequestHandler] (http-nio-9900-exec-1) [-] [] Resource not found
2025-02-14 09:51:41,958+0530 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http-nio-9900-exec-1) [-] [] Resolved [org.springframework.web.servlet.resource.NoResourceFoundException: No static resource graphql.]
2025-02-14 09:51:41,959+0530 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-nio-9900-exec-1) [-] [] Completed 404 NOT_FOUND
2025-02-14 09:51:41,973+0530 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-nio-9900-exec-1) [-] [] "ERROR" dispatch for POST "/error", parameters={}
2025-02-14 09:51:41,995+0530 DEBUG [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] (http-nio-9900-exec-1) [-] [] Using 'application/json', given [application/json, text/plain, */*] and supported [application/json, application/*+json, application/x-jackson-smile, application/cbor]
2025-02-14 09:51:41,997+0530 DEBUG [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] (http-nio-9900-exec-1) [-] [] Writing [{timestamp=Fri Feb 14 04:21:41 GMT 2025, status=404, error=Not Found, path=/graphql}]
2025-02-14 09:51:42,010+0530 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-nio-9900-exec-1) [-] [] Exiting from "ERROR" dispatch, status 404

I don't want to any static files in my resources rather to be initialized in runtime.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.