You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/topics/relationships.md
+95Lines changed: 95 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,101 @@ Non-map argument types, e.g `argument :author, :integer` (expecting an author id
73
73
JSON:API, because it expects `{"type": _type, "id" => id}` for relationship values. To support non-map arguments in `relationship_arguments`,
74
74
instead of `:author`, use `{:id, :author}`. This works for `{:array, _}` type arguments as well, so the value would be a list of ids.
75
75
76
+
77
+
## Creating related resources without the id
78
+
79
+
This feature is useful for creating relationships without requiring two separate API calls and without needing to associate resources with an ID first. This is an escape hatch from the standard approach and is not JSON:API spec compliant, but it is completely possible to implement.
80
+
81
+
```elixir
82
+
# With a post route that references the `leads` argument, this will mean that
83
+
# locations will have the ability to create Lead resources when called from
84
+
# the API
85
+
json_api do
86
+
routes do
87
+
base_route "/location", Marketplace.Locationdo
88
+
post :create, relationship_arguments: [:leads]
89
+
end
90
+
91
+
base_route "/lead", Marketplace.Leaddo
92
+
post :create
93
+
end
94
+
end
95
+
end
96
+
97
+
98
+
# In the leads resource you will have the following:
This way, when requesting to create a location, leads will be automatically created as well.
129
+
130
+
```json
131
+
{
132
+
"data": {
133
+
"type": "location",
134
+
"attributes": {
135
+
"name": "Test Location",
136
+
"location": {
137
+
"lat": 32323,
138
+
"long": 23232,
139
+
"address": "test street 123"
140
+
},
141
+
"images": ["url1", "url2", "url3"]
142
+
},
143
+
"relationships": {
144
+
"leads": {
145
+
"data": [
146
+
{
147
+
"type": "lead",
148
+
"meta": {
149
+
"type": "Roof",
150
+
"description": "roofing has 3 holes to fix",
151
+
"priority": "high"
152
+
}
153
+
},
154
+
{
155
+
"type": "lead",
156
+
"meta": {
157
+
"type": "garden",
158
+
"description": "Garden looks like it could be polished",
159
+
"priority": "medium"
160
+
}
161
+
}
162
+
]
163
+
}
164
+
}
165
+
}
166
+
}
167
+
```
168
+
169
+
Note that the related data won't be returned unless included with the include query parameter. For the JSON:API specification on the include parameter, see [json api fetching includes](https://jsonapi.org/format/#fetching-includes).
170
+
76
171
## Relationship Manipulation Routes
77
172
78
173
You can also specify routes that are dedicated to manipulating relationships. We generally suggest the above approach, but JSON:API spec also allows for dedicated relationship routes. For example:
0 commit comments