{"record":{"id":"49cff6e8f949694d","repo":"facebook/relay","slug":"babelpluginrelay-expected-a-fragment-mutation-q","errorCode":null,"errorMessage":"BabelPluginRelay: Expected a fragment, mutation, query, or subscription, got `${definition.kind}`.","messagePattern":"BabelPluginRelay: Expected a fragment, mutation, query, or subscription, got `(.+?)`\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/babel-plugin-relay/compileGraphQLTag.js","lineNumber":61,"sourceCode":"/**\n * Given a graphql`` tagged template literal, replace it with the appropriate\n * runtime artifact.\n */\nfunction compileGraphQLTag(\n  t: $FlowFixMe,\n  path: Object,\n  state: BabelState,\n  ast: DocumentNode,\n): void {\n  if (ast.definitions.length !== 1) {\n    throw new Error(\n      'BabelPluginRelay: Expected exactly one definition per graphql tag.',\n    );\n  }\n  const definition = ast.definitions[0];\n  if (\n    definition.kind !== 'FragmentDefinition' &&\n    definition.kind !== 'OperationDefinition'\n  ) {\n    throw new Error(\n      'BabelPluginRelay: Expected a fragment, mutation, query, or ' +\n        'subscription, got `' +\n        definition.kind +\n        '`.',\n    );\n  }\n\n  const eagerEsModules = state.opts?.eagerEsModules ?? true;\n  const isHasteMode = state.opts?.jsModuleFormat === 'haste';\n  const isDevVariable = state.opts?.isDevVariableName;\n  const artifactDirectory = state.opts?.artifactDirectory;\n  const buildCommand = state.opts?.codegenCommand ?? 'relay-compiler';\n  // Fallback is 'true'\n  const isDevelopment =\n    // $FlowFixMe[cannot-resolve-name]\n    (process.env.BABEL_ENV || process.env.NODE_ENV) !== 'production';","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/facebook/relay/blob/668b1b85e06261aa3b58dabfc51f8b5524a70955/packages/babel-plugin-relay/compileGraphQLTag.js#L43-L79","documentation":"After ensuring exactly one definition, compileGraphQLTag checks that the definition kind is either FragmentDefinition or OperationDefinition. Anything else (e.g. SchemaDefinition, DirectiveDefinition, or a malformed node from an odd parse) is rejected. Relay's Babel plugin only supports fragments, queries, mutations, and subscriptions inside graphql tags.","triggerScenarios":"A graphql`` tag whose single parsed definition is not a fragment or operation — e.g. graphql`schema { query: Query }`, a directive/type-system definition, or text that parses to an unsupported definition kind.","commonSituations":"Pasting SDL/type-system GraphQL (schema definitions, scalar declarations) into a client graphql`` tag; a typo causing the parser to infer an unexpected kind; using the tag in a .graphql schema-like file.","solutions":["Replace the type-system definition with a supported construct: a fragment (fragment X on Type {...}) or an operation (query/mutation/subscription)","Move SDL/schema definitions out of component code into a separate .graphql schema file","Inspect the definition text and confirm the first statement starts with 'fragment', 'query', 'mutation', or 'subscription'"],"exampleFix":"// before\nconst x = graphql`scalar DateTime`;\n\n// after (SDL moved to schema; component uses a fragment)\nconst x = graphql`fragment DateTimeInfo on Post { publishedAt }`;","handlingStrategy":"validation","validationCode":"function assertSupportedKind(text) {\n  const { parse } = require('graphql');\n  const def = parse(text).definitions[0];\n  if (def.kind !== 'FragmentDefinition' && def.kind !== 'OperationDefinition') {\n    throw new Error(`Unsupported definition kind in graphql tag: ${def.kind}`);\n  }\n}","typeGuard":"const isExecutable = (def) =>\n  def && (def.kind === 'FragmentDefinition' || def.kind === 'OperationDefinition');","tryCatchPattern":null,"preventionTips":["Never put SDL/type-system definitions (scalar, schema, directive) in graphql`` tags","Start every tag's body with fragment/query/mutation/subscription","Keep schema definitions in a separate .graphql file consumed by the Relay compiler"],"tags":["babel","build-time","graphql","relay"],"backgroundTag":"invalid-graphql-tag","analyzedSha":"668b1b85e06261aa3b58dabfc51f8b5524a70955","analyzedAt":"2026-09-02T19:57:20.783Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}