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
- Use the exact class names 'Course' or 'Account' for targetContextType
- Validate the type value client-side against the allowed set before calling the mutation
- 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
- Use exact class names 'Course' and 'Account', never lowercase labels
- Derive the type from a fixed constant, not user input
- Trim and case-normalize strings before sending
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
- Outcome is not available in context #
- Outcome is not available in context #
- targetContextId required if targetContextType provided
- targetContextType required if targetContextId provided
- A maximum of 50 assessees can be provided at once
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.contextView on GitHub (pinned to 1c9f0bb801)