@@ -30,10 +30,11 @@ Since this project is under development, please pin your dependencies to avoid p
3030- documentation
3131- examples for other ORMs
3232
33- ## Docs
34- You should take a look at the [ examples] ( examples ) directory for implementation ideas .
33+ ## Documentation
34+ You should take a look at the [ examples] ( examples ) directory for implementations .
3535
36- After you've decided which ORM to use, you can start writing the associated schemas,
36+ ### Defining a schema
37+ After you've decided which ORM to use, you can start writing the associated schemas
3738using the marshmallow_jsonapi library (which itself is extending [ marshmallow] ( https://marshmallow.readthedocs.io/ ) ).
3839
3940``` python
@@ -71,6 +72,7 @@ You can also generate the associated `links` object by specifying more options i
7172``` python
7273from marshmallow_jsonapi import fields
7374from starlette_jsonapi.schema import JSONAPISchema
75+
7476class SomeSchema (JSONAPISchema ):
7577 id = fields.Str(dump_only = True )
7678 name = fields.Str()
@@ -82,8 +84,28 @@ class SomeSchema(JSONAPISchema):
8284 self_route_many = ' some-resource:get_all'
8385```
8486
87+ ### Defining a resource
88+
89+ Once the schema is defined, we can add the associated resource class and link them by setting the ` schema ` attribute
90+ on the resource class.
91+
92+ Resources are implemented by subclassing ` starlette_jsonapi.resource.BaseResource ` .
93+ A json: api compliant service will implement GET, POST, PATCH, DELETE HTTP methods on a resource,
94+ so the ` BaseResource ` comes with 5 methods that you can override:
95+ - ` get -> handling GET /<id> `
96+ - ` patch -> handling PATCH /<id> `
97+ - ` delete -> handling DELETE /<id> `
98+ - ` get_all -> handling GET / `
99+ - ` post -> handling POST / `
100+
101+ All methods return 405 Method Not Allowed by default.
102+ You can also customize ` allowed_methods ` to a subset of the above HTTP methods.
103+
104+ Additionally, the ` prepare_relations ` method is available for enabling inclusion of related resources.
105+ Since the json: api specification does not enforce this, the default implementation will skip inclusion
106+ in order to avoid data leaks.
85107
86- Once the schema is defined, we can move to adding the associated resource class
108+ Example:
87109``` python
88110from starlette_jsonapi.resource import BaseResource
89111from starlette_jsonapi.responses import JSONAPIResponse
@@ -135,6 +157,7 @@ class ExampleResource(BaseResource):
135157
136158 # We didn't ask for a partial deserialization, so a required field shouldn't throw a KeyError
137159 example[' some_required_field' ] = body[' some_required_field' ]
160+ examples_db[example[' id' ]] = example
138161
139162 return await self .serialize(example)
140163```
0 commit comments