Skip to content

Improve error message for generic actions used in#400

Open
dallingson wants to merge 2 commits intoash-project:mainfrom
dallingson:main
Open

Improve error message for generic actions used in#400
dallingson wants to merge 2 commits intoash-project:mainfrom
dallingson:main

Conversation

@dallingson
Copy link

Improve error message for generic actions used in GraphQL mutation blocks

Problem

When developers accidentally use a generic action (type :action) in a typed GraphQL mutation block (create, update, or destroy), they get a cryptic KeyError instead of a helpful error message.

Before:

** (KeyError) key :error_location not found in: %AshGraphql.Resource.Mutation{...}

Solution

Added early validation that catches the mistake and provides a clear error message with fix suggestions.

After:

** (RuntimeError) Invalid GraphQL mutation.

`create` references Ash action `:some_generic_action`, but that action has type `:action`.

GraphQL `create` mutations require an Ash action with type `:create`.

Resource: Safari.Outcrop.SupplementalDocument

Fix:
  • Define an Ash `:create` action
  • Or expose the generic action under `graphql.actions`

Changes

  • Added validation in mutation_types/4 to catch generic actions in typed mutation blocks early
  • Changed unsafe field access to Map.get/3 for defensive coding
  • Added tests covering all mutation types (create, update, destroy)

Contributor checklist

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

GraphQL mutation blocks by:
- Add validation in mutation_types/4 to prevent
generic :action being used in create/update/destroy
blocks
- Raise clear, actionable error with resource,
mutation, and action context
- Safely handle optional error_location to avoid
KeyError
- Add tests and test resources covering invalid and
valid generic action usage
@zachdaniel
Copy link
Contributor

Looks like some conflicts. I also know there is a failing build step, I'll address that separately. Please just confirm your tests pass locally and we can merge once the other checks pass 😄

@dallingson
Copy link
Author

@zachdaniel Yes, my tests do pass locally! :) If you notice anything else, just let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants