marmelab/react-admin · error
Unable to determine the query operation
Error message
Unable to determine the query operation
What it means
getQueryOperation extracts operation ('query' or 'mutation') from a parsed GraphQL document's first definition. If the document is null/undefined, has no definitions array, or is empty, the operation cannot be determined and the helper throws.
Source
Thrown at packages/ra-data-graphql/src/index.ts:273
};
const handleError = (error: ApolloError) => {
if (error?.networkError as ServerError) {
throw new HttpError(
(error?.networkError as ServerError)?.message,
(error?.networkError as ServerError)?.statusCode
);
}
throw new HttpError(error.message, 200, error);
};
const getQueryOperation = query => {
if (query && query.definitions && query.definitions.length > 0) {
return query.definitions[0].operation;
}
throw new Error('Unable to determine the query operation');
};
export type GetIntrospection = () => Promise<IntrospectionResult>;
export type GraphqlDataProvider = DataProvider & {
getIntrospection: GetIntrospection;
client: ApolloClient<unknown>;
};
export default buildGraphQLProvider;
View on GitHub (pinned to 051f511bb0)
Solutions
- Ensure the query passed is a valid non-empty gql document: check it with console.log(query).
- Fix conditional template strings so they always produce at least one definition.
- Verify your custom buildQuery/buildGqlQuery returns a query for every fetch type.
- Validate the document parses: gql`query { ... }` instead of an empty string.
Example fix
// before
const query = condition ? gql`query { ... }` : '';
// after
const query = condition ? gql`query { ... }` : gql`query { noop }`; Defensive patterns
Strategy: type-guard
Validate before calling
const isValidDoc = (q: unknown): boolean =>
!!q && typeof q === 'object' &&
Array.isArray((q as any).definitions) &&
(q as any).definitions.length > 0;
if (!isValidDoc(query)) throw new Error('Query must be a non-empty DocumentNode'); Type guard
const isDocumentNode = (q: unknown): q is DocumentNode => typeof q === 'object' && q !== null && 'definitions' in q && Array.isArray((q as DocumentNode).definitions) && (q as DocumentNode).definitions.length > 0;
Try / catch
try {
return await dataProvider.getList(resource, params);
} catch (e) {
if (e.message === 'Unable to determine the query operation') {
console.error('Invalid/empty GraphQL document passed to provider', e);
}
throw e;
} Prevention
- Always build queries with gql`` template literals, never raw strings or conditionals yielding empty strings.
- Unit-test custom buildQuery implementations for every fetch type.
- Assert documents are non-empty DocumentNodes in dev builds.
When it happens
Trigger: Passing a null/empty/invalid parsed query (DocumentNode) to the provider machinery — e.g. a gql`` template producing an empty document, or a variable holding undefined instead of a parsed document — flowing into operation()/getQueryOperation.
Common situations: Typo in gql tag usage, conditional query building that yields an empty string, a custom buildQuery returning undefined query for a fetch type, or corrupted cached documents.
Related errors
- Empty sparse fields. Specify at least one field or remove th
- Requested sparse fields not found. Ensure sparse fields are
- Unknown resource ${resourceName}. Make sure it has been decl
- No query or mutation matching fetch type ${raFetchType} coul
- ${(error?.networkError as ServerError)?.message}
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/6a3056d8ab3a667e.
Report an issue: GitHub.