Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions packages/schema/compose/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import {
EnumRef,
EnvDefinition,
ImportedEnvDefinition,
MapDefinition,
ArrayDefinition,
PropertyDefinition,
MethodDefinition,
} from "@polywrap/wrap-manifest-types-js";
import {
parseSchema,
Expand Down Expand Up @@ -56,6 +60,8 @@ import {
createImportedEnvDefinition,
visitImportedEnvDefinition,
isImportedEnvType,
mapUtils,
ScalarType,
} from "@polywrap/schema-parse";

type ImplementationWithInterfaces = {
Expand Down Expand Up @@ -441,6 +447,95 @@ const extractObjectImportDependencies = (

const namespaceTypes = (namespace: string): AbiTransforms => ({
enter: {
PropertyDefinition: (def: PropertyDefinition & Namespaced) => {
if (def.__namespaced) {
return def;
}

return {
...def,
type: mapUtils.appendNamespace(namespace, def.type),
__namespaced: true,
};
},
MethodDefinition: (def: MethodDefinition & Namespaced) => {
if (def.__namespaced) {
return def;
}

return {
...def,
type: mapUtils.appendNamespace(namespace, def.type),
__namespaced: true,
};
},
MapDefinition: (def: MapDefinition & Namespaced) => {
if (def.__namespaced) {
return def;
}

return {
...def,
type: mapUtils.appendNamespace(namespace, def.type),
__namespaced: true,
};
},
ArrayDefinition: (def: ArrayDefinition & Namespaced) => {
if (def.__namespaced) {
return def;
}

const _item = def.item && {
...def.item,
type: mapUtils.appendNamespace(namespace, def.item.type),
__namespaced: true,
};

const _array = def.array && {
...def.array,
type: mapUtils.appendNamespace(namespace, def.array.type),
__namespaced: true,
};

const _object = def.object && {
...def.object,
type: mapUtils.appendNamespace(namespace, def.object.type),
__namespaced: true,
};

const _enum = def.enum && {
...def.enum,
type: mapUtils.appendNamespace(namespace, def.enum.type),
__namespaced: true,
};

const _map = def.map && {
...def.map,
type: mapUtils.appendNamespace(namespace, def.map.type),
__namespaced: true,
};

const _scalar = def.scalar && {
...def.scalar,
type: mapUtils.appendNamespace(
namespace,
def.scalar.type
) as ScalarType,
__namespaced: true,
};

return {
...def,
item: _item,
array: _array,
object: _object,
enum: _enum,
map: _map,
scalar: _scalar,
type: mapUtils.appendNamespace(namespace, def.type),
__namespaced: true,
};
},
ObjectRef: (def: ObjectRef & Namespaced) => {
if (def.__namespaced) {
return def;
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/parse/src/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const extractors: SchemaExtractorBuilder[] = [
getEnvVisitor,
getImportedEnvTypesVisitor,
];

export * as mapUtils from "./utils/map-utils";
45 changes: 45 additions & 0 deletions packages/schema/parse/src/extract/utils/map-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,47 @@ const _toGraphQLType = (rootType: string, type: string): string => {
}
};

const _appendNamespace = (
namespace: string,
rootType: string,
type: string
): string => {
const parsedCurrentType = _parseCurrentType(rootType, type);
let { subType } = parsedCurrentType;
const { currentType } = parsedCurrentType;

if (!subType) {
return isScalarType(currentType)
? currentType
: `${namespace}_${currentType}`;
}

switch (currentType) {
case "Array": {
if (subType.endsWith("!")) {
subType = subType.slice(0, -1);
}
return `[${_appendNamespace(namespace, rootType, subType)}]`;
}
case "Map": {
const firstDelimiter = subType.indexOf(",");

const keyType = subType.substring(0, firstDelimiter).trim();
const valType = subType.substring(firstDelimiter + 1).trim();

return `Map<${_appendNamespace(
namespace,
rootType,
keyType
)}, ${_appendNamespace(namespace, rootType, valType)}>`;
}
default:
throw new Error(
`Found unknown type ${currentType} while parsing ${rootType}`
);
}
};

const _parseMapType = (
rootType: string,
type: string,
Expand Down Expand Up @@ -189,3 +230,7 @@ export function parseMapType(type: string, name?: string): GenericDefinition {
export function toGraphQLType(type: string): string {
return _toGraphQLType(type, type);
}

export function appendNamespace(namespace: string, type: string): string {
return _appendNamespace(namespace, type, type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,61 @@ export const abi: WrapAbi = {
"required": true,
"kind": 4
}
}
},
{
"kind": 34,
"map": {
"array": {
"item": {
"kind": 8192,
"name": "map",
"required": true,
"type": "ExternalType",
},
"kind": 18,
"name": "map",
"object": {
"kind": 8192,
"name": "map",
"required": true,
"type": "ExternalType",
},
"required": true,
"type": "[ExternalType]",
},
"key": {
"kind": 4,
"name": "map",
"required": true,
"type": "String",
},
"kind": 262146,
"name": "map",
"required": true,
"type": "Map<String, [ExternalType]>",
"value": {
"item": {
"kind": 8192,
"name": "map",
"required": true,
"type": "ExternalType",
},
"kind": 18,
"name": "map",
"object": {
"kind": 8192,
"name": "map",
"required": true,
"type": "ExternalType",
},
"required": true,
"type": "[ExternalType]",
},
},
"name": "map",
"required": true,
"type": "Map<String, [ExternalType]>",
},
],
"return": {
"type": "String",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Namespace_Module @imported(
) {
envMethod(
arg: String!
map: Map! @annotate(type: "Map<String!, [Namespace_ExternalType!]!>!")
): String! @env(required: true)

optEnvMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
WrapAbi,
createImportedModuleDefinition,
createImportedEnvDefinition,
createMapPropertyDefinition,
createMapKeyDefinition,
createArrayDefinition,
createObjectRef,
} from "@polywrap/schema-parse";

export const abi: WrapAbi = {
Expand Down Expand Up @@ -47,6 +51,26 @@ export const abi: WrapAbi = {
type: "String",
required: true,
}),
createMapPropertyDefinition({
name: "map",
type: "Map<String, [Namespace_ExternalType]>",
required: true,
key: createMapKeyDefinition({
name: "map",
required: true,
type: "String"
}),
value: createArrayDefinition({
name: "map",
type: "[Namespace_ExternalType]",
required: true,
item: createObjectRef({
name: "map",
type: "Namespace_ExternalType",
required: true,
})
})
})
],
env: {
required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Module {

type Argument {
str: String!
map: Map @annotate(type: "Map<String!, [InterfaceType!]!>")
}

type InterfaceType {
Expand Down