Skip to content

Commit

Permalink
Added support for additional types for resource
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima committed Dec 13, 2021
1 parent 8bff3c9 commit decf38c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
6 changes: 0 additions & 6 deletions library/encoder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ class Encoder {
}

encodeNodeType(node: DecodedNode, requiredTypes: Iri[], nodeId: NodeId) {
const inputTypes = Array.isArray(node.$type)
? node.$type
: node.$type
? [node.$type]
: [];

const finalTypes = new Set([...this.getNodeTypes(node), ...requiredTypes]);

finalTypes.forEach((type) => {
Expand Down
29 changes: 21 additions & 8 deletions library/resource/src/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,30 @@ export class QueryBuilder {
private getShape(
mainVar = "iri",
includeOptional = false,
wrapOptional = true
wrapOptional = true,
setSpecificTypes = true
) {
const conditions: (Quad | ReturnType<typeof $>)[] = [];

const populateConditionsRecursive = (s: Schema, varPrefix: string) => {
const rdfType = s["@type"];
const properties = getSchemaProperties(s);

rdfType.forEach((type) => {
conditions.push($`${variable(varPrefix)} a ${namedNode(type)} .`);
});
if (setSpecificTypes) {
rdfType.forEach((type) => {
conditions.push(
quad(variable(varPrefix), namedNode(rdf.type), namedNode(type))
);
});
} else {
conditions.push(
quad(
variable(varPrefix),
namedNode(rdf.type),
variable(`${varPrefix}_type`)
)
);
}

Object.keys(properties).forEach((prop, index) => {
const property = properties[prop];
Expand Down Expand Up @@ -96,9 +109,9 @@ export class QueryBuilder {

const query = CONSTRUCT`
${this.getResourceSignature()}
${this.getShape("iri", true, false)}
${this.getShape("iri", true, false, false)}
`.WHERE`
${this.getShape("iri", true, true)}
${this.getShape("iri", true, true, false)}
{
${selectSubQuery}
}
Expand All @@ -110,9 +123,9 @@ export class QueryBuilder {
getByIrisQuery(iris: Iri[]) {
const query = CONSTRUCT`
${this.getResourceSignature()}
${this.getShape("iri", true, false)}
${this.getShape("iri", true, false, false)}
`.WHERE`
${this.getShape("iri", true, true)}
${this.getShape("iri", true, true, false)}
VALUES ?iri {
${iris.map(namedNode)}
}
Expand Down
3 changes: 2 additions & 1 deletion library/resource/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
findByIris(iris: Iri[]) {
const q = this.queryBuilder.getByIrisQuery(iris);
console.log(q);
return quadsQuery(q, this.context).pipe(
return this.$trigger.pipe(
switchMap(() => quadsQuery(q, this.context)),
map((graph) => {
return this.decode(graph);
})
Expand Down
14 changes: 14 additions & 0 deletions specs/src/resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,18 @@ describe("Resource", () => {
name: "Christopher Nolan",
});
});

test("Support for custom types", async () => {
const result = await run(
movies.insert({
$id: x.KillBill,
$type: [x.TarantinoMovie],
name: "Kill Bill",
director: { $id: x.QuentinTarantino },
}),
movies.findByIri(x.KillBill)
);

expect(result?.$type).toEqual([x.Movie, x.TarantinoMovie]);
});
});

0 comments on commit decf38c

Please sign in to comment.