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

Invalid targetContextType

Error message

Invalid targetContextType

What it means

ImportOutcomes#get_target resolves the target context via context_class(input[:target_context_type]) (e.g. Course, Account). If the type string does not correspond to a known class, the lookup raises NameError, which is rescued and re-raised as this ExecutionError.

Solutions

  1. Use the exact class names 'Course' or 'Account' for targetContextType
  2. Validate the type value client-side against the allowed set before calling the mutation
  3. Check for case/whitespace issues in the value being sent

Example fix

// before
input: { targetContextType: "account", targetContextId: 7 }
// after
input: { targetContextType: "Account", targetContextId: 7 }
Defensive patterns

Strategy: validation

Validate before calling

const ALLOWED = ['Course', 'Account'];
if (input.targetContextType && !ALLOWED.includes(input.targetContextType)) {
  throw new Error(`targetContextType must be one of ${ALLOWED.join(', ')}`);
}

Type guard

const isValidContextType = (t) => t === 'Course' || t === 'Account';

Try / catch

try {
  await client.mutate({ mutation: IMPORT_OUTCOMES, variables: { input } });
} catch (e) {
  if (e.message.includes('Invalid targetContextType')) {
    // normalize the type string to 'Course' or 'Account' and retry once
  }
}

Prevention

When it happens

Trigger: Passing targetContextType values other than Course/Account, misspellings ('course', lowercase), or empty-but-present strings to the importOutcomes mutation.

Common situations: Clients using human-readable labels ('account') instead of the GraphQL class names; case-sensitivity mistakes after switching from enums to strings; typos in saved API scripts.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

          "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?
        raise GraphQL::ExecutionError, I18n.t("no such target context")
      end

      [target_context, target_context.root_outcome_group]
    end
  end

  def context_class(context_type)
    raise NameError unless VALID_CONTEXTS.include? context_type

    context_type.constantize
  end

  def process_job(source_context:, target_group:, group: nil, outcome_id: nil)
    target_context = target_group.context

View on GitHub (pinned to 1c9f0bb801)