Skip to content

[JSON:API] Some custom fetch settings are overridden by defaults #941

Open

Description

@SafaAlfulaij

const settings = {
...requestProcessor.buildFetchSettings(request),
method: 'PATCH',
json: requestDoc
};

I have several cases that I need to call custom endpoints to act on records. These endpoints doesn't comply fully with JSON:API, but I'd like Orbit to call them since they return proper formatted JSON:API responses that I would like Orbit to process (add to cache, etc).

Currently:

{
  "data": {
    "id": "1",
    "type": "planet",
    "attributes": {
      "name": "Custom Planet",
      "oxygen": 0.3
    }
  }
}

Example:

remote.update(
  (t) => t.updateRecord({"type": "planet", "id": "1"}),
  {
    sources: {
        remote: {
          settings: {
              method: "POST",
          },
          url: `/api/planets/1/purify-air/`,
        },
    },
  }
);

(Just an example, purify-air can change a lot of other things based on different planet conditions, so can't simply say replaceAttribute(..., "oxygen", 1))
Return:

{
  "data": {
    "id": "1",
    "type": "planet",
    "attributes": {
      "name": "Custom Planet",
      "oxygen": 1
    }
  }
}

Note that I'm using POST instead of PATCH for this custom endpoint, and would like Orbit to treat it as updateRecord/PATCH

Activity

  1. dgeb commented on Mar 21, 2022

    @dgeb
    Member

    We should probably reverse the order of these settings, so that custom settings can always be used to override defaults. For example:

    const settings = { 
       method: 'PATCH', 
       json: requestDoc,
       ...requestProcessor.buildFetchSettings(request)
    };

    Of course, we'll need some tests to ensure that this doesn't have unintended side effects.

  2. SafaAlfulaij commented on Mar 21, 2022

    @SafaAlfulaij
    Author

    Exactly. I'm not sure though why it was not like that when you wrote it :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions