Skip to content

Commit ee5bc96

Browse files
artparclaude
andcommitted
Fix relate/unrelate to use JSON:API relationships endpoint
Use PATCH/DELETE to /api/{entity}/{id}/relationships/{relation} with array body, matching the JSON:API spec and dashboard3 implementation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2506ff6 commit ee5bc96

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

client/relation.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,32 @@ func (e *ExtendedClient) FindRelated(entityName, referenceId, relationColumn str
3838
return result, nil
3939
}
4040

41-
// AddRelation associates a target entity with a source via a relationship.
42-
// POST /api/{entity}/{referenceId}/{relationColumn}
41+
// AddRelation associates a target entity with a source via JSON:API relationships endpoint.
42+
// PATCH /api/{entity}/{referenceId}/relationships/{relationColumn}
4343
func (e *ExtendedClient) AddRelation(entityName, referenceId, relationColumn, targetType, targetRefId string) error {
4444
body := map[string]interface{}{
45-
"data": map[string]interface{}{
46-
"type": targetType,
47-
"id": targetRefId,
45+
"data": []map[string]interface{}{
46+
{"type": targetType, "id": targetRefId},
4847
},
4948
}
5049

51-
resp, err := e.nextRequest().SetBody(body).Post(
52-
e.Endpoint + "/api/" + entityName + "/" + referenceId + "/" + relationColumn,
50+
resp, err := e.nextRequest().SetBody(body).Patch(
51+
e.Endpoint + "/api/" + entityName + "/" + referenceId + "/relationships/" + relationColumn,
5352
)
5453
return e.checkResponse(resp, err)
5554
}
5655

57-
// RemoveRelation removes a relationship association.
58-
// DELETE /api/{entity}/{referenceId}/{relationColumn}/{targetRefId}
59-
func (e *ExtendedClient) RemoveRelation(entityName, referenceId, relationColumn, targetRefId string) error {
60-
resp, err := e.nextRequest().Delete(
61-
e.Endpoint + "/api/" + entityName + "/" + referenceId + "/" + relationColumn + "/" + targetRefId,
56+
// RemoveRelation removes a relationship association via JSON:API relationships endpoint.
57+
// DELETE /api/{entity}/{referenceId}/relationships/{relationColumn}
58+
func (e *ExtendedClient) RemoveRelation(entityName, referenceId, relationColumn, targetType, targetRefId string) error {
59+
body := map[string]interface{}{
60+
"data": []map[string]interface{}{
61+
{"type": targetType, "id": targetRefId},
62+
},
63+
}
64+
65+
resp, err := e.nextRequest().SetBody(body).Delete(
66+
e.Endpoint + "/api/" + entityName + "/" + referenceId + "/relationships/" + relationColumn,
6267
)
6368
return e.checkResponse(resp, err)
6469
}

cmd/relate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func unrelateCommand(appCtx *AppContext) *cli.Command {
4848
return fmt.Errorf("usage: unrelate <entity> <reference_id> <relation_column> <target_ref_id>")
4949
}
5050

51-
err := appCtx.Client.RemoveRelation(entity, refId, relation, targetRefId)
51+
targetType := strings.TrimSuffix(relation, "_id")
52+
err := appCtx.Client.RemoveRelation(entity, refId, relation, targetType, targetRefId)
5253
if err != nil {
5354
return err
5455
}

0 commit comments

Comments
 (0)