Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions documentation/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {
}

dependencies {
implementation 'com.graphql-java:graphql-java:24.3'
implementation 'com.graphql-java:graphql-java:25.0'
}
```
</TabItem>
Expand All @@ -38,7 +38,7 @@ repositories {
}

dependencies {
implementation("com.graphql-java:graphql-java:24.3")
implementation("com.graphql-java:graphql-java:25.0")
}
```
</TabItem>
Expand All @@ -51,7 +51,7 @@ Dependency:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>24.3</version>
<version>25.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const config = {
routeBasePath: 'documentation',
sidebarPath: require.resolve('./sidebars.js'),
editUrl: 'https://github.com/graphql-java/graphql-java-page/edit/master/',
lastVersion: "v24",
lastVersion: "v25",
versions: {
current: {
label: "master",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,67 @@ DataFetcher<?> dataFetcherThatCallsTheDataLoader = new DataFetcher<Object>() {
}
};
```

## Chained DataLoaders

The automatic dispatching of Chained DataLoaders is a new feature included in GraphQL Java 25.0 onwards. Before this version, DataLoaders in chains needed to be manually dispatched.

A Chained DataLoader is where one DataLoader depends on another, within the same DataFetcher.

For example in code:
```java
DataFetcher<CompletableFuture<Object>> df1 = env -> {
return env.getDataLoader("name").load("Key1").thenCompose(result -> {
return env.getDataLoader("email").load(result);
});
};
```

### How do I enable Chained DataLoaders?
You must opt-in to Chained DataLoaders via `GraphQLUnusualConfiguration.DataloaderConfig`, as this may change order of dispatching.

Set `enableDataLoaderChaining(true)` to enable Chained DataLoaders.

For example, to set `enableDataLoaderChaining`:
```java
GraphQL graphQL = GraphQL.unusualConfiguration(graphqlContext)
.dataloaderConfig()
.enableDataLoaderChaining(true);
```

### What changed in GraphQL Java 25.0?
The DataFetcher in the example above, before version 25.0 would have caused execution to hang, because the second DataLoader ("email") was never dispatched.

Prior to version 25.0, users of GraphQL Java needed to manually dispatch DataLoaders to ensure execution completed. From version 25.0, the GraphQL Java engine will automatically dispatch Chained DataLoaders.

If you're looking for more examples, and the technical details, please see [our tests](https://github.com/graphql-java/graphql-java/blob/master/src/test/groovy/graphql/ChainedDataLoaderTest.groovy).

Note: The GraphQL Java engine can only optimally calculate DataLoader dispatches on a per-level basis. It does not calculate optimal DataLoader dispatching across different levels of an operation's field tree.

### A special case: Delayed DataLoaders

In a previous code snippet, we demonstrated one DataLoader depending on another DataLoader.

Another special case is a "delayed" DataLoader, where a DataLoader depends on a slow async task instead. For example, here are two DataFetchers from [a test example](https://github.com/graphql-java/graphql-java/blob/master/src/test/groovy/graphql/ChainedDataLoaderTest.groovy):

```groovy
def fooDF = { env ->
return supplyAsync {
Thread.sleep(1000)
return "fooFirstValue"
}.thenCompose {
return env.getDataLoader("dl").load(it)
}
} as DataFetcher

def barDF = { env ->
return supplyAsync {
Thread.sleep(1000)
return "barFirstValue"
}.thenCompose {
return env.getDataLoader("dl").load(it)
}
} as DataFetcher
```

By opting in to Chained DataLoaders, GraphQL Java will also calculate when to dispatch "delayed" DataLoaders. These "delayed" DataLoaders will be enqueued for dispatch after the async task completes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {
}

dependencies {
implementation 'com.graphql-java:graphql-java:24.3'
implementation 'com.graphql-java:graphql-java:25.0'
}
```
</TabItem>
Expand All @@ -38,7 +38,7 @@ repositories {
}

dependencies {
implementation("com.graphql-java:graphql-java:24.3")
implementation("com.graphql-java:graphql-java:25.0")
}
```
</TabItem>
Expand All @@ -51,7 +51,7 @@ Dependency:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>24.3</version>
<version>25.0</version>
</dependency>
```

Expand Down
116 changes: 116 additions & 0 deletions versioned_docs/version-v25/profiler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
title: "Profiling requests"
date: 2025-08-05T12:52:46+10:00
---

# Profiling GraphQL requests

We've introduced a new profiling feature to help you understand the performance of your GraphQL executions. It provides detailed insights into DataFetcher calls, Dataloader usage, and execution timing. This guide will show you how to use it and interpret the results.

The Profiler is available in all versions after v25.0.beta-5. It will also be included in the forthcoming official v25.0 release.

## Enabling the Profiler

To enable profiling for a GraphQL execution, you need to set a flag on your `ExecutionInput`. It's as simple as calling `.profileExecution(true)` when building it.

```java
import graphql.ExecutionInput;
import graphql.GraphQL;

// ...
GraphQL graphql = GraphQL.newGraphQL(schema).build();

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query("query Hello { world }")
.profileExecution(true) // Enable profiling
.build();

graphql.execute(executionInput);
```

## Accessing the Profiler Results

The profiling results are stored in the `GraphQLContext` associated with your `ExecutionInput`. After the execution is complete, you can retrieve the `ProfilerResult` object from the context.

The result object is stored under the key `ProfilerResult.PROFILER_CONTEXT_KEY`.

```java
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.ProfilerResult;

// ...
ExecutionInput executionInput = /* ... see above ... */
ExecutionResult executionResult = graphql.execute(executionInput);

ProfilerResult profilerResult = executionInput.getGraphQLContext().get(ProfilerResult.PROFILER_CONTEXT_KEY);

if (profilerResult != null) {
Map<String, Object> summary = profilerResult.shortSummaryMap();
System.out.println(summary); // or log as you prefer
}
```

## Understanding the Profiler Results

A great way to get a quick overview about `ProfilerResult` is by using the `shortSummaryMap()` method. It returns a map with key metrics about the execution, which you can use for logging. Let's break down what each key means.

### The ProfilerResult Short Summary Map

Here's a detailed explanation of the fields you'll find in the map returned by `shortSummaryMap()`:

| Key | Type | Description |
| ------------------------------------- | ----------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `executionId` | `String` | The unique ID for this GraphQL execution. |
| `operationName` | `String` | The name of the operation, if one was provided in the query (e.g., `query MyQuery { ... }`). |
| `operationType` | `String` | The type of operation, such as `QUERY`, `MUTATION`, or `SUBSCRIPTION`. |
| `startTimeNs` | `long` | The system time in nanoseconds when the execution started. |
| `endTimeNs` | `long` | The system time in nanoseconds when the execution finished. |
| `totalRunTimeNs` | `long` | The total wall-clock time of the execution (`endTimeNs - startTimeNs`). This includes time spent waiting for asynchronous operations like database calls or external API requests within your DataFetchers. |
| `engineTotalRunningTimeNs` | `long` | The total time the GraphQL engine spent actively running on a thread. This is like the "CPU time" of the execution and excludes time spent waiting for `CompletableFuture`s to complete. Comparing this with `totalRunTimeNs` can give you a good idea of how much time is spent on I/O. |
| `totalDataFetcherInvocations` | `int` | The total number of times any DataFetcher was invoked. |
| `totalCustomDataFetcherInvocations` | `int` | The number of invocations for DataFetchers you've written yourself (i.e., not the built-in `PropertyDataFetcher`). |
| `totalTrivialDataFetcherInvocations` | `int` | The number of invocations for the built-in `PropertyDataFetcher`. |
| `totalWrappedTrivialDataFetcherInvocations` | `int` | The number of invocations for DataFetchers that wrap a `PropertyDataFetcher`. |
| `fieldsFetchedCount` | `int` | The number of unique fields fetched during the execution. |
| `dataLoaderChainingEnabled` | `boolean` | `true` if the experimental Dataloader chaining feature was enabled for this execution. |
| `dataLoaderLoadInvocations` | `Map` | A map where keys are Dataloader names and values are the number of times `load()` was called on them. Note that this counts all `load()` calls, including those that hit the Dataloader cache. |
| `oldStrategyDispatchingAll` | `Set` | If Chained DataLoaders are not used, the older dispatching strategy is used instead. This key lists the levels where DataLoaders were dispatched. |
| `dispatchEvents` | `List<Map>` | A list of events, one for each time a Dataloader was dispatched. See below for details. |
| `instrumentationClasses` | `List<String>` | A list of the class names of the `Instrumentation`s used during this execution. |
| `dataFetcherResultTypes` | `Map` | A summary of the types of values returned by your custom DataFetchers. See below for details. |

#### `dispatchEvents`

This is a list of maps, each detailing a `DataLoader` dispatch event.

| Key | Type | Description |
| -------------- | -------- | -------------------------------------------------------------------- |
| `type` | `String` | The type of dispatch. Can be `LEVEL_STRATEGY_DISPATCH`, `CHAINED_STRATEGY_DISPATCH`, `DELAYED_DISPATCH`, `CHAINED_DELAYED_DISPATCH`, or `MANUAL_DISPATCH`. |
| `dataLoader` | `String` | The name of the `DataLoader` that was dispatched. |
| `level` | `int` | The execution strategy level at which the dispatch occurred. |
| `keyCount` | `int` | The number of keys that were dispatched in this batch. |

#### `dataFetcherResultTypes`

This map gives you more information into the type of your DataFetchers' return values.

The keys are `COMPLETABLE_FUTURE_COMPLETED`, `COMPLETABLE_FUTURE_NOT_COMPLETED`, and `MATERIALIZED`.
Each key maps to another map with two keys:
* `count`: The number of unique fields with DataFetchers that returned this result type.
* `invocations`: The total number of invocations across all fields that returned this result type.

Here's what the result types mean:

| Result Type | Meaning |
| ---------------------------------- | --------------------------------------------------------------------------------------------- |
| `COMPLETABLE_FUTURE_COMPLETED` | The DataFetcher returned a `CompletableFuture` that was already completed when it was returned. |
| `COMPLETABLE_FUTURE_NOT_COMPLETED` | The DataFetcher returned an incomplete `CompletableFuture`. |
| `MATERIALIZED` | The DataFetcher returned a simple value (i.e., not a `CompletableFuture`). |

### A note on engine timing statistics logged from an `Instrumentation`

If you're logging the `ProfilerResult` from an `Instrumentation`, note that engine timing statistics such as `startTimeNs`, `endTimeNs`, `totalRunTimeNs`, `engineTotalRunningTimeNs` will be set to `0`. This is because the timing is set after all `Instrumentation`s have run, so it is not available from within an `Instrumentation`.

Apart from engine timing information, all other `ProfilerResult` information is still valid if accessed from within an `Instrumentation`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version-v24/tutorialSidebar": [
"version-v25/tutorialSidebar": [
{
"type": "autogenerated",
"dirName": "."
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
"v24"
"v25"
]