GraphQL DataLoader fails with 400 when batching many IDs

I'm running a GraphQL server using postgraphile and dataloader to batch and load data from a backend microservice. When the number of IDs passed to the loader grows large (~100+), the request fails with a 400 Bad Request error.
Here's the relevant code:
Data loader:
import DataLoader from "dataloader";
import { OptMetricsServiceApi } from "../integrations/OptMetricsService";
export const createDefaultAnalyticsLoader = (
optMetricService: OptMetricsServiceApi
) =>
new DataLoader(async (opportunityIds: Array<string>) => {
const opportunityDefaultAnalytics =
await optMetricService.getDefaultAnalyticForOpportunities({
requestBody: opportunityIds,
});
return opportunityIds.map((id) =>
opportunityDefaultAnalytics.find(
({ opportunityId }) => opportunityId === id
)
);
});
And the resolver that uses it:
const loadReductionData = async (data: any, args: any, context: any) => {
try {
const defaultAnalytic: OpportunityAnalyticModel =
await context.defaultAnalyticsLoader.load(data.runId);
const reductionRawValue = getReductionRawValue(defaultAnalytic);
return {
reductionRawValue,
...defaultAnalytic,
};
} catch (error) {
logger.warn(
`Failed to load defaultAnalytic for ${
data.runId
}. Error: ${error}. Error Info: ${JSON.stringify(error)}`
);
return null;
}
};
On many IDs(~100+) in the request, the request would fail with:
AxiosError: Request failed with status code 400
Versions:
dataloader: ^2.1.0
postgraphile: 4.12.9
Answer
We changed the backend API to accept the IDs in the POST body instead of as URL query params. Once that happened, the DataLoader worked correctly even with a large batch of IDs.
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions