Skip to content

Commit 78a8e82

Browse files
authored
Merge pull request #1372 from Niraj-Kamdar/nk/fix-app-codegen
feat(schema-compose): importing type with map from external imports
2 parents 06e1eab + b791dd7 commit 78a8e82

7 files changed

Lines changed: 223 additions & 1 deletion

File tree

packages/schema/compose/src/resolve.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import {
2929
EnumRef,
3030
EnvDefinition,
3131
ImportedEnvDefinition,
32+
MapDefinition,
33+
ArrayDefinition,
34+
PropertyDefinition,
35+
MethodDefinition,
3236
} from "@polywrap/wrap-manifest-types-js";
3337
import {
3438
parseSchema,
@@ -56,6 +60,8 @@ import {
5660
createImportedEnvDefinition,
5761
visitImportedEnvDefinition,
5862
isImportedEnvType,
63+
mapUtils,
64+
ScalarType,
5965
} from "@polywrap/schema-parse";
6066

6167
type ImplementationWithInterfaces = {
@@ -441,6 +447,95 @@ const extractObjectImportDependencies = (
441447

442448
const namespaceTypes = (namespace: string): AbiTransforms => ({
443449
enter: {
450+
PropertyDefinition: (def: PropertyDefinition & Namespaced) => {
451+
if (def.__namespaced) {
452+
return def;
453+
}
454+
455+
return {
456+
...def,
457+
type: mapUtils.appendNamespace(namespace, def.type),
458+
__namespaced: true,
459+
};
460+
},
461+
MethodDefinition: (def: MethodDefinition & Namespaced) => {
462+
if (def.__namespaced) {
463+
return def;
464+
}
465+
466+
return {
467+
...def,
468+
type: mapUtils.appendNamespace(namespace, def.type),
469+
__namespaced: true,
470+
};
471+
},
472+
MapDefinition: (def: MapDefinition & Namespaced) => {
473+
if (def.__namespaced) {
474+
return def;
475+
}
476+
477+
return {
478+
...def,
479+
type: mapUtils.appendNamespace(namespace, def.type),
480+
__namespaced: true,
481+
};
482+
},
483+
ArrayDefinition: (def: ArrayDefinition & Namespaced) => {
484+
if (def.__namespaced) {
485+
return def;
486+
}
487+
488+
const _item = def.item && {
489+
...def.item,
490+
type: mapUtils.appendNamespace(namespace, def.item.type),
491+
__namespaced: true,
492+
};
493+
494+
const _array = def.array && {
495+
...def.array,
496+
type: mapUtils.appendNamespace(namespace, def.array.type),
497+
__namespaced: true,
498+
};
499+
500+
const _object = def.object && {
501+
...def.object,
502+
type: mapUtils.appendNamespace(namespace, def.object.type),
503+
__namespaced: true,
504+
};
505+
506+
const _enum = def.enum && {
507+
...def.enum,
508+
type: mapUtils.appendNamespace(namespace, def.enum.type),
509+
__namespaced: true,
510+
};
511+
512+
const _map = def.map && {
513+
...def.map,
514+
type: mapUtils.appendNamespace(namespace, def.map.type),
515+
__namespaced: true,
516+
};
517+
518+
const _scalar = def.scalar && {
519+
...def.scalar,
520+
type: mapUtils.appendNamespace(
521+
namespace,
522+
def.scalar.type
523+
) as ScalarType,
524+
__namespaced: true,
525+
};
526+
527+
return {
528+
...def,
529+
item: _item,
530+
array: _array,
531+
object: _object,
532+
enum: _enum,
533+
map: _map,
534+
scalar: _scalar,
535+
type: mapUtils.appendNamespace(namespace, def.type),
536+
__namespaced: true,
537+
};
538+
},
444539
ObjectRef: (def: ObjectRef & Namespaced) => {
445540
if (def.__namespaced) {
446541
return def;

packages/schema/parse/src/extract/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export const extractors: SchemaExtractorBuilder[] = [
2222
getEnvVisitor,
2323
getImportedEnvTypesVisitor,
2424
];
25+
26+
export * as mapUtils from "./utils/map-utils";

packages/schema/parse/src/extract/utils/map-utils.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,47 @@ const _toGraphQLType = (rootType: string, type: string): string => {
108108
}
109109
};
110110

111+
const _appendNamespace = (
112+
namespace: string,
113+
rootType: string,
114+
type: string
115+
): string => {
116+
const parsedCurrentType = _parseCurrentType(rootType, type);
117+
let { subType } = parsedCurrentType;
118+
const { currentType } = parsedCurrentType;
119+
120+
if (!subType) {
121+
return isScalarType(currentType)
122+
? currentType
123+
: `${namespace}_${currentType}`;
124+
}
125+
126+
switch (currentType) {
127+
case "Array": {
128+
if (subType.endsWith("!")) {
129+
subType = subType.slice(0, -1);
130+
}
131+
return `[${_appendNamespace(namespace, rootType, subType)}]`;
132+
}
133+
case "Map": {
134+
const firstDelimiter = subType.indexOf(",");
135+
136+
const keyType = subType.substring(0, firstDelimiter).trim();
137+
const valType = subType.substring(firstDelimiter + 1).trim();
138+
139+
return `Map<${_appendNamespace(
140+
namespace,
141+
rootType,
142+
keyType
143+
)}, ${_appendNamespace(namespace, rootType, valType)}>`;
144+
}
145+
default:
146+
throw new Error(
147+
`Found unknown type ${currentType} while parsing ${rootType}`
148+
);
149+
}
150+
};
151+
111152
const _parseMapType = (
112153
rootType: string,
113154
type: string,
@@ -189,3 +230,7 @@ export function parseMapType(type: string, name?: string): GenericDefinition {
189230
export function toGraphQLType(type: string): string {
190231
return _toGraphQLType(type, type);
191232
}
233+
234+
export function appendNamespace(namespace: string, type: string): string {
235+
return _appendNamespace(namespace, type, type);
236+
}

packages/test-cases/cases/compose/002-external-imports/00-sanity/imports-ext/external.eth/module.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,61 @@ export const abi: WrapAbi = {
6565
"required": true,
6666
"kind": 4
6767
}
68-
}
68+
},
69+
{
70+
"kind": 34,
71+
"map": {
72+
"array": {
73+
"item": {
74+
"kind": 8192,
75+
"name": "map",
76+
"required": true,
77+
"type": "ExternalType",
78+
},
79+
"kind": 18,
80+
"name": "map",
81+
"object": {
82+
"kind": 8192,
83+
"name": "map",
84+
"required": true,
85+
"type": "ExternalType",
86+
},
87+
"required": true,
88+
"type": "[ExternalType]",
89+
},
90+
"key": {
91+
"kind": 4,
92+
"name": "map",
93+
"required": true,
94+
"type": "String",
95+
},
96+
"kind": 262146,
97+
"name": "map",
98+
"required": true,
99+
"type": "Map<String, [ExternalType]>",
100+
"value": {
101+
"item": {
102+
"kind": 8192,
103+
"name": "map",
104+
"required": true,
105+
"type": "ExternalType",
106+
},
107+
"kind": 18,
108+
"name": "map",
109+
"object": {
110+
"kind": 8192,
111+
"name": "map",
112+
"required": true,
113+
"type": "ExternalType",
114+
},
115+
"required": true,
116+
"type": "[ExternalType]",
117+
},
118+
},
119+
"name": "map",
120+
"required": true,
121+
"type": "Map<String, [ExternalType]>",
122+
},
69123
],
70124
"return": {
71125
"type": "String",

packages/test-cases/cases/compose/002-external-imports/00-sanity/output/module.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Namespace_Module @imported(
5656
) {
5757
envMethod(
5858
arg: String!
59+
map: Map! @annotate(type: "Map<String!, [Namespace_ExternalType!]!>!")
5960
): String! @env(required: true)
6061

6162
optEnvMethod(

packages/test-cases/cases/compose/002-external-imports/00-sanity/output/module.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
WrapAbi,
88
createImportedModuleDefinition,
99
createImportedEnvDefinition,
10+
createMapPropertyDefinition,
11+
createMapKeyDefinition,
12+
createArrayDefinition,
13+
createObjectRef,
1014
} from "@polywrap/schema-parse";
1115

1216
export const abi: WrapAbi = {
@@ -47,6 +51,26 @@ export const abi: WrapAbi = {
4751
type: "String",
4852
required: true,
4953
}),
54+
createMapPropertyDefinition({
55+
name: "map",
56+
type: "Map<String, [Namespace_ExternalType]>",
57+
required: true,
58+
key: createMapKeyDefinition({
59+
name: "map",
60+
required: true,
61+
type: "String"
62+
}),
63+
value: createArrayDefinition({
64+
name: "map",
65+
type: "[Namespace_ExternalType]",
66+
required: true,
67+
item: createObjectRef({
68+
name: "map",
69+
type: "Namespace_ExternalType",
70+
required: true,
71+
})
72+
})
73+
})
5074
],
5175
env: {
5276
required: true

packages/test-cases/cases/wrappers/wasm-as/interface-invoke/test-interface/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Module {
66

77
type Argument {
88
str: String!
9+
map: Map @annotate(type: "Map<String!, [InterfaceType!]!>")
910
}
1011

1112
type InterfaceType {

0 commit comments

Comments
 (0)