datx
datx copied to clipboard
Query builder
Please select all that apply:
- [x] This PR contains a new feature
- [x] This PR updates an existing feature
- [ ] This PR contains bugfixes
- [x] This PR contains all the relevant tests
- [x] This PR creates a breaking change
Please describe the differences between the current and new behavior Refactor the whole network core
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { Collection, Model } from '@datx/core';
import { Client, Request, QueryBuilder } from '@datx/network';
import { SwrRequest } from '@datx/react';
import { JsonApiQueryBuilder } from '@datx/jsonapi';
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
});
const httpMock = TestBed.inject(HttpClient);
const pc = new Client({
QueryBuilder: JsonApiQueryBuilder,
collection: new Collection(),
network: new Network.Promise(window.fetch),
request: SwrRequest,
});
const rc = new Client({
QueryBuilder,
collection: new Collection(),
network: new Network.Rx(httpMock),
request: Request,
});
class ModelA extends Model {
public a = 'a';
}
class ModelB extends Model {
public b = 'b';
}
pc.from(ModelA)
.id('1')
.buildRequest()
.fetch()
.then((resp) => {
console.log(resp.data?.a);
});
pc.from(ModelB)
.id('1')
.buildRequest(
(client, respB) => respB.data && client.from(ModelA).id(respB.data.b).buildRequest(),
)
.swr();
rc.from(ModelA)
.id('1')
.buildRequest()
.fetch()
.subscribe((resp) => {
// @ts-expect-error
console.log(resp.data?.b);
});
rc.from(ModelA)
.match({ a: 2 })
.buildRequest(
(client, respA) =>
respA.data &&
client
.from(ModelA)
.match({ a: respA.data?.map((a) => a.a).join(',') })
.buildRequest(),
)
.fetch()
.subscribe((resp) => {
console.log(resp.data?.a);
});
The network-core tests are passing, but jsonapi and later tests are failing.
This is still not feature complete (e.g. missing headers), but it's a good baseline to continue working on other things, so I would like to have this as a first alpha version.
@fvoska and @isBatak I'm requesting a review from you since you two were mostly involved in the API design. No need to go into the code if you don't understand it, but just to see if everything here makes sense. Also, feel fre to make a list of things we're still missing (like the mentioned headers).
JsonApiQueryBuilder
and SwrRequest
are out of scope there, they will be part of @datx/jsonapi
and @datx/react
packages - we just need to make sure this core part can support their requirements.
Just to confirm: All relevant tests are passing. The pipeline is failing because of test failures in the react package, but this is not in scope of this PR.
We can investigate the disappearance of the angular package later on and re-add it