Skip to content

Commit b53a248

Browse files
committed
added use Ash.TypedStruct to docs
1 parent 843c844 commit b53a248

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

documentation/topics/use-struct-types-with-graphql.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ To get structured GraphQL input types for standalone data arguments, create a cu
2525

2626
### Step 1: Create a custom type
2727

28+
## Choosing Your Approach
29+
30+
**For new standalone data structures:**
31+
32+
```elixir
33+
defmodule MyApp.Types.TicketMetadataType do
34+
use Ash.TypedStruct
35+
36+
typed_struct do
37+
field :priority, :string, allow_nil?: false
38+
field :category, :string, allow_nil?: false
39+
field :tags, {:array, :string}, allow_nil?: true
40+
end
41+
42+
use AshGraphql.Type
43+
44+
@impl true
45+
def graphql_type(_), do: :ticket_metadata
46+
47+
@impl true
48+
def graphql_input_type(_), do: :create_ticket_metadata_input
49+
end
50+
```
51+
52+
**For referencing existing resource structures:**
53+
2854
```elixir
2955
defmodule MyApp.Types.TicketMetadataType do
3056
use Ash.Type.NewType,
@@ -43,6 +69,18 @@ defmodule MyApp.Types.TicketMetadataType do
4369
end
4470
```
4571

72+
## Comparison
73+
74+
| Aspect | Ash.TypedStruct | Ash.Type.NewType + instance_of |
75+
|--------|-----------------|--------------------------------|
76+
| **Best for** | New standalone structures | Referencing existing resources |
77+
| **Field definition** | Manual field declarations | Automatic from target resource |
78+
| **Type safety** | Constructor functions (`new/1`, `new!/1`) | Inherits from target resource |
79+
| **Validation** | Custom field constraints | Resource's existing validations |
80+
| **Maintenance** | Fields must be kept in sync manually | Automatically syncs with resource changes |
81+
| **DRY principle** | Duplicates field definitions | References single source of truth |
82+
| **Flexibility** | Full control over structure | Constrained by target resource |
83+
4684
### Step 2: Use the custom type in your resource
4785

4886
```elixir
@@ -68,6 +106,7 @@ input AddMetadataInput {
68106
input CreateTicketMetadataInput {
69107
priority: String!
70108
category: String!
109+
tags: [String!]
71110
}
72111
```
73112

0 commit comments

Comments
 (0)