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

modelName being used as payloadKey #465

Open
Techn1x opened this issue May 30, 2022 · 0 comments
Open

modelName being used as payloadKey #465

Techn1x opened this issue May 30, 2022 · 0 comments

Comments

@Techn1x
Copy link
Contributor

Techn1x commented May 30, 2022

I have a model, adapter and serializer for a library-book, which are are all jsonapi

library-book/
- model.ts
- adapter.ts
- serializer.ts

Nothing weird so far. However, the API that my application talks to, sets the type / payload key as book. In order for the above to talk to the API correctly, my serializer adds the library- prefix on incoming payloads (via modelNameFromPayloadKey), and strips the prefix on outgoing requests (via payloadKeyFromModelName)

This all works great against the real API, but I've been having challenges getting that to work with factory guy.

Consider the following example

const json = buildList('library-book', { name: 'a' }, { name: 'b' }).add({ meta: { page: 1 } })
mockQuery('library-book').returns({ json })

When running a test that calls store.query('library-book', { page: 1 }) and debugging the incoming payload, it seems the type key (payload key) is set to 'library-book' when I need it to be 'book'. I would have thought that factory guy would call payloadKeyFromModelName from the serializer to figure out the necessary payload key to use, but my debugging shows that it doesn't seem to be called.

A quick search on this repo brings up this, which seems to indicate that payloadKeyFromModelName should be called when serializeMode is true - but I don't really know what that means or if I am able to set that easily.

super(store, { transformKeys, serializeMode });
this.typeTransformFn = this.serializeMode
? this.typeTransformViaSerializer
: dasherize;
this.defaultKeyTransformFn = dasherize;
this.polymorphicTypeTransformFn = dasherize;
this.included = [];
}
typeTransformViaSerializer(modelName) {
let serializer = this.store.serializerFor(modelName);
return serializer.payloadKeyFromModelName(modelName);

Any help appreciated, thanks! (Even if the answer is "you can't do that", it'll save me digging into this further)

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

1 participant