{"record":{"id":"7b932307316f8355","repo":"facebook/relay","slug":"graphql-operations-and-fragments-must-contain-name","errorCode":null,"errorMessage":"GraphQL operations and fragments must contain names","messagePattern":"GraphQL operations and fragments must contain names","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/babel-plugin-relay/compileGraphQLTag.js","lineNumber":119,"sourceCode":"  graphqlDefinition: OperationDefinitionNode | FragmentDefinitionNode,\n  options: {\n    // If an output directory is specified when running relay-compiler this should point to that directory\n    artifactDirectory: ?string,\n    // Generate eager es modules instead of lazy require\n    eagerEsModules: boolean,\n    // The command to run to compile Relay files, used for error messages.\n    buildCommand: string,\n    // Generate extra validation, defaults to true.\n    isDevelopment: boolean,\n    // Wrap the validation code in a conditional checking this variable.\n    isDevVariable: ?string,\n    // Use haste style global requires, defaults to false.\n    isHasteMode: boolean,\n  },\n): Object {\n  const definitionName = graphqlDefinition.name && graphqlDefinition.name.value;\n  if (!definitionName) {\n    throw new Error('GraphQL operations and fragments must contain names');\n  }\n  const requiredFile = definitionName + '.graphql';\n  const requiredPath = posixifyPath(\n    options.isHasteMode\n      ? requiredFile\n      : options.artifactDirectory\n        ? getRelativeImportPath(state, options.artifactDirectory, requiredFile)\n        : GENERATED + requiredFile,\n  );\n\n  const hash = crypto\n    .createHash('md5')\n    .update(print(graphqlDefinition), 'utf8')\n    .digest('hex');\n\n  let topScope = path.scope;\n  while (topScope.parent) {\n    topScope = topScope.parent;","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/facebook/relay/blob/668b1b85e06261aa3b58dabfc51f8b5524a70955/packages/babel-plugin-relay/compileGraphQLTag.js#L101-L137","documentation":"Relay requires every operation and fragment to have an explicit name so it can generate named artifacts and derive the module require path. This branch (getRequiredModulePath-style logic) computes a filename like `${definitionName}.graphql` for haste-mode or artifact resolution and throws if the definition's name is absent. Anonymous operations/fragments cannot be compiled.","triggerScenarios":"A graphql`` tag containing an anonymous operation (e.g. graphql`query { viewer { id } }`) or a fragment missing its name token, so graphqlDefinition.name is null or name.value is empty.","commonSituations":"Writing shorthand anonymous queries copied from a playground; accidentally deleting the fragment name during refactoring; codegen output that emits unnamed operations.","solutions":["Add a unique name: query ViewerQuery { ... } instead of query { ... }","Ensure fragments are declared as fragment Name on Type { ... } with the name present","Run Relay compiler separately to surface naming errors with better messages before Babel runs"],"exampleFix":"// before\nconst data = graphql`query { viewer { id } }`;\n\n// after\nconst data = graphql`query ViewerQuery { viewer { id } }`;","handlingStrategy":"validation","validationCode":"function assertNamed(text) {\n  const { parse } = require('graphql');\n  const def = parse(text).definitions[0];\n  if (!def.name || !def.name.value) {\n    throw new Error('Relay requires every operation/fragment to have a name');\n  }\n}","typeGuard":"const isNamed = (def) =>\n  !!def && !!def.name && typeof def.name.value === 'string' && def.name.value.length > 0;","tryCatchPattern":null,"preventionTips":["Always name queries, mutations, subscriptions, and fragments","Enable Relay compiler in CI to catch anonymous operations before Babel","Reject anonymous operations in code review / lint rules"],"tags":["babel","build-time","graphql","naming","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"}