instructure/canvas-lms · error · GraphQL::ExecutionError

targetContextType required if targetContextId provided

Error message

targetContextType required if targetContextId provided

What it means

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).

Solutions

  1. Add targetContextType ('Course' or 'Account') to the mutation input alongside targetContextId
  2. If no target context is intended, omit both targetContextId and targetContextType and pass targetGroupId instead
  3. Fix the client form so the context type is always populated when an ID is chosen

Example fix

// before
input: { targetContextId: 42 }
// after
input: { targetContextId: 42, targetContextType: "Course" }
Defensive patterns

Strategy: validation

Validate before calling

if (input.targetContextId && !input.targetContextType) {
  throw new Error('targetContextType is required when targetContextId is provided');
}

Try / catch

try {
  await client.mutate({ mutation: IMPORT_OUTCOMES, variables: { input } });
} catch (e) {
  if (e.message.includes('targetContextType required')) {
    // prompt user to select a context type
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/37b6af6cc24faef7. Report an issue: GitHub.

Appendix: source

Thrown at app/graphql/mutations/import_outcomes.rb:324

  private

  def get_target(input)
    if input[:target_group_id]
      target_group = LearningOutcomeGroup.active.find_by(id: input[:target_group_id])
      if target_group.nil?
        raise GraphQL::ExecutionError, I18n.t("no such target group")
      end

      target_context = target_group.context

      [target_context, target_group]
    else
      if input[:target_context_type].blank? && input[:target_context_id].blank?
        raise GraphQL::ExecutionError, I18n.t(
          "You must provide targetGroupId or targetContextId and targetContextType"
        )
      elsif input[:target_context_type].blank? && input[:target_context_id].present?
        raise GraphQL::ExecutionError, I18n.t(
          "targetContextType required if targetContextId provided"
        )
      elsif input[:target_context_type].present? && input[:target_context_id].blank?
        raise GraphQL::ExecutionError, I18n.t(
          "targetContextId required if targetContextType provided"
        )
      end

      target_context =
        begin
          context_class(input[:target_context_type]).find_by(
            id: input[:target_context_id]
          )
        rescue NameError
          raise GraphQL::ExecutionError, I18n.t("Invalid targetContextType")
        end

      if target_context.nil?

View on GitHub (pinned to 1c9f0bb801)