Starter for building GraphQL applications with Spring GraphQL
—
Spring Boot GraphQL Starter provides extensive configuration options through Spring Boot's configuration properties mechanism. All properties use the spring.graphql prefix and support environment-specific overrides.
@ConfigurationProperties("spring.graphql")
public class GraphQlProperties {
private final Http http = new Http();
private final Graphiql graphiql = new Graphiql();
private final Rsocket rsocket = new Rsocket();
private final Schema schema = new Schema();
private final Websocket websocket = new Websocket();
// Getters for all nested properties
public Http getHttp() { return this.http; }
public Graphiql getGraphiql() { return this.graphiql; }
public Schema getSchema() { return this.schema; }
public Websocket getWebsocket() { return this.websocket; }
public Rsocket getRsocket() { return this.rsocket; }
}Configure HTTP endpoint settings and transport-specific options.
public static class Http {
private String path = "/graphql";
private final Sse sse = new Sse();
public String getPath() { return this.path; }
public void setPath(String path) { this.path = path; }
public Sse getSse() { return this.sse; }
}# HTTP endpoint path (default: /graphql)
spring.graphql.http.path=/api/graphql
# Server-Sent Events configuration
spring.graphql.http.sse.keep-alive=30s
spring.graphql.http.sse.timeout=60spublic static class Sse {
private Duration keepAlive;
private Duration timeout;
public Duration getKeepAlive() { return this.keepAlive; }
public void setKeepAlive(Duration keepAlive) { this.keepAlive = keepAlive; }
public Duration getTimeout() { return this.timeout; }
public void setTimeout(Duration timeout) { this.timeout = timeout; }
}Control GraphQL schema loading, validation, and development features.
public static class Schema {
private String[] locations = {"classpath:graphql/**/"};
private String[] fileExtensions = {".graphqls", ".gqls"};
private Resource[] additionalFiles = {};
private final Inspection inspection = new Inspection();
private final Introspection introspection = new Introspection();
private final Printer printer = new Printer();
// Getters and setters
public String[] getLocations() { return this.locations; }
public void setLocations(String[] locations) { this.locations = appendSlashIfNecessary(locations); }
public String[] getFileExtensions() { return this.fileExtensions; }
public void setFileExtensions(String[] fileExtensions) { this.fileExtensions = fileExtensions; }
public Resource[] getAdditionalFiles() { return this.additionalFiles; }
public void setAdditionalFiles(Resource[] additionalFiles) { this.additionalFiles = additionalFiles; }
}# Schema file locations (default: classpath:graphql/**/)
spring.graphql.schema.locations=classpath:schemas/,classpath:types/
# Supported file extensions (default: .graphqls, .gqls)
spring.graphql.schema.file-extensions=.graphqls,.gqls,.graphql
# Additional schema files
spring.graphql.schema.additional-files=classpath:extra-schema.graphqlspublic static class Inspection {
private boolean enabled = true;
public boolean isEnabled() { return this.enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
}# Enable schema-to-application mapping validation (default: true)
spring.graphql.schema.inspection.enabled=truepublic static class Introspection {
private boolean enabled = true;
public boolean isEnabled() { return this.enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
}# Enable field introspection at schema level (default: true)
# Disable in production for security
spring.graphql.schema.introspection.enabled=falsepublic static class Printer {
private boolean enabled = false;
public boolean isEnabled() { return this.enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
}# Enable schema printer endpoint at /graphql/schema (default: false)
spring.graphql.schema.printer.enabled=trueConfigure the built-in GraphiQL development interface.
public static class Graphiql {
private String path = "/graphiql";
private boolean enabled = false;
public String getPath() { return this.path; }
public void setPath(String path) { this.path = path; }
public boolean isEnabled() { return this.enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
}# Enable GraphiQL UI (default: false)
spring.graphql.graphiql.enabled=true
# GraphiQL endpoint path (default: /graphiql)
spring.graphql.graphiql.path=/graphql-uiConfigure WebSocket transport for GraphQL subscriptions.
public static class Websocket {
private String path;
private Duration connectionInitTimeout = Duration.ofSeconds(60);
private Duration keepAlive;
public String getPath() { return this.path; }
public void setPath(String path) { this.path = path; }
public Duration getConnectionInitTimeout() { return this.connectionInitTimeout; }
public void setConnectionInitTimeout(Duration timeout) { this.connectionInitTimeout = timeout; }
public Duration getKeepAlive() { return this.keepAlive; }
public void setKeepAlive(Duration keepAlive) { this.keepAlive = keepAlive; }
}# Enable WebSocket support by setting path
spring.graphql.websocket.path=/graphql-ws
# Connection initialization timeout (default: 60s)
spring.graphql.websocket.connection-init-timeout=30s
# Keep-alive interval for WebSocket connections
spring.graphql.websocket.keep-alive=10sConfigure RSocket transport for GraphQL over RSocket.
public static class Rsocket {
private String mapping;
public String getMapping() { return this.mapping; }
public void setMapping(String mapping) { this.mapping = mapping; }
}# RSocket message handler mapping
spring.graphql.rsocket.mapping=graphqlConfigure Cross-Origin Resource Sharing for GraphQL endpoints.
@ConfigurationProperties("spring.graphql.cors")
public class GraphQlCorsProperties {
private List<String> allowedOrigins;
private List<String> allowedOriginPatterns;
private List<String> allowedHeaders;
private List<String> allowedMethods;
private List<String> exposedHeaders;
private Boolean allowCredentials;
private Duration maxAge;
// Standard CORS configuration methods
public CorsConfiguration toCorsConfiguration() {
// Converts properties to Spring's CorsConfiguration
}
}# CORS configuration for GraphQL endpoints
spring.graphql.cors.allowed-origins=http://localhost:3000,https://example.com
spring.graphql.cors.allowed-methods=GET,POST
spring.graphql.cors.allowed-headers=*
spring.graphql.cors.allow-credentials=true
spring.graphql.cors.max-age=3600// Deprecated in Spring Boot 3.5.0
@DeprecatedConfigurationProperty(replacement = "spring.graphql.http.path", since = "3.5.0")
@Deprecated(since = "3.5.0", forRemoval = true)
public String getPath() {
return getHttp().getPath();
}@Deprecated(since = "3.5.1", forRemoval = true)
public static final class DeprecatedSse {
@DeprecatedConfigurationProperty(replacement = "spring.graphql.http.sse.timeout", since = "3.5.0")
@Deprecated(since = "3.5.0", forRemoval = true)
public Duration getTimeout() {
return this.sse.getTimeout();
}
}# application-dev.yml
spring:
graphql:
http:
path: /graphql
graphiql:
enabled: true
path: /graphiql
schema:
inspection:
enabled: true
introspection:
enabled: true
printer:
enabled: true
websocket:
path: /graphql-ws
keep-alive: 30s# application-prod.yml
spring:
graphql:
http:
path: /api/graphql
sse:
timeout: 30s
keep-alive: 15s
graphiql:
enabled: false
schema:
inspection:
enabled: false
introspection:
enabled: false
printer:
enabled: false
cors:
allowed-origins:
- https://app.example.com
- https://admin.example.com
allowed-methods: POST
allow-credentials: true
max-age: 3600The starter validates configuration properties at startup:
Invalid configurations result in clear error messages during application startup.
# Expose GraphQL endpoint info via actuator
management.endpoints.web.exposure.include=health,info,graphql
management.endpoint.graphql.enabled=trueThe starter provides configuration metadata for IDE autocompletion and validation:
{
"properties": [
{
"name": "spring.graphql.http.path",
"type": "java.lang.String",
"description": "Path at which to expose a GraphQL request HTTP endpoint.",
"defaultValue": "/graphql"
}
]
}This enables rich IDE support with autocompletion, validation, and documentation tooltips.
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter-graphql