Packages
ash_graphql
1.5.0
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.17
1.7.16
1.7.15
1.7.14
1.7.13
1.7.12
1.7.11
1.7.10
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.5
1.0.0-rc.4
retired
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.28.1
0.28.0
0.27.1
0.27.0
0.26.9
0.26.8
0.26.7
0.26.6
0.26.5
0.26.4
0.26.3
0.26.2
0.26.0
0.25.13
0.25.12
0.25.10
0.25.9
0.25.8
0.25.7
0.25.6
0.25.5
0.25.4
0.25.3
0.25.2
0.25.1
0.25.0
0.24.1
0.24.0
0.23.3
0.23.2
0.23.1
0.23.0
0.22.13
0.22.12
0.22.11
0.22.10
0.22.9
0.22.8
0.22.7
0.22.6
0.22.4
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
retired
0.20.5
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0-rc.3
0.20.0-rc.2
0.20.0-rc.1
0.20.0-rc.0
0.19.0
0.18.0-rc0
0.17.5
0.17.5-rc0
0.17.4
0.17.2
0.17.1
0.17.0
0.16.28
0.16.27
0.16.26
0.16.25
0.16.24
0.16.23
0.16.22
0.16.21
0.16.20
0.16.18-rc5
0.16.18-rc4
0.16.18-rc3
0.16.18-rc2
0.16.18-rc1
0.16.18-rc0
0.16.17
0.16.16
0.16.15
0.16.14
0.16.13
0.16.12
0.16.11
0.16.10
0.16.9
0.16.8
0.16.7
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.10
0.15.9
0.15.8
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.1
0.14.0
0.13.1
0.13.0
0.12.5
0.12.4
0.12.3
0.12.1
0.12.0
0.10.0
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
0.1.2
The extension for building GraphQL APIs with Ash
Current section
200 Versions
Jump to
Current section
200 Versions
Compare versions
11
files changed
+221
additions
-46
deletions
| @@ -5,6 +5,19 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline | |
| 5 5 | |
| 6 6 | <!-- changelog --> |
| 7 7 | |
| 8 | + ## [v1.5.0](https://github.com/ash-project/ash_graphql/compare/v1.4.7...v1.5.0) (2025-01-10) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + ### Features: |
| 14 | + |
| 15 | + * errors: carry action name in context (#257) |
| 16 | + |
| 17 | + ### Improvements: |
| 18 | + |
| 19 | + * error handling in resources (#253) |
| 20 | + |
| 8 21 | ## [v1.4.7](https://github.com/ash-project/ash_graphql/compare/v1.4.6...v1.4.7) (2024-12-20) |
| @@ -72,6 +72,7 @@ end | |
| 72 72 | | [`generate_object?`](#graphql-generate_object?){: #graphql-generate_object? } | `boolean` | `true` | Whether or not to create the GraphQL object, this allows you to manually create the GraphQL object. | |
| 73 73 | | [`filterable_fields`](#graphql-filterable_fields){: #graphql-filterable_fields } | `list(atom)` | | A list of fields that are allowed to be filtered on. Defaults to all filterable fields for which a GraphQL type can be created. | |
| 74 74 | | [`nullable_fields`](#graphql-nullable_fields){: #graphql-nullable_fields } | `atom \| list(atom)` | | Mark fields as nullable even if they are required. This is useful when using field policies. See the authorization guide for more. | |
| 75 | + | [`error_handler`](#graphql-error_handler){: #graphql-error_handler } | `mfa` | `{AshGraphql.DefaultErrorHandler, :handle_error, []}` | Set an MFA to intercept/handle any errors that are generated. | |
| 75 76 | |
| 76 77 | |
| 77 78 | ## graphql.queries |
| @@ -16,6 +16,28 @@ end | |
| 16 16 | |
| 17 17 | If you are doing authorization, you'll need to provide an `actor`. |
| 18 18 | |
| 19 | + ### Using AshAuthentication |
| 20 | + |
| 21 | + If you have not yet installed AshAuthentication, you can install it with igniter: |
| 22 | + |
| 23 | + ```bash |
| 24 | + # installs ash_authentication & ash_authentication_phoenix |
| 25 | + mix igniter.install ash_authentication_phoenix |
| 26 | + ``` |
| 27 | + |
| 28 | + If you've already set up `AshGraphql` before adding `AshAuthentication`, you will |
| 29 | + just need to make sure that your `:graphql` scope in your router looks like this: |
| 30 | + |
| 31 | + ```elixir |
| 32 | + pipeline :graphql do |
| 33 | + plug :load_from_bearer |
| 34 | + plug :set_actor, :user |
| 35 | + plug AshGraphql.Plug |
| 36 | + end |
| 37 | + ``` |
| 38 | + |
| 39 | + ### Using Something Else |
| 40 | + |
| 19 41 | To set the `actor` for authorization, you'll need to add an `actor` key to the |
| 20 42 | absinthe context. Typically, you would have a plug that fetches the current user and uses `Ash.PlugHelpers.set_actor/2` to set the actor in the `conn` (likewise with `Ash.PlugHelpers.set_tenant/2`). |
| @@ -81,6 +81,82 @@ defmodule AshGraphql.DefaultErrorHandler do | |
| 81 81 | end |
| 82 82 | ``` |
| 83 83 | |
| 84 | + ### Error handler in resources |
| 85 | + Error handlers can also be specified in a resource. For examples: |
| 86 | + |
| 87 | + ```elixir |
| 88 | + defmodule MyApp.Resource do |
| 89 | + use Ash.Resource, |
| 90 | + domain: [MyApp.Domain], |
| 91 | + extensions: [AshGraphql] |
| 92 | + |
| 93 | + graphql do |
| 94 | + type :ticket |
| 95 | + error_handler {MyApp.Resource.GraphqlErrorHandler, :handle_error, []} |
| 96 | + end |
| 97 | + |
| 98 | + # ... |
| 99 | + end |
| 100 | + ``` |
| 101 | + |
| 102 | + If both an error handler for the resource and one for the domain are defined, |
| 103 | + they both take action: first the resource handler and then the domain handler. |
| 104 | + |
| 105 | + If an action on a resource calls other actions (e.g. with a |
| 106 | + `manage_relationships`) the errors are handled by the primary resource that |
| 107 | + called the action. |
| 108 | + |
| 109 | + ### Filtering by action |
| 110 | + |
| 111 | + The error handler carries in the context the name of the primary action that |
| 112 | + returned the error down the line. With that one can set different behaviors |
| 113 | + depending on the specific action that triggered the error. For example consider |
| 114 | + the following resource with `:create`, `:custom_create` and `:update` actions: |
| 115 | + |
| 116 | + ```elixir |
| 117 | + defmodule MyApp.Resource do |
| 118 | + use Ash.Resource, |
| 119 | + domain: [MyApp.Domain], |
| 120 | + extensions: [AshGraphql] |
| 121 | + |
| 122 | + graphql do |
| 123 | + type :ticket |
| 124 | + error_handler {MyApp.Resource.GraphqlErrorHandler, :handle_error, []} |
| 125 | + end |
| 126 | + |
| 127 | + actions do |
| 128 | + deafults [:read, :destroy, :create] |
| 129 | + create :custom_create do |
| 130 | + # ... |
| 131 | + change manage_relationships # ... |
| 132 | + end |
| 133 | + |
| 134 | + update :update do |
| 135 | + # ... |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + ``` |
| 140 | + |
| 141 | + The error handler `MyApp.Resource.GraphqlErrorHandler` can in this case set |
| 142 | + different behaviors depending on the specific action that caused the error: |
| 143 | + |
| 144 | + ```elixir |
| 145 | + defmodule MyApp.Resource.GraphqlErrorHandler do |
| 146 | + |
| 147 | + def handle_error(error, context) do |
| 148 | + %{action: action} = context |
| 149 | + |
| 150 | + case action do |
| 151 | + :custom_create -> custom_create_behavior(error) |
| 152 | + :update -> update_behavior(error) |
| 153 | + |
| 154 | + _ -> deafult_behvaior(error) |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | + ``` |
| 159 | + |
| 84 160 | ## Custom Errors |
| 85 161 | |
| 86 162 | If you created your own Errors as described in the [Ash Docs](https://hexdocs.pm/ash/error-handling.html#using-a-custom-exception) you also need to implement |
| @@ -22,7 +22,7 @@ mix igniter.install ash_graphql | |
| 22 22 | def deps() |
| 23 23 | [ |
| 24 24 | ... |
| 25 | - {:ash_graphql, "~> 1.4.7"} |
| 25 | + {:ash_graphql, "~> 1.5.0"} |
| 26 26 | ] |
| 27 27 | end |
| 28 28 | ``` |
Loading more files…