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

targetContextId required if targetContextType provided

Error message

targetContextId required if targetContextType provided

What it means

The mirror case of error 210 in ImportOutcomes#get_target: targetContextType is given without targetContextId, so the mutation cannot locate a concrete context. It raises this ExecutionError to enforce that the type and ID are always provided together.

Solutions

  1. Include a valid targetContextId matching the given targetContextType
  2. Remove targetContextType entirely and use targetGroupId if no target context is needed
  3. Add client-side validation requiring both fields be set or both be blank

Example fix

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

Strategy: validation

Validate before calling

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

Try / catch

try {
  await client.mutate({ mutation: IMPORT_OUTCOMES, variables: { input } });
} catch (e) {
  if (e.message.includes('targetContextId required')) {
    // require selecting the target entity before submit
  }
}

Prevention

When it happens

Trigger: Calling importOutcomes with input { targetContextType: "Account" } but blank targetContextId.

Common situations: Hardcoded type constants in clients while the ID variable resolves to null/empty; UI state where a type selection persists but the entity was cleared; copy-paste mutations missing the ID field.

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/4469c6913a5b344a. Report an issue: GitHub.

Appendix: source

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

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

      [target_context, target_context.root_outcome_group]

View on GitHub (pinned to 1c9f0bb801)