Packages

The extension for building GraphQL APIs with Ash

Current section

200 Versions

Jump to

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…