{"record":{"id":"37b6af6cc24faef7","repo":"instructure/canvas-lms","slug":"targetcontexttype-required-if-targetcontextid-provided","errorCode":null,"errorMessage":"targetContextType required if targetContextId provided","messagePattern":"targetContextType required if targetContextId provided","errorType":"exception","errorClass":"GraphQL::ExecutionError","httpStatus":null,"severity":"error","filePath":"app/graphql/mutations/import_outcomes.rb","lineNumber":324,"sourceCode":"  private\n\n  def get_target(input)\n    if input[:target_group_id]\n      target_group = LearningOutcomeGroup.active.find_by(id: input[:target_group_id])\n      if target_group.nil?\n        raise GraphQL::ExecutionError, I18n.t(\"no such target group\")\n      end\n\n      target_context = target_group.context\n\n      [target_context, target_group]\n    else\n      if input[:target_context_type].blank? && input[:target_context_id].blank?\n        raise GraphQL::ExecutionError, I18n.t(\n          \"You must provide targetGroupId or targetContextId and targetContextType\"\n        )\n      elsif input[:target_context_type].blank? && input[:target_context_id].present?\n        raise GraphQL::ExecutionError, I18n.t(\n          \"targetContextType required if targetContextId provided\"\n        )\n      elsif input[:target_context_type].present? && input[:target_context_id].blank?\n        raise GraphQL::ExecutionError, I18n.t(\n          \"targetContextId required if targetContextType provided\"\n        )\n      end\n\n      target_context =\n        begin\n          context_class(input[:target_context_type]).find_by(\n            id: input[:target_context_id]\n          )\n        rescue NameError\n          raise GraphQL::ExecutionError, I18n.t(\"Invalid targetContextType\")\n        end\n\n      if target_context.nil?","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/app/graphql/mutations/import_outcomes.rb#L306-L342","documentation":"The ImportOutcomes GraphQL mutation's get_target validates that target context inputs are coherent. When targetContextId is supplied but targetContextType is omitted, it raises this ExecutionError because the type is needed to resolve the polymorphic context class (Course or Account).","triggerScenarios":"Calling the importOutcomes mutation with input { targetContextId: 123 } but no targetContextType, while other required inputs (targetGroupId or the pair) are being validated in get_target.","commonSituations":"Frontend forms that collect a numeric target context but whose type dropdown defaults to empty; clients built against older schema versions before targetContextType existed; scripts copying only the ID from a URL.","solutions":["Add targetContextType ('Course' or 'Account') to the mutation input alongside targetContextId","If no target context is intended, omit both targetContextId and targetContextType and pass targetGroupId instead","Fix the client form so the context type is always populated when an ID is chosen"],"exampleFix":"// before\ninput: { targetContextId: 42 }\n// after\ninput: { targetContextId: 42, targetContextType: \"Course\" }","handlingStrategy":"validation","validationCode":"if (input.targetContextId && !input.targetContextType) {\n  throw new Error('targetContextType is required when targetContextId is provided');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await client.mutate({ mutation: IMPORT_OUTCOMES, variables: { input } });\n} catch (e) {\n  if (e.message.includes('targetContextType required')) {\n    // prompt user to select a context type\n  }\n}","preventionTips":["Always send targetContextType and targetContextId as a pair","Use targetGroupId alone when no target context is needed","Bind the type field to the same form state as the ID picker"],"tags":["graphql","validation","outcomes","missing-field"],"backgroundTag":"missing-required-argument","analyzedSha":"1c9f0bb8013ed69c4f2efe11fd483025469b7e6c","analyzedAt":"2026-09-15T20:33:18.891Z","contentChangedAt":"2026-09-15T20:33:18.891Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}