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

invalid context type

Error message

invalid context type

What it means

GraphQLHelpers::ContextFetcher.context_fetcher first checks that input[:context_type] is among valid_context_types; if the client passes a context_type not in the allowlist, it raises GraphQL::ExecutionError 'invalid context type' before attempting lookup.

Solutions

  1. Pass context_type exactly matching an allowed value for the field (e.g. 'Course' or 'Account').
  2. Check the field/mutation documentation for its valid_context_types list.
  3. Ensure context_type is not nil — send it explicitly.

Example fix

// before
mutation { createConversationMessage(input: {contextType: "Group", ...}) }
// after
mutation { createConversationMessage(input: {contextType: "Course", ...}) }
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try { await mutation(input) } catch (e) { if (e.message === 'invalid context type') { /* correct contextType and retry */ } else throw e }

Prevention

When it happens

Trigger: A GraphQL mutation/query passing contextType like 'Group' or 'User' when only ['Course','Account'] are valid for that field, or a misspelled/nil context_type.

Common situations: Clients copying contextType values between fields with different allowlists; typos in casing ('course' vs 'Course'); forgetting to send contextType at all so it is nil.

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

Appendix: source

Thrown at app/graphql/graphql_helpers/context_fetcher.rb:24

# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

module GraphQLHelpers::ContextFetcher
  def context_fetcher(input, valid_context_types = [])
    if valid_context_types.exclude?(input[:context_type])
      raise GraphQL::ExecutionError, I18n.t("invalid context type")
    end

    context =
      begin
        context_type = Object.const_get(input[:context_type])
        context_type.find_by(id: input[:context_id])
      rescue
        raise GraphQL::ExecutionError, I18n.t("invalid context type")
      end
    raise GraphQL::ExecutionError, I18n.t("context not found") if context.nil?

    context
  end
end

View on GitHub (pinned to 1c9f0bb801)