Customize GraphiQL in Spring Boot: Path and Custom Page
The default GraphiQL page is great for a quick start. But sooner or later, you want to change something. Maybe you need a different URL, or a page that does not depend on a CDN, or a header with your company name.
In this post, we will customize GraphiQL in Spring Boot, step by step. We start with simple property changes and then move to a fully custom GraphiQL page that you build and serve yourself.
Table of Contents
- What You Get by Default
- Change the Paths
- Expose the Schema as Text
- Control Introspection
- Subscriptions and WebSocket Path
- Why Build a Custom Page?
- Step 1: Build Your GraphiQL Page
- Step 2: Serve It from Spring Boot
- Branding, Plugins, and Storage
- Other Ways to Customize
- Common Mistakes
- FAQs
What You Get by Default
Spring for GraphQL ships with a stock GraphiQL page. It is a single index.html file that loads its JavaScript and CSS from the esm.sh CDN. Spring Boot applications turn it on with one property:
spring.graphql.graphiql.enabled=true
Here is what the defaults look like:
| Setting | Property | Default |
|---|---|---|
| GraphiQL page | spring.graphql.graphiql.path |
/graphiql |
| GraphQL HTTP endpoint | spring.graphql.http.path |
/graphql |
| Schema as text | spring.graphql.schema.printer.enabled |
Off |
| WebSocket endpoint | spring.graphql.websocket.path |
Off until a path is set |
If you use spring-boot-devtools, the page is exposed automatically during development.
Change the Paths
Move the GraphiQL page
Maybe /graphiql clashes with another route, or you want a less obvious URL. Change it with one property:
spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/tools/graphiql
The page is now at http://localhost:8080/tools/graphiql.
Move the GraphQL endpoint
You can also change where the API itself lives:
spring.graphql.http.path=/api/graphql
The stock GraphiQL page knows where your endpoint is, so it keeps working after you change the path. If you see older tutorials that use spring.graphql.path, know that the property is deprecated in newer Spring Boot versions and replaced by spring.graphql.http.path.
Update your security rules
If you followed the authentication post, remember to change the paths in your security configuration too. Otherwise your rules will protect the old URLs and leave the new ones unguarded or blocked.
.requestMatchers("/api/graphql").authenticated()
Allow more HTTP methods
By default, the GraphQL HTTP transport accepts only POST requests. You can change this with spring.graphql.http.methods. HTTP OPTIONS requests on the endpoint are answered with an Allow header that lists the configured methods, so clients like GraphiQL can discover them.
Expose the Schema as Text
It is handy to see your whole schema in one plain-text page. Turn on the schema printer:
spring.graphql.schema.printer.enabled=true
The schema is then available at the GraphQL path plus /schema. With the default settings, that is:
http://localhost:8080/graphql/schema
You can open it in the browser or fetch it with curl:
curl http://localhost:8080/graphql/schema
This is useful for code generation, for sharing the schema with a front-end team, or for checking what you have deployed. Treat it like any other development tool: protect it or turn it off in production.
Control Introspection
GraphiQL uses introspection to read your schema. Spring Boot allows it by default, because tools like GraphiQL need it. To hide your schema from clients, switch it off:
spring.graphql.schema.introspection.enabled=false
With introspection disabled, GraphiQL cannot build its documentation explorer or autocomplete, so you would normally only do this in production. We will cover that in more detail in the last post of the series.
You can also change where Spring looks for schema files:
spring.graphql.schema.locations=classpath:graphql/
spring.graphql.schema.file-extensions=.graphqls,.gqls
Subscriptions and WebSocket Path
If your API supports subscriptions over WebSocket, the page needs to know the WebSocket address. The WebSocket endpoint is off by default. For a servlet application, add the WebSocket starter and set the path:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
spring.graphql.websocket.path=/graphql-ws
For a WebFlux application, no extra dependency is needed, but you must still set the path property.
Why Build a Custom Page?
The stock page is fine for learning. For a team or a company, you often want more. The Spring documentation says that during development a stock integration is often enough, but in production, applications may need a custom GraphiQL build, such as one with a company logo or specific authentication support.
Here are the common reasons:
- No CDN dependency: some networks block outside CDNs, and you may not want to depend on one.
- Branding: show your product name instead of the default title.
- Login support: fetch a token automatically instead of pasting it.
- Custom plugins: add your own tools to the sidebar.
- Version control: pin a GraphiQL version so the page does not change under you.
- Security review: serve only code that you have audited.
Step 1: Build Your GraphiQL Page
Spring's documentation says the exact build is outside its scope, because there are several options. You can copy the build result into your application resources, or integrate a JavaScript build into your project with Node.js plugins for Gradle or Maven.
Here is a simple approach with a small React project.
Create the project
mkdir graphiql-custom
cd graphiql-custom
npm init -y
npm install graphiql react react-dom graphql @graphiql/toolkit
GraphiQL needs React, React DOM, and graphql as peer dependencies, so you install them yourself. Pick a bundler such as Vite, Webpack, or Parcel to produce the final files. The GraphiQL repository includes starter examples for several of them.
Write the entry file
This follows the setup shown in the GraphiQL README:
import { createTransport } from '@graphiql/toolkit';
import { GraphiQL } from 'graphiql';
import { createRoot } from 'react-dom/client';
import 'graphiql/style.css';
const transport = createTransport({ url: '/graphql' });
const root = createRoot(document.getElementById('root'));
root.render(<GraphiQL transport={transport} />);
Here is what it does:
createTransporttells GraphiQL where to send operations. We use a relative URL,/graphql, so it works on any host.- The
GraphiQLcomponent renders the full interface. - The style import brings in the required CSS.
Note that the transport prop is the recommended way in GraphiQL 6. It replaces the older fetcher prop, which still works for existing integrations. By default, the transport sends an OPTIONS request, reads the server's Allow header to discover supported HTTP methods, and falls back to POST if discovery is unavailable. This works well with Spring Boot, which answers OPTIONS with an Allow header.
The HTML shell
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Java Inspires API Explorer</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body style="margin: 0; height: 100vh;">
<div id="root" style="height: 100vh;"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
Build the project with your bundler, and you will get an index.html plus JavaScript and CSS files in an output folder.
Step 2: Serve It from Spring Boot
You have two options. The simplest one is to treat the build as static content. The other is to expose the page through a handler.
Option A: static resources
Copy the build output into your Spring Boot project:
src/main/resources/static/explorer/
├── index.html
└── assets/
├── main.js
└── main.css
Spring Boot serves files in static/ automatically. Open http://localhost:8080/explorer/index.html. Because the page is now part of your app, it is on the same origin as /graphql, so you do not need CORS.
Make sure your bundler uses a base path that matches the folder, such as /explorer/, or the browser will not find the script and style files.
Option B: expose it with a router function
The Spring documentation shows how to expose a GraphiQL instance with the functional web framework. Once a build is on the classpath, you can serve it from an endpoint you choose:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.server.webmvc.GraphiQlHandler;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
@Configuration
public class GraphiQlConfiguration {
@Bean
@Order(0)
public RouterFunction<ServerResponse> graphiQlRouterFunction() {
RouterFunctions.Builder builder = RouterFunctions.route();
ClassPathResource graphiQlPage =
new ClassPathResource("graphiql/index.html");
GraphiQlHandler graphiQLHandler =
new GraphiQlHandler("/graphql", "", graphiQlPage);
builder = builder.GET("/graphiql", graphiQLHandler::handleRequest);
return builder.build();
}
}
This example mirrors the one in the Spring reference. It loads the page from the classpath, creates a handler that knows the GraphQL path, and maps the handler to a URL. The constructor arguments can differ slightly between Spring for GraphQL versions, so check the GraphiQlHandler API documentation for the version in your project.
You may also need to serve the relevant static resources, such as scripts and styles, for your page.
Turn off the stock page
If you serve your own page at /graphiql, switch off the built-in one so the two do not compete:
spring.graphql.graphiql.enabled=false
Protect your custom page
A custom page does not protect itself. Apply the same security rules as for the rest of your application. For example, allow it only for signed-in developers, or serve it only in a dev profile:
@Configuration
@Profile("dev")
public class GraphiQlConfiguration {
// router function from above
}
Branding, Plugins, and Storage
These options come from the GraphiQL 6 README and apply when you build your own page with the React component.
Add your brand name
In GraphiQL 6, the brand prop replaces the default hexagon icon and "GraphiQL" wordmark in the top bar:
root.render(
<GraphiQL transport={transport} brand="Java Inspires" />
);
Add a footer
<GraphiQL transport={transport}>
<GraphiQL.Footer>
Internal tool. Do not share tokens.
</GraphiQL.Footer>
</GraphiQL>
The GraphiQL.Footer child adds a custom footer below the response editor.
Choose the plugins
GraphiQL includes the History, Query Builder, and Collections plugins by default, and the documentation explorer is the default reference plugin. You can pass your own list to the plugins prop, which replaces the default list. Start from the DEFAULT_PLUGINS export so you do not have to import the built-in plugins directly:
import { DEFAULT_PLUGINS, GraphiQL } from 'graphiql';
const plugins = [...DEFAULT_PLUGINS, myPlugin];
<GraphiQL plugins={plugins} transport={transport} />;
A plugin is a plain JavaScript object with three properties: a unique title, an icon component for the sidebar, and a content component shown next to it.
Theme the interface
GraphiQL uses CSS variables for colors. As of version 6, the supported set is the OKLCH design tokens. Overriding these variables is the only officially supported way to change the look. Class names are not stable and may change between minor or patch versions, so do not depend on them.
The editors are built on Monaco, so editor colors use Monaco themes. The editorTheme prop takes a pair of theme names, one for dark mode and one for light:
<GraphiQL
transport={transport}
editorTheme={{ dark: 'graphiql-DARK', light: 'graphiql-LIGHT' }}
/>
Use a separate storage namespace
GraphiQL saves state, such as history and tabs, in localStorage. If several GraphiQL instances run on the same origin, they can overwrite each other's data. Give each one its own storage namespace by passing a custom storage object that adds a prefix to every key:
const NAMESPACE = 'books-api';
const storage = {
...localStorage,
getItem(key) {
return localStorage.getItem(`${NAMESPACE}:${key}`);
},
setItem(key, value) {
return localStorage.setItem(`${NAMESPACE}:${key}`, value);
},
removeItem(key) {
return localStorage.removeItem(`${NAMESPACE}:${key}`);
},
};
<GraphiQL transport={transport} storage={storage} />;
Default query and headers
Many teams want GraphiQL to open with a sample query or a prefilled header. The GraphiQL props documentation lists the available options for this, and they can change between major versions. Check the API docs for the version you build with, and test the behavior before relying on it.
Other Ways to Customize
Use a different tool altogether
GraphiQL is not the only choice. If your team prefers another client, you can leave the stock page off and use a standalone tool with your endpoint. We compare these tools in the last post of this series.
Serve the page only to some environments
Use profiles to enable the page where you need it:
# application-dev.yml
spring:
graphql:
graphiql:
enabled: true
schema:
printer:
enabled: true
# application-prod.yml
spring:
graphql:
graphiql:
enabled: false
schema:
printer:
enabled: false
Run the app with the profile you want:
java -jar app.jar --spring.profiles.active=prod
Common Mistakes
- Changing the GraphQL path but not the security rules: the new URL ends up unprotected or blocked.
- Wrong base path in the custom build: the page loads blank because scripts and styles return 404.
- Leaving both pages on: a custom page and the stock page can clash. Disable the stock one.
- Relying on CSS class names: they are not stable. Use the documented CSS variables.
- Serving a custom page without authentication: a custom build is not secure by itself.
- Using an old property name: use
spring.graphql.http.pathfor the endpoint path in current versions. - Forgetting the CDN: the stock page needs access to the CDN. If the page is blank on a locked-down network, that is the likely reason.
FAQs
How do I change the GraphiQL URL in Spring Boot?
Set spring.graphql.graphiql.path. For example, spring.graphql.graphiql.path=/tools/graphiql moves the page to /tools/graphiql.
How do I change the GraphQL endpoint path?
Set spring.graphql.http.path. The default is /graphql.
How do I see the schema as text?
Set spring.graphql.schema.printer.enabled=true. The schema is then available at the GraphQL path plus /schema, for example /graphql/schema.
Does GraphiQL in Spring Boot need the internet?
The stock page loads its static resources from a CDN, so the browser needs access to it. To avoid this, build and serve your own GraphiQL page.
How do I use a custom GraphiQL build with Spring Boot?
Build a GraphiQL page with your own bundler, then either place it under src/main/resources/static or serve it with a router function and GraphiQlHandler.
How do I change the title or logo?
In a custom GraphiQL 6 build, use the brand prop. For the HTML title, edit the title tag in your own index.html.
Can I add my own tools to GraphiQL?
Yes. Write a plugin object with a title, an icon, and a content component, then pass it in the plugins prop together with DEFAULT_PLUGINS.
Why is my customized page blank?
Check the browser's network tab. The most common causes are a wrong base path for assets, a blocked CDN, or a security rule that blocks the page's files.
Final Thoughts
Most projects only need a few properties: a different path, the schema printer, and the right introspection setting. When you need more control, a custom GraphiQL page gives you branding, plugins, and full independence from a CDN.
Keep your security rules in sync with every path you change, and keep any GraphiQL page for development unless you have protected it properly.
Comments
Post a Comment