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

Ability to attach foreign model/dto objects to the JSON tree without any defined relationships #90

Open
vincesocal opened this issue Dec 7, 2022 · 5 comments

Comments

@vincesocal
Copy link

@scott-mcdonald in one of my projects I need to return some straggler objects without any relationships in the JSON response, but the issue #69 prevent me in doing so. Can we add this to the framework. If you need help on this, please let me know.

@vincesocal
Copy link
Author

linked to #88

@vincesocal
Copy link
Author

linked to #87

@scott-mcdonald
Copy link
Owner

@vincesocal I asked the original author of #69 and I'll ask you the same question, can you provide a JSON API document example of what you are asking for. My preliminary assessment is this violates the specification full linkage requirement? JsonApiFramework include feature works by traversing the relationships tree in the model in order to create the respective in-memory DOM tree of the document based on the specification...

@tylerton
Copy link

tylerton commented Jan 3, 2023

Hello @scott-mcdonald , I originally posted the #88. Here is an example of what we doing now.

An application model has persons.
Each person has person-type and incomes.
Each income has income-type.

So, we want to include all of persons, person-types, incomes, income-types in a call of application. There are some models are not directly linked to the application. Is there any way to include those to a single API call? Or is there anything we can help on that please?

@scott-mcdonald
Copy link
Owner

@tylerton The answer to your question needs to be broken up into multiple parts:

Part 1 - JSON API Spec
From the JSON API specification level you can accomplish what you are asking above with respect to the server request with the include parameter you are not doubt familiar with. The include query parameter would be the following:

/application/1234?include=persons.person-type,persons.incomes.income-type

Part 2 - Server Implementation
First thing I need to note is JsonApiFramework allows you to define a service model and serializes/deserializes based on that service model a DOM tree you create at runtime into json:api documents according to the JSON API specification. It has no knowledge about your database or how to "get" the data.

JsonApiFramework is a low to medium level framework where you will need to develop higher level frameworks that understand your domain and data access layer, etc. These higher level frameworks will parse the include query parameter, know how to pull the requested data, and use JsonApiFramework to build the json:api document to return as the response of the request itself.

So to further assist you I will tell you at a very high level how I accomplished this on several work projects that use JsonApiFramework:

  • I used the Repository design pattern to create a layer that abstracts away the data access layer itself for each possible JSON API resource type.
  • I used the Registry design pattern to create a well known registry of Repository instances per resource type
  • At runtime for each request, I create a JSON API document programmatically in parts
    • For the primary data, lookup the respective repository via the repository registry to get the primary data
    • For the optional included data, "traverse" the include query parameter which uses rel names and the dot '.' operator per the specification. For each rel traversed you will know the "from" resource type and the rel name where you use the JsonApiFramework service model at runtime to lookup the relationship itself; which gives you the "to" resource type. Now you know the "from" type and "from" primary keys, you know the "to" type and the relationship name, use the respective repository to get the related data. Include that in the document, then go on to the next related resource until you have traversed the entire include request... This is the challenging part but this is what software engineering is...

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

3 participants