aribouius / jsonapi-react

A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
MIT License
149 stars 28 forks source link

Is schema strictly required? #28

Closed dariusk closed 2 years ago

dariusk commented 3 years ago

This is more a point of clarification that could lead to a README update rather than a bug.

From the source it seems like schema is not strictly required. Its presence is checked for, and it's used if you do any custom serialization or normalization, but for a simple API interaction where the client is just doing GET requests that pull down data, it seems optional.

Is my understanding correct, or is there somewhere it is likely to fail silently down the line?

If my understanding is correct, I'll make a README PR with clarification. For context, at work we spent some time trying to figure out how to automatically generate the schema and include it in our build process before I realized I could remove it entirely from the ApiClient configs and everything still seemed to work like normal.

aribouius commented 3 years ago

Hi @dariusk,

Your understanding is correct in that the schema is not required to use this library. When deserializing GET requests the schema doesn't play much of a role, other than specifying how specific attributes should be coerced. A typical JSONAPI response contains all of the resource mapping required to fully normalize it, so it is unlikely to fail silently as far as I know, provided the response follows the spec.

When I was first developing this library the schema really didn't become necessary until I started building support for mutations, since I had no other way of knowing which attributes were actually relationships when serializing the request. Additionally, the schema is also utilized to identify cached GET requests that should be invalidated as a result of a mutation.

While my initial intent was to make the library as simple as possible to use (which included making the schema optional), after about a year of using it in a production environment my opinion on this has shifted a bit, and I am now leaning towards requiring the schema in the next major release (no date on this yet, but likely not happening anytime soon), as it would help eliminate a lot of "a ha" moments when debugging mutations failing as a result of someone forgetting to update the schema.

dariusk commented 3 years ago

Thank you for the detailed response! Does your schema format follow any standard currently in use, or if not do you plan to make it use one? I ask because what I would like to do is make a PR to the jsonapi server library we are using to provide a function that generates a schema (which it currently lacks). It would be better for me to provide one that generates a standard rather than a feature that's only for compatibility with jsonapi-react.

aribouius commented 3 years ago

@dariusk the schema does not currently follow any standard, largely because the JSONAPI spec doesn't specify one. From what i've found its been discussed for a long time (example), but so far its only resulted in recommendations to use either OpenAPI or JSON Schema, neither of which I found particularly appealing initially. I would love to adopt something, so I'm certainly open to suggestions.