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

  1. Ensure the query passed is a valid non-empty gql document: check it with console.log(query).
  2. Fix conditional template strings so they always produce at least one definition.
  3. Verify your custom buildQuery/buildGqlQuery returns a query for every fetch type.
  4. 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

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


AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30). Data as JSON: /api/errors/6a3056d8ab3a667e. Report an issue: GitHub.