@shaclmate/compiler 2.0.23 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +5 -5
  2. package/dist/ShapesGraphToAstTransformer.js +5 -5
  3. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.d.ts +2 -0
  4. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.js +2 -0
  5. package/dist/_ShapesGraphToAstTransformer/ShapeStack.d.ts +15 -0
  6. package/dist/_ShapesGraphToAstTransformer/ShapeStack.js +51 -0
  7. package/dist/_ShapesGraphToAstTransformer/index.d.ts +5 -5
  8. package/dist/_ShapesGraphToAstTransformer/index.js +5 -5
  9. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +77 -29
  10. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.d.ts +10 -0
  11. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.js +185 -0
  12. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.d.ts +10 -0
  13. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.js +32 -0
  14. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.d.ts +10 -0
  15. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.js +46 -0
  16. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.d.ts +12 -0
  17. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.js +24 -0
  18. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.d.ts +13 -0
  19. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.js +14 -0
  20. package/dist/ast/OptionType.d.ts +0 -2
  21. package/dist/ast/SetType.d.ts +1 -6
  22. package/dist/ast/index.d.ts +1 -0
  23. package/dist/ast/index.js +1 -0
  24. package/dist/generators/json/AstJsonGenerator.js +7 -1
  25. package/dist/generators/ts/BooleanType.d.ts +2 -1
  26. package/dist/generators/ts/BooleanType.js +9 -4
  27. package/dist/generators/ts/DateTimeType.d.ts +2 -1
  28. package/dist/generators/ts/DateTimeType.js +9 -4
  29. package/dist/generators/ts/IdentifierType.d.ts +1 -1
  30. package/dist/generators/ts/IdentifierType.js +20 -14
  31. package/dist/generators/ts/Import.d.ts +2 -1
  32. package/dist/generators/ts/Import.js +29 -24
  33. package/dist/generators/ts/ListType.js +16 -5
  34. package/dist/generators/ts/LiteralType.d.ts +5 -2
  35. package/dist/generators/ts/LiteralType.js +81 -15
  36. package/dist/generators/ts/NumberType.d.ts +2 -1
  37. package/dist/generators/ts/NumberType.js +9 -4
  38. package/dist/generators/ts/ObjectType.d.ts +3 -2
  39. package/dist/generators/ts/ObjectType.js +16 -6
  40. package/dist/generators/ts/ObjectUnionType.d.ts +2 -0
  41. package/dist/generators/ts/ObjectUnionType.js +21 -9
  42. package/dist/generators/ts/OptionType.d.ts +1 -1
  43. package/dist/generators/ts/OptionType.js +6 -4
  44. package/dist/generators/ts/PrimitiveType.d.ts +1 -1
  45. package/dist/generators/ts/PrimitiveType.js +6 -3
  46. package/dist/generators/ts/SetType.d.ts +4 -4
  47. package/dist/generators/ts/SetType.js +15 -12
  48. package/dist/generators/ts/SnippetDeclarations.d.ts +3 -2
  49. package/dist/generators/ts/SnippetDeclarations.js +41 -3
  50. package/dist/generators/ts/StringType.d.ts +2 -1
  51. package/dist/generators/ts/StringType.js +14 -7
  52. package/dist/generators/ts/TermType.d.ts +18 -19
  53. package/dist/generators/ts/TermType.js +55 -51
  54. package/dist/generators/ts/TsGenerator.js +24 -12
  55. package/dist/generators/ts/Type.d.ts +12 -3
  56. package/dist/generators/ts/Type.js +1 -0
  57. package/dist/generators/ts/TypeFactory.d.ts +8 -0
  58. package/dist/generators/ts/TypeFactory.js +114 -23
  59. package/dist/generators/ts/UnionType.js +1 -1
  60. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +3 -2
  61. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +1 -1
  62. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +15 -1
  63. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +6 -6
  64. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +59 -29
  65. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +14 -1
  66. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +89 -11
  67. package/dist/generators/ts/_ObjectType/Property.d.ts +10 -4
  68. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +1 -1
  69. package/dist/generators/ts/_ObjectType/ShaclProperty.js +14 -2
  70. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +1 -1
  71. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +1 -1
  72. package/dist/generators/ts/_ObjectType/classDeclaration.js +16 -12
  73. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +22 -10
  74. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +3 -1
  75. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +14 -2
  76. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.js +1 -1
  77. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +95 -84
  78. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +3 -3
  79. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +1 -1
  80. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +35 -15
  81. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +19 -12
  82. package/dist/generators/ts/forwardingObjectSetClassDeclaration.d.ts +8 -0
  83. package/dist/generators/ts/forwardingObjectSetClassDeclaration.js +29 -0
  84. package/dist/generators/ts/objectSetDeclarations.js +2 -0
  85. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +26 -19
  86. package/dist/generators/ts/rdfjsTermExpression.js +3 -3
  87. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +6 -6
  88. package/dist/input/PropertyPath.d.ts +2 -3
  89. package/dist/input/PropertyPath.js +6 -8
  90. package/dist/input/ShapesGraph.js +4 -8
  91. package/dist/input/generated.d.ts +96 -54
  92. package/dist/input/generated.js +1350 -744
  93. package/package.json +5 -8
  94. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +0 -12
  95. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +0 -193
  96. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +0 -12
  97. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +0 -29
  98. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +0 -12
  99. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +0 -43
  100. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +0 -14
  101. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +0 -21
  102. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +0 -15
  103. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +0 -60
@@ -1,29 +1,29 @@
1
1
  import { rdf } from "@tpluscode/rdf-ns-builders";
2
2
  import { Maybe } from "purify-ts";
3
3
  import { invariant } from "ts-invariant";
4
+ import { logger } from "../../../logger.js";
4
5
  import { Import } from "../Import.js";
5
6
  import { SnippetDeclarations } from "../SnippetDeclarations.js";
6
7
  import { rdfjsTermExpression } from "../rdfjsTermExpression.js";
7
8
  import { syntheticNamePrefix } from "../syntheticNamePrefix.js";
8
9
  import { Property } from "./Property.js";
9
10
  export class IdentifierProperty extends Property {
10
- classGetAccessorScope;
11
- classPropertyDeclarationVisibility;
11
+ getAccessorScope;
12
12
  identifierMintingStrategy;
13
13
  identifierPrefixPropertyName;
14
14
  override;
15
+ propertyDeclarationVisibility;
15
16
  typeAlias;
16
17
  abstract;
17
- equalsFunction = `${syntheticNamePrefix}booleanEquals`;
18
+ equalsFunction = Maybe.of(`${syntheticNamePrefix}booleanEquals`);
18
19
  mutable = false;
19
20
  recursive = false;
20
- constructor({ abstract, classGetAccessorScope, classPropertyDeclarationVisibility, identifierMintingStrategy, identifierPrefixPropertyName, override, typeAlias, ...superParameters }) {
21
+ constructor({ abstract, getAccessorScope, identifierMintingStrategy, identifierPrefixPropertyName, override, propertyDeclarationVisibility, typeAlias, ...superParameters }) {
21
22
  super(superParameters);
22
23
  invariant(this.visibility === "public");
23
24
  this.abstract = abstract;
24
- this.classGetAccessorScope = classGetAccessorScope;
25
- this.classPropertyDeclarationVisibility =
26
- classPropertyDeclarationVisibility;
25
+ this.getAccessorScope = getAccessorScope;
26
+ this.propertyDeclarationVisibility = propertyDeclarationVisibility;
27
27
  this.identifierMintingStrategy = identifierMintingStrategy;
28
28
  this.identifierPrefixPropertyName = identifierPrefixPropertyName;
29
29
  this.override = override;
@@ -39,9 +39,21 @@ export class IdentifierProperty extends Property {
39
39
  typeNames.add(conversion.sourceTypeName);
40
40
  }
41
41
  }
42
+ let hasQuestionToken;
43
+ switch (this.objectType.declarationType) {
44
+ case "class":
45
+ hasQuestionToken = this.identifierMintingStrategy.isJust();
46
+ break;
47
+ case "interface": {
48
+ const identifierMintingStrategy = this.identifierMintingStrategy.extract();
49
+ hasQuestionToken =
50
+ typeof identifierMintingStrategy !== "undefined" &&
51
+ identifierMintingStrategy !== "sha256";
52
+ break;
53
+ }
54
+ }
42
55
  return Maybe.of({
43
- hasQuestionToken: this.objectType.declarationType === "class" &&
44
- this.identifierMintingStrategy.isJust(),
56
+ hasQuestionToken,
45
57
  isReadonly: true,
46
58
  name: this.name,
47
59
  type: [...typeNames].sort().join(" | "),
@@ -49,26 +61,23 @@ export class IdentifierProperty extends Property {
49
61
  }
50
62
  get declarationImports() {
51
63
  const imports = this.type.useImports().concat();
52
- if (this.objectType.features.has("hash") &&
53
- this.objectType.declarationType === "class") {
54
- this.identifierMintingStrategy.ifJust((identifierMintingStrategy) => {
55
- switch (identifierMintingStrategy) {
56
- case "sha256":
57
- imports.push(Import.SHA256);
58
- break;
59
- case "uuidv4":
60
- imports.push(Import.UUID);
61
- break;
62
- }
63
- });
64
- }
64
+ this.identifierMintingStrategy.ifJust((identifierMintingStrategy) => {
65
+ switch (identifierMintingStrategy) {
66
+ case "sha256":
67
+ imports.push(Import.SHA256);
68
+ break;
69
+ case "uuidv4":
70
+ imports.push(Import.UUID);
71
+ break;
72
+ }
73
+ });
65
74
  return imports;
66
75
  }
67
76
  get getAccessorDeclaration() {
68
- if (this.classGetAccessorScope.isNothing()) {
77
+ if (this.getAccessorScope.isNothing()) {
69
78
  return Maybe.empty();
70
79
  }
71
- invariant(this.classGetAccessorScope.unsafeCoerce() === "public");
80
+ invariant(this.getAccessorScope.unsafeCoerce() === "public");
72
81
  if (this.identifierMintingStrategy.isJust()) {
73
82
  let memoizeMintedIdentifier;
74
83
  let mintIdentifier;
@@ -109,6 +118,8 @@ export class IdentifierProperty extends Property {
109
118
  get graphqlField() {
110
119
  invariant(this.name.startsWith(syntheticNamePrefix));
111
120
  return Maybe.of({
121
+ args: Maybe.empty(),
122
+ description: Maybe.empty(),
112
123
  name: `_${this.name.substring(syntheticNamePrefix.length)}`,
113
124
  resolve: `(source) => ${this.typeAlias}.toString(source.${this.name})`,
114
125
  type: this.type.graphqlName.toString(),
@@ -153,9 +164,28 @@ export class IdentifierProperty extends Property {
153
164
  invariant(conversion.sourceTypeName !== "undefined");
154
165
  conversionBranches.push(`if (${conversion.sourceTypeCheckExpression(variables.parameter)}) { ${lhs} = ${conversion.conversionExpression(variables.parameter)}; }`);
155
166
  }
156
- if (lhs.startsWith("this._")) {
157
- conversionBranches.push(`if (typeof ${variables.parameter} === "undefined") { }`);
158
- }
167
+ this.identifierMintingStrategy.ifJust((identifierMintingStrategy) => {
168
+ switch (this.objectType.declarationType) {
169
+ case "class":
170
+ conversionBranches.push(`if (typeof ${variables.parameter} === "undefined") { }`);
171
+ break;
172
+ case "interface": {
173
+ let mintIdentifier;
174
+ switch (identifierMintingStrategy) {
175
+ case "blankNode":
176
+ mintIdentifier = "dataFactory.blankNode()";
177
+ break;
178
+ case "sha256":
179
+ logger.warn("minting %s identifiers with %s is unsupported", this.objectType.declarationType, identifierMintingStrategy);
180
+ return;
181
+ case "uuidv4":
182
+ mintIdentifier = `dataFactory.namedNode(\`\${${variables.parameters}.${this.identifierPrefixPropertyName} ?? "urn:shaclmate:${this.objectType.discriminatorValue}:"}\${uuid.v4()}\`)`;
183
+ break;
184
+ }
185
+ conversionBranches.push(`if (typeof ${variables.parameter} === "undefined") { ${lhs} = ${mintIdentifier}; }`);
186
+ }
187
+ }
188
+ });
159
189
  // We shouldn't need this else, since the parameter now has the never type, but have to add it to appease the TypeScript compiler
160
190
  conversionBranches.push(`{ ${lhs} = (${variables.parameter}) satisfies never;\n }`);
161
191
  statements.push(conversionBranches.join(" else "));
@@ -217,7 +247,7 @@ export class IdentifierProperty extends Property {
217
247
  });
218
248
  }
219
249
  // See note in TypeFactory re: the logic of whether to declare the identifier in the class or not.
220
- if (!this.classPropertyDeclarationVisibility.isJust()) {
250
+ if (!this.propertyDeclarationVisibility.isJust()) {
221
251
  return Maybe.empty();
222
252
  }
223
253
  if (this.identifierMintingStrategy.isJust()) {
@@ -225,7 +255,7 @@ export class IdentifierProperty extends Property {
225
255
  return Maybe.of({
226
256
  hasQuestionToken: true,
227
257
  name: `_${this.name}`,
228
- scope: this.classPropertyDeclarationVisibility
258
+ scope: this.propertyDeclarationVisibility
229
259
  .map(Property.visibilityToScope)
230
260
  .unsafeCoerce(),
231
261
  type: `${this.typeAlias}`,
@@ -18,6 +18,9 @@ export declare namespace LazyShaclProperty {
18
18
  readonly discriminatorProperty: _Type["discriminatorProperty"];
19
19
  readonly mutable = false;
20
20
  readonly typeof = "object";
21
+ abstract readonly graphqlArgs: Maybe<Record<string, {
22
+ type: string;
23
+ }>>;
21
24
  protected readonly resolvedType: ResolvedTypeT;
22
25
  protected readonly runtimeClass: {
23
26
  readonly name: string;
@@ -34,7 +37,6 @@ export declare namespace LazyShaclProperty {
34
37
  get conversions(): readonly _Type.Conversion[];
35
38
  get equalsFunction(): string;
36
39
  get graphqlName(): _Type.GraphqlName;
37
- graphqlResolveExpression({ variables, }: Parameters<_Type["graphqlResolveExpression"]>[0]): string;
38
40
  hashStatements({ depth, variables, }: Parameters<_Type["hashStatements"]>[0]): readonly string[];
39
41
  get jsonName(): _Type.JsonName;
40
42
  jsonUiSchemaElement(parameters: Parameters<_Type["jsonUiSchemaElement"]>[0]): Maybe<string>;
@@ -61,9 +63,20 @@ export declare namespace LazyShaclProperty {
61
63
  get conversions(): readonly _Type.Conversion[];
62
64
  fromJsonExpression(parameters: Parameters<_Type["fromJsonExpression"]>[0]): string;
63
65
  fromRdfExpression(parameters: Parameters<_Type["fromRdfExpression"]>[0]): string;
66
+ get graphqlArgs(): Maybe<{
67
+ limit: {
68
+ type: string;
69
+ };
70
+ offset: {
71
+ type: string;
72
+ };
73
+ }>;
74
+ graphqlResolveExpression({ variables, }: Parameters<_Type["graphqlResolveExpression"]>[0]): string;
64
75
  }
65
76
  abstract class SingleObjectType<ResolvedTypeT extends Exclude<Type.ResolvedTypeConstraint, SetType<ObjectType | ObjectUnionType>>, StubTypeT extends Exclude<Type.StubTypeConstraint, SetType<ObjectType | ObjectUnionType>>> extends Type<ResolvedTypeT, StubTypeT> {
66
77
  fromJsonExpression(parameters: Parameters<_Type["fromJsonExpression"]>[0]): string;
78
+ get graphqlArgs(): Type<ResolvedTypeT, ObjectUnionType>["graphqlArgs"];
79
+ graphqlResolveExpression({ variables, }: Parameters<_Type["graphqlResolveExpression"]>[0]): string;
67
80
  }
68
81
  export class OptionalObjectType<ResolvedTypeT extends OptionType<ObjectType | ObjectUnionType>, StubTypeT extends OptionType<ObjectType | ObjectUnionType>> extends SingleObjectType<ResolvedTypeT, StubTypeT> {
69
82
  constructor({ resolvedType, stubType, }: {
@@ -7,23 +7,39 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { Maybe } from "purify-ts";
8
8
  import "ts-morph";
9
9
  import { Memoize } from "typescript-memoize";
10
+ import { invariant } from "ts-invariant";
10
11
  import { Import } from "../Import.js";
11
12
  import { SnippetDeclarations } from "../SnippetDeclarations.js";
12
13
  import { Type as _Type } from "../Type.js";
13
14
  import { syntheticNamePrefix } from "../syntheticNamePrefix.js";
14
15
  import { ShaclProperty } from "./ShaclProperty.js";
16
+ function stubObjectUnionTypeToResolvedObjectUnionTypeSwitchStatement({ resolvedObjectUnionType, stubObjectUnionType, variables, }) {
17
+ invariant(resolvedObjectUnionType.memberTypes.length ===
18
+ stubObjectUnionType.memberTypes.length);
19
+ const caseBlocks = resolvedObjectUnionType.memberTypes.map((resolvedObjectType, objectTypeI) => {
20
+ return `${resolvedObjectType.discriminatorPropertyValues.map((discriminatorPropertyValue) => `case "${discriminatorPropertyValue}":`).join("\n")} return ${stubObjectUnionType.memberTypes[objectTypeI].newExpression({ parameters: variables.value })};`;
21
+ });
22
+ caseBlocks.push(`default: ${variables.value} satisfies never; throw new Error("unrecognized type");`);
23
+ return `switch (${variables.value}.${resolvedObjectUnionType.discriminatorProperty.unsafeCoerce().name}) { ${caseBlocks.join("\n")} }`;
24
+ }
15
25
  export class LazyShaclProperty extends ShaclProperty {
16
26
  mutable = false;
17
27
  recursive = false;
18
28
  get graphqlField() {
29
+ const args = this.type.graphqlArgs;
30
+ const argsVariable = args.isJust() ? "args" : "_args";
19
31
  return Maybe.of({
20
- description: this.comment.map(JSON.stringify).extract(),
32
+ args,
33
+ description: this.comment.map(JSON.stringify),
21
34
  name: this.name,
22
- resolve: `async (source) => ${this.type.graphqlResolveExpression({ variables: { value: `source.${this.name}` } })}`,
35
+ resolve: `async (source, ${argsVariable}) => ${this.type.graphqlResolveExpression({ variables: { args: argsVariable, value: `source.${this.name}` } })}`,
23
36
  type: this.type.graphqlName.toString(),
24
37
  });
25
38
  }
26
39
  }
40
+ __decorate([
41
+ Memoize()
42
+ ], LazyShaclProperty.prototype, "graphqlField", null);
27
43
  (function (LazyShaclProperty) {
28
44
  class Type extends _Type {
29
45
  discriminatorProperty = Maybe.empty();
@@ -53,9 +69,6 @@ export class LazyShaclProperty extends ShaclProperty {
53
69
  get graphqlName() {
54
70
  return this.resolvedType.graphqlName;
55
71
  }
56
- graphqlResolveExpression({ variables, }) {
57
- return `(await ${variables.value}.resolve()).unsafeCoerce()`;
58
- }
59
72
  hashStatements({ depth, variables, }) {
60
73
  return this.stubType.hashStatements({
61
74
  depth: depth + 1,
@@ -129,7 +142,17 @@ export class LazyShaclProperty extends ShaclProperty {
129
142
  const conversions = super.conversions.concat();
130
143
  if (this.stubType.itemType.kind === "ObjectType") {
131
144
  conversions.push({
132
- conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}.map(parameters => ${this.stubType.itemType.newExpression({ parameters: "parameters" })}), resolver: async () => purify.Either.of(${value} as readonly ${this.resolvedType.itemType.name}[]) })`,
145
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}.map(object => ${this.stubType.itemType.newExpression({ parameters: "object" })}), resolver: async () => purify.Either.of(${value} as readonly ${this.resolvedType.itemType.name}[]) })`,
146
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
147
+ sourceTypeName: `readonly ${this.resolvedType.itemType.name}[]`,
148
+ });
149
+ }
150
+ else if (this.resolvedType.itemType.kind === "ObjectUnionType" &&
151
+ this.stubType.itemType.kind === "ObjectUnionType" &&
152
+ this.resolvedType.itemType.memberTypes.length ===
153
+ this.stubType.itemType.memberTypes.length) {
154
+ conversions.push({
155
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}.map(object => { ${stubObjectUnionTypeToResolvedObjectUnionTypeSwitchStatement({ resolvedObjectUnionType: this.resolvedType.itemType, stubObjectUnionType: this.stubType.itemType, variables: { value: "object" } })} }), resolver: async () => purify.Either.of(${value} as readonly ${this.resolvedType.itemType.name}[]) })`,
133
156
  sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
134
157
  sourceTypeName: `readonly ${this.resolvedType.itemType.name}[]`,
135
158
  });
@@ -146,17 +169,39 @@ export class LazyShaclProperty extends ShaclProperty {
146
169
  }
147
170
  fromRdfExpression(parameters) {
148
171
  const { variables } = parameters;
149
- return `${this.stubType.fromRdfExpression(parameters)}.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifiers) => ${variables.objectSet}.${this.resolvedType.itemType.objectSetMethodNames.objects}({ where: { identifiers, type: "identifiers" }}) }))`;
172
+ return `${this.stubType.fromRdfExpression(parameters)}.map(values => values.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifiers) => ${variables.objectSet}.${this.resolvedType.itemType.objectSetMethodNames.objects}({ where: { identifiers, type: "identifiers" }}) })))`;
173
+ }
174
+ get graphqlArgs() {
175
+ return Maybe.of({
176
+ limit: {
177
+ type: "graphql.GraphQLInt",
178
+ },
179
+ offset: {
180
+ type: "graphql.GraphQLInt",
181
+ },
182
+ });
183
+ }
184
+ graphqlResolveExpression({ variables, }) {
185
+ return `(await ${variables.value}.resolve({ limit: ${variables.args}.limit, offset: ${variables.args}.offset })).unsafeCoerce()`;
150
186
  }
151
187
  }
152
188
  __decorate([
153
189
  Memoize()
154
190
  ], ObjectSetType.prototype, "conversions", null);
191
+ __decorate([
192
+ Memoize()
193
+ ], ObjectSetType.prototype, "graphqlArgs", null);
155
194
  LazyShaclProperty.ObjectSetType = ObjectSetType;
156
195
  class SingleObjectType extends Type {
157
196
  fromJsonExpression(parameters) {
158
197
  return `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${this.stubType.fromJsonExpression(parameters)}, resolver: (identifier) => Promise.resolve(purify.Left(new Error(\`unable to resolve identifier \${rdfjsResource.Resource.Identifier.toString(identifier)} deserialized from JSON\`))) })`;
159
198
  }
199
+ get graphqlArgs() {
200
+ return Maybe.empty();
201
+ }
202
+ graphqlResolveExpression({ variables, }) {
203
+ return `(await ${variables.value}.resolve()).unsafeCoerce()`;
204
+ }
160
205
  }
161
206
  class OptionalObjectType extends SingleObjectType {
162
207
  constructor({ resolvedType, stubType, }) {
@@ -175,11 +220,30 @@ export class LazyShaclProperty extends ShaclProperty {
175
220
  const conversions = super.conversions.concat();
176
221
  if (this.stubType.itemType.kind === "ObjectType") {
177
222
  conversions.push({
178
- conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}.map(parameters => ${this.stubType.itemType.newExpression({ parameters: "parameters" })}), resolver: async () => purify.Either.of((${value} as purify.Maybe<${this.resolvedType.itemType.name}>).unsafeCoerce()) })`,
223
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}.map(object => ${this.stubType.itemType.newExpression({ parameters: "object" })}), resolver: async () => purify.Either.of((${value} as purify.Maybe<${this.resolvedType.itemType.name}>).unsafeCoerce()) })`,
179
224
  sourceTypeCheckExpression: (value) => `purify.Maybe.isMaybe(${value})`,
180
225
  sourceTypeName: `purify.Maybe<${this.resolvedType.itemType.name}>`,
181
226
  }, {
182
227
  conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: purify.Maybe.of(${this.stubType.itemType.newExpression({ parameters: value })}), resolver: async () => purify.Either.of(${value} as ${this.resolvedType.itemType.name}) })`,
228
+ // Don't check instanceof value since the ObjectUnionType may be an interface
229
+ // Rely on the fact that this will be the last type check on an object
230
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
231
+ sourceTypeName: this.resolvedType.itemType.name,
232
+ });
233
+ }
234
+ else if (this.resolvedType.itemType.kind === "ObjectUnionType" &&
235
+ this.stubType.itemType.kind === "ObjectUnionType" &&
236
+ this.resolvedType.itemType.memberTypes.length ===
237
+ this.stubType.itemType.memberTypes.length) {
238
+ const maybeMap = `.map(object => { ${stubObjectUnionTypeToResolvedObjectUnionTypeSwitchStatement({ resolvedObjectUnionType: this.resolvedType.itemType, stubObjectUnionType: this.stubType.itemType, variables: { value: "object" } })} })`;
239
+ conversions.push({
240
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${value}${maybeMap}, resolver: async () => purify.Either.of((${value} as purify.Maybe<${this.resolvedType.itemType.name}>).unsafeCoerce()) })`,
241
+ sourceTypeCheckExpression: (value) => `purify.Maybe.isMaybe(${value})`,
242
+ sourceTypeName: `purify.Maybe<${this.resolvedType.itemType.name}>`,
243
+ }, {
244
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: purify.Maybe.of(${value})${maybeMap}, resolver: async () => purify.Either.of(${value} as ${this.resolvedType.itemType.name}) })`,
245
+ // Don't check instanceof value since the ObjectUnionType may be an interface
246
+ // Rely on the fact that this will be the last type check on an object
183
247
  sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
184
248
  sourceTypeName: this.resolvedType.itemType.name,
185
249
  });
@@ -193,7 +257,7 @@ export class LazyShaclProperty extends ShaclProperty {
193
257
  }
194
258
  fromRdfExpression(parameters) {
195
259
  const { variables } = parameters;
196
- return `${this.stubType.fromRdfExpression(parameters)}.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifier) => ${variables.objectSet}.${this.resolvedType.itemType.objectSetMethodNames.object}(identifier) }))`;
260
+ return `${this.stubType.fromRdfExpression(parameters)}.map(values => values.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifier) => ${variables.objectSet}.${this.resolvedType.itemType.objectSetMethodNames.object}(identifier) })))`;
197
261
  }
198
262
  graphqlResolveExpression(parameters) {
199
263
  return `${super.graphqlResolveExpression(parameters)}.extractNullable()`;
@@ -221,7 +285,21 @@ export class LazyShaclProperty extends ShaclProperty {
221
285
  if (this.stubType.kind === "ObjectType") {
222
286
  conversions.push({
223
287
  conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ${this.stubType.newExpression({ parameters: value })}, resolver: async () => purify.Either.of(${value} as ${this.resolvedType.name}) })`,
224
- sourceTypeCheckExpression: (value) => `typeof ${value} === "object" && ${value} instanceof ${this.resolvedType.name}`,
288
+ // Don't check instanceof value since the ObjectType may be an interface
289
+ // Rely on the fact that this will be the last type check on an object
290
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
291
+ sourceTypeName: this.resolvedType.name,
292
+ });
293
+ }
294
+ else if (this.resolvedType.kind === "ObjectUnionType" &&
295
+ this.stubType.kind === "ObjectUnionType" &&
296
+ this.resolvedType.memberTypes.length ===
297
+ this.stubType.memberTypes.length) {
298
+ conversions.push({
299
+ conversionExpression: (value) => `new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}: ((object: ${this.resolvedType.name}) => { ${stubObjectUnionTypeToResolvedObjectUnionTypeSwitchStatement({ resolvedObjectUnionType: this.resolvedType, stubObjectUnionType: this.stubType, variables: { value: "object" } })} })(${value}), resolver: async () => purify.Either.of(${value} as ${this.resolvedType.name}) })`,
300
+ // Don't check instanceof value since the ObjectUnionType may be an interface
301
+ // Rely on the fact that this will be the last type check on an object
302
+ sourceTypeCheckExpression: (value) => `typeof ${value} === "object"`,
225
303
  sourceTypeName: this.resolvedType.name,
226
304
  });
227
305
  }
@@ -229,7 +307,7 @@ export class LazyShaclProperty extends ShaclProperty {
229
307
  }
230
308
  fromRdfExpression(parameters) {
231
309
  const { variables } = parameters;
232
- return `${this.stubType.fromRdfExpression(parameters)}.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifier) => ${variables.objectSet}.${this.resolvedType.objectSetMethodNames.object}(identifier) }))`;
310
+ return `${this.stubType.fromRdfExpression(parameters)}.map(values => values.map(${this.runtimeClass.stubPropertyName} => new ${this.runtimeClass.name}({ ${this.runtimeClass.stubPropertyName}, resolver: (identifier) => ${variables.objectSet}.${this.resolvedType.objectSetMethodNames.object}(identifier) })))`;
233
311
  }
234
312
  }
235
313
  LazyShaclProperty.RequiredObjectType = RequiredObjectType;
@@ -12,7 +12,7 @@ export declare abstract class Property<TypeT extends Pick<Type, "mutable" | "nam
12
12
  /**
13
13
  * Function declaration that takes two values of the property and compares them, returning an $EqualsResult.
14
14
  */
15
- abstract readonly equalsFunction: string;
15
+ abstract readonly equalsFunction: Maybe<string>;
16
16
  /**
17
17
  * Optional get accessor to include in a class declaration of the object type.
18
18
  */
@@ -21,8 +21,12 @@ export declare abstract class Property<TypeT extends Pick<Type, "mutable" | "nam
21
21
  * GraphQL.js field definition.
22
22
  */
23
23
  abstract readonly graphqlField: Maybe<{
24
+ args: Maybe<Record<string, {
25
+ type: string;
26
+ }>>;
27
+ description: Maybe<string>;
24
28
  name: string;
25
- description?: string;
29
+ resolve: string;
26
30
  type: string;
27
31
  }>;
28
32
  /**
@@ -75,6 +79,7 @@ export declare abstract class Property<TypeT extends Pick<Type, "mutable" | "nam
75
79
  abstract constructorStatements(parameters: {
76
80
  variables: {
77
81
  parameter: string;
82
+ parameters: string;
78
83
  };
79
84
  }): readonly string[];
80
85
  /**
@@ -86,13 +91,13 @@ export declare abstract class Property<TypeT extends Pick<Type, "mutable" | "nam
86
91
  };
87
92
  }): readonly string[];
88
93
  /**
89
- * Statements to deserialize RDF for this property to a typed value of the property.
94
+ * Statements to deserialize this property on the given rdfjsResource.Resource to a typed value of the property.
90
95
  */
91
96
  abstract fromRdfStatements(parameters: {
92
97
  variables: {
93
98
  context: string;
94
- languageIn: string;
95
99
  objectSet: string;
100
+ preferredLanguages: string;
96
101
  resource: string;
97
102
  };
98
103
  }): readonly string[];
@@ -142,6 +147,7 @@ export declare abstract class Property<TypeT extends Pick<Type, "mutable" | "nam
142
147
  */
143
148
  abstract sparqlWherePatterns(parameters: {
144
149
  variables: {
150
+ preferredLanguages: string;
145
151
  subject: string;
146
152
  variablePrefix: string;
147
153
  };
@@ -15,7 +15,7 @@ export declare abstract class ShaclProperty<TypeT extends Type> extends Property
15
15
  label: Maybe<string>;
16
16
  path: rdfjs.NamedNode;
17
17
  } & ConstructorParameters<typeof Property<TypeT>>[0]);
18
- get equalsFunction(): string;
18
+ get equalsFunction(): Maybe<string>;
19
19
  get constructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
20
20
  constructorStatements({ variables, }: Parameters<Property<TypeT>["constructorStatements"]>[0]): readonly string[];
21
21
  fromJsonStatements({ variables, }: Parameters<Property<TypeT>["fromJsonStatements"]>[0]): readonly string[];
@@ -23,7 +23,7 @@ export class ShaclProperty extends Property {
23
23
  this.path = path;
24
24
  }
25
25
  get equalsFunction() {
26
- return this.type.equalsFunction;
26
+ return Maybe.of(this.type.equalsFunction);
27
27
  }
28
28
  get constructorParametersPropertySignature() {
29
29
  let hasQuestionToken = false;
@@ -137,8 +137,16 @@ export class ShaclProperty extends Property {
137
137
  // Assume the property has the correct range and ignore the object's RDF type.
138
138
  // This also accommodates the case where the object of a property is a dangling identifier that's not the
139
139
  // subject of any statements.
140
+ const typeFromRdfExpression = this.type.fromRdfExpression({
141
+ variables: {
142
+ ...variables,
143
+ ignoreRdfType: true,
144
+ predicate: this.predicate,
145
+ resourceValues: `purify.Either.of<Error, rdfjsResource.Resource.Values<rdfjsResource.Resource.Value>>(${variables.resource}.values(${syntheticNamePrefix}properties.${this.name}["identifier"], { unique: true }))`,
146
+ },
147
+ });
140
148
  return [
141
- `const _${this.name}Either: purify.Either<Error, ${this.type.name}> = ${this.type.fromRdfExpression({ variables: { ...variables, ignoreRdfType: true, predicate: this.predicate, resourceValues: `${variables.resource}.values(${syntheticNamePrefix}properties.${this.name}["identifier"], { unique: true })` } })};`,
149
+ `const _${this.name}Either: purify.Either<Error, ${this.type.name}> = ${typeFromRdfExpression}.chain(values => values.head());`,
142
150
  `if (_${this.name}Either.isLeft()) { return _${this.name}Either; }`,
143
151
  `const ${this.name} = _${this.name}Either.unsafeCoerce();`,
144
152
  ];
@@ -164,6 +172,7 @@ export class ShaclProperty extends Property {
164
172
  variables: {
165
173
  object: `dataFactory.variable!(${objectString})`,
166
174
  predicate: this.predicate,
175
+ preferredLanguages: variables.preferredLanguages,
167
176
  subject: variables.subject,
168
177
  variablePrefix: objectString,
169
178
  },
@@ -190,6 +199,9 @@ export class ShaclProperty extends Property {
190
199
  return `${this.objectType.staticModuleName}.${syntheticNamePrefix}properties.${this.name}["identifier"]`;
191
200
  }
192
201
  }
202
+ __decorate([
203
+ Memoize()
204
+ ], ShaclProperty.prototype, "equalsFunction", null);
193
205
  __decorate([
194
206
  Memoize()
195
207
  ], ShaclProperty.prototype, "constructorParametersPropertySignature", null);
@@ -7,7 +7,7 @@ export declare class TypeDiscriminatorProperty extends Property<TypeDiscriminato
7
7
  private readonly override;
8
8
  readonly constructorParametersPropertySignature: Maybe<OptionalKind<PropertySignatureStructure>>;
9
9
  readonly declarationImports: readonly Import[];
10
- readonly equalsFunction = "$strictEquals";
10
+ readonly equalsFunction: Maybe<string>;
11
11
  readonly getAccessorDeclaration: Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
12
12
  readonly graphqlField: Property<TypeDiscriminatorProperty.Type>["graphqlField"];
13
13
  readonly initializer: string;
@@ -15,7 +15,7 @@ export class TypeDiscriminatorProperty extends Property {
15
15
  override;
16
16
  constructorParametersPropertySignature = Maybe.empty();
17
17
  declarationImports = [];
18
- equalsFunction = `${syntheticNamePrefix}strictEquals`;
18
+ equalsFunction = Maybe.of(`${syntheticNamePrefix}strictEquals`);
19
19
  getAccessorDeclaration = Maybe.empty();
20
20
  graphqlField = Maybe.empty();
21
21
  initializer;
@@ -7,22 +7,19 @@ import { hashFunctionOrMethodDeclarations } from "./hashFunctionOrMethodDeclarat
7
7
  import { toJsonFunctionOrMethodDeclaration } from "./toJsonFunctionOrMethodDeclaration.js";
8
8
  import { toRdfFunctionOrMethodDeclaration } from "./toRdfFunctionOrMethodDeclaration.js";
9
9
  function constructorDeclaration() {
10
- const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature
11
- .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
12
- .toList());
13
- let parametersType;
10
+ const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature.toList());
11
+ const parametersType = [];
14
12
  if (parametersPropertySignatures.length > 0) {
15
- parametersType = `{ ${parametersPropertySignatures.join(", ")} }`;
16
- }
17
- else {
18
- parametersType = "";
13
+ parametersType.push(`{ ${parametersPropertySignatures
14
+ .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
15
+ .join(", ")} }`);
19
16
  }
20
17
  if (this.parentObjectTypes.length > 0) {
21
18
  // Pass up parameters
22
- parametersType = `${parametersType}${parametersType.length > 0 ? " & " : ""}ConstructorParameters<typeof ${this.parentObjectTypes[0].name}>[0]`;
19
+ parametersType.push(`ConstructorParameters<typeof ${this.parentObjectTypes[0].name}>[0]`);
23
20
  }
24
21
  if (parametersType.length === 0) {
25
- parametersType = "object";
22
+ parametersType.push("object");
26
23
  }
27
24
  const statements = [];
28
25
  if (this.parentObjectTypes.length > 0) {
@@ -30,8 +27,14 @@ function constructorDeclaration() {
30
27
  // of trying to sense whether we need to or not.
31
28
  statements.push("super(parameters);");
32
29
  }
30
+ const parametersHasQuestionToken = this.parentObjectTypes.length === 0 &&
31
+ parametersPropertySignatures.every((propertySignature) => !!propertySignature.hasQuestionToken);
32
+ const parametersVariable = `parameters${parametersHasQuestionToken ? "?" : ""}`;
33
33
  const propertyStatements = this.properties.flatMap((property) => property.constructorStatements({
34
- variables: { parameter: `parameters.${property.name}` },
34
+ variables: {
35
+ parameter: `${parametersVariable}.${property.name}`,
36
+ parameters: parametersVariable,
37
+ },
35
38
  }));
36
39
  statements.push(...propertyStatements);
37
40
  return {
@@ -40,8 +43,9 @@ function constructorDeclaration() {
40
43
  : undefined,
41
44
  parameters: [
42
45
  {
46
+ hasQuestionToken: parametersHasQuestionToken,
43
47
  name: statements.length > 0 ? "parameters" : "_parameters",
44
- type: parametersType,
48
+ type: parametersType.join(" & "),
45
49
  },
46
50
  ],
47
51
  statements,
@@ -12,23 +12,34 @@ export function createFunctionDeclaration() {
12
12
  if (this.extern) {
13
13
  return Maybe.empty();
14
14
  }
15
- const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature
16
- .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
17
- .toList());
18
- // Should always have at least an identifier
19
- invariant(parametersPropertySignatures.length > 0);
20
- const parametersType = [`{ ${parametersPropertySignatures.join(", ")} }`]
21
- .concat(this.parentObjectTypes.map((parentObjectType) => `Parameters<typeof ${parentObjectType.staticModuleName}.${syntheticNamePrefix}create>[0]`))
22
- .join(" & ");
15
+ const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature.toList());
16
+ const parametersType = [];
17
+ if (parametersPropertySignatures.length > 0) {
18
+ parametersType.push(`{ ${parametersPropertySignatures
19
+ .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
20
+ .join(", ")} }`);
21
+ }
22
+ for (const parentObjectType of this.parentObjectTypes) {
23
+ parametersType.push(`Parameters<typeof ${parentObjectType.staticModuleName}.${syntheticNamePrefix}create>[0]`);
24
+ }
25
+ if (parametersType.length === 0) {
26
+ parametersType.push("object");
27
+ }
23
28
  const propertyInitializers = [];
24
29
  const omitPropertyNames = [];
25
30
  const propertyStatements = [];
26
31
  for (const parentObjectType of this.parentObjectTypes) {
27
32
  propertyInitializers.push(`...${parentObjectType.staticModuleName}.${syntheticNamePrefix}create(parameters)`);
28
33
  }
34
+ const parametersHasQuestionToken = this.parentObjectTypes.length === 0 &&
35
+ parametersPropertySignatures.every((propertySignature) => !!propertySignature.hasQuestionToken);
36
+ const parametersVariable = `parameters${parametersHasQuestionToken ? "?" : ""}`;
29
37
  for (const property of this.properties) {
30
38
  const thisPropertyStatements = property.constructorStatements({
31
- variables: { parameter: `parameters.${property.name}` },
39
+ variables: {
40
+ parameter: `${parametersVariable}.${property.name}`,
41
+ parameters: parametersVariable,
42
+ },
32
43
  });
33
44
  if (thisPropertyStatements.length > 0) {
34
45
  propertyInitializers.push(property.name);
@@ -46,8 +57,9 @@ export function createFunctionDeclaration() {
46
57
  name: `${syntheticNamePrefix}create`,
47
58
  parameters: [
48
59
  {
60
+ hasQuestionToken: parametersHasQuestionToken,
49
61
  name: "parameters",
50
- type: parametersType,
62
+ type: parametersType.join(" & "),
51
63
  },
52
64
  ],
53
65
  returnType: omitPropertyNames.length === 0
@@ -38,7 +38,9 @@ export function equalsFunctionOrMethodDeclaration() {
38
38
  }
39
39
  }
40
40
  for (const property of this.ownProperties) {
41
- chain.push(`(${property.equalsFunction})(${leftVariable}.${property.name}, ${rightVariable}.${property.name}).mapLeft(propertyValuesUnequal => ({ left: ${leftVariable}, right: ${rightVariable}, propertyName: "${property.name}", propertyValuesUnequal, type: "Property" as const }))`);
41
+ property.equalsFunction.ifJust((equalsFunction) => {
42
+ chain.push(`(${equalsFunction})(${leftVariable}.${property.name}, ${rightVariable}.${property.name}).mapLeft(propertyValuesUnequal => ({ left: ${leftVariable}, right: ${rightVariable}, propertyName: "${property.name}", propertyValuesUnequal, type: "Property" as const }))`);
43
+ });
42
44
  }
43
45
  return Maybe.of({
44
46
  hasOverrideKeyword,