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

[Feature] Serialize resources using schemas #2

Merged
merged 10 commits into from
Feb 2, 2021

Conversation

lindyhopchris
Copy link
Contributor

@lindyhopchris lindyhopchris commented Jan 30, 2021

Makes the Core\Resources\JsonApiResource class non-abstract, by injecting the schema as well as the model into the class constructor. Attributes and relationships can then be serialized using the schema. To do this, the schema's fields must implement serializable contracts:

  • Contracts\Resources\Serializer\Attribute
  • Contracts\Resources\Serializer\Relation

These are separate interfaces so that serializable fields are always optional - for example, a non-Eloquent implementation might always need resources to exist rather than supporting serialization via the schema.

To wire this in, the Core\Resources\ResourceResolver now checks whether the computed resource class name exists, and if it doesn't it falls back on the default implementation - i.e. using the JsonApiResource class. The resource factory has been updated so that it injects both the schema and the model into the resource class that it is building.

There were some additional changes implemented at the same time:

  • The JsonApiResource method signatures for attributes, relationships, meta and links have been updated to receive the request as a parameter. This brings it in line with Laravel's Eloquent resources, though in our implementation the request can be null (for example, if serializing for broadcasting).
  • The Encoder contract now has a withRequest method to inject the request into the encoding process. (The encoder now needs access to the request because of the changes in the previous bullet point.)

@lindyhopchris lindyhopchris merged commit a05802a into develop Feb 2, 2021
@lindyhopchris lindyhopchris deleted the feature/schema-resources branch February 2, 2021 09:38
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

Successfully merging this pull request may close these issues.

1 participant