Skip to content

Subrequests - #68

Merged
jgaehring merged 50 commits into
farmOS:mainfrom
jgaehring:subrequests
Oct 25, 2022
Merged

jgaehring merged 50 commits into
farmOS:mainfrom
jgaehring:subrequests

Conversation

@jgaehring

Copy link
Copy Markdown
Member

This still has some issues, but I couldn't help committing some changes to the overall project structure along the way, which I'm too lazy to try and disentangle, so I just added some warnings instead.

This branches off #67 and should be merged subsequently.

@jgaehring
jgaehring force-pushed the subrequests branch 2 times, most recently from 447e100 to 24aacad Compare October 19, 2022 19:11
@jgaehring

Copy link
Copy Markdown
Member Author

This still has some issues

Namely:

// FIXME: This isn't working, presumably because the server is not
// conforming to the JSON:API spec for to-many relationships. See:
// https://jsonapi.org/format/#crud-updating-to-many-relationships
const uuid = `{{${requestId}.body@$.data.id}}`;
const blueprint = {
requestId: `${requestId}.${field}`,
// Note the entity's relationship endpoint is used here.
uri: `${BASE_URI}/${entity}/${bundle}/${uuid}/relationships/${field}`,
waitFor: [reqId, requestId],
action: 'create',
headers,
body: JSON.stringify({
data: [{
id: `{{${reqId}.body@$.data[*].id}}`,
type: path([reqId, 'type'], ready),
}],
}),
};
posthoc.push(blueprint);

@mstenta or @paul121, any guess why a POST request to a an entity's relationship endpoint returns a 204 w/o updating that relationship?

Here's the example I tested locally, with a preexisting input log and quantity:

const uri = '/api/log/input/f9f44a71-4373-4a60-ac36-37f5564b86e8/relationships/quantity';
const quantity = {
id: '9fb2963d-7185-4da7-9ccd-d6e29e0f22c1',
type: 'quantity--standard',
};
const data = JSON.stringify({ data: [quantity] });
return farm.remote.request(uri, { method: 'POST', data });

Here's the relevant JSON:API spec:
https://jsonapi.org/format/#crud-updating-to-many-relationships

@paul121

paul121 commented Oct 19, 2022 •

Copy link
Copy Markdown
Member

Log quantities are a bit unique because they have a revision ID as well. When you update an existing quantity then the log needs to be updated to that new revision as well. I haven't tried creating or updating log quantities via the relationship endpoint so there might be some gotchas there.

Some python code I have where I first create a quantity entity and then create/update a log in a second request. This is the part that specifies the quantity relationship & quantity revision ID:

# Add quantity to log.
log["relationships"]["quantity"] = {
    "data": [
        {
            "type": "quantity--standard",
            "id": harvest_quantity["data"]["id"],
            "meta": {
                "target_revision_id": harvest_quantity["data"]["attributes"]["drupal_internal__revision_id"]
            }
        },
    ]
}

Maybe this would work, assuming a quantityRevisionId variable is populated:

 const uri = '/api/log/input/f9f44a71-4373-4a60-ac36-37f5564b86e8/relationships/quantity'; 
 const quantity = { 
   id: '9fb2963d-7185-4da7-9ccd-d6e29e0f22c1', 
   type: 'quantity--standard',
   meta: {
     target_revision_id: quantityRevisionId,
   },
 }; 
 const data = JSON.stringify({ data: [quantity] });
 return farm.remote.request(uri, { method: 'POST', data }); 

@jgaehring

Copy link
Copy Markdown
Member Author

Oh that sounds like a good lead! Thanks, @paul121, I will try that out tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants