Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SafaAlfulaij opened this issue Mar 19, 2022 · 2 comments
Open

Comments

@SafaAlfulaij
Copy link

SafaAlfulaij commented Mar 19, 2022

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

@dgeb
Copy link
Member

dgeb commented Mar 21, 2022

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.

@SafaAlfulaij
Copy link
Author

SafaAlfulaij commented Mar 21, 2022

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants