Skip to content

Commit

Permalink
feat: imports for enums added (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Muh-Hasan committed Oct 14, 2021
1 parent 8f6bb1d commit 8e40429
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
17 changes: 14 additions & 3 deletions src/lib/api/buildSchemaToTypescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let upperFirst = require("lodash/upperFirst");

export const buildSchemaToTypescript = (gqlSchema: any) => {
export const buildSchemaToTypescript = (gqlSchema: any, introspection: any) => {
let includeDeprecatedFields = false;

let collectionsObject: {
Expand All @@ -10,6 +10,7 @@ export const buildSchemaToTypescript = (gqlSchema: any) => {
};

let allImports: string[] = [];
let allEnumImports: string[] = [];

let typeStr = "type TestCollection = {\n fields: {\n";

Expand Down Expand Up @@ -48,9 +49,17 @@ export const buildSchemaToTypescript = (gqlSchema: any) => {
field.args.length > 0
? (typeStr += `${type}: [{arguments: ${description}${upperFirst(
type
)}Args; response: ${responseType} }];\n`)
)}Args; response: ${responseType} } ];\n`)
: (typeStr += `${type}: [{ response: ${responseType} }];\n`);

introspection.__schema.types.forEach((v: any) => {
if (v.kind === "ENUM") {
if (v.name !== "__TypeKind" && v.name !== "__DirectiveLocation") {
allEnumImports.push(v.name);
}
}
});

if (
(responseTypeName === "String" ||
responseTypeName === "Int" ||
Expand Down Expand Up @@ -80,10 +89,12 @@ export const buildSchemaToTypescript = (gqlSchema: any) => {

// Remove Duplication from Imports array
let imports: string[] = [...new Set(allImports)];
let enumImports: string[] = [...new Set(allEnumImports)];

return {
collections: collectionsObject,
types: typeStr,
imports: imports,
enumImports: enumImports,
};
};
};
15 changes: 10 additions & 5 deletions src/lib/api/functions/DefineYourOwnApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
mkdirRecursiveAsync,
} from "../../../fs";
import { contextInfo, generatePanacloudConfig } from "../../info";
import { Config, APITYPE, ApiModel, PanacloudconfigFile } from "../../../../utils/constants";
import {
Config,
APITYPE,
ApiModel,
PanacloudconfigFile,
} from "../../../../utils/constants";
import { generator } from "../../generators";
import { introspectionFromSchema, buildSchema } from "graphql";
import { buildSchemaToTypescript } from "../../buildSchemaToTypescript";
Expand Down Expand Up @@ -153,19 +158,19 @@ async function defineYourOwnApi(config: Config, templateDir: string) {
// Model Config
const queriesFields: any = gqlSchema.getQueryType()?.getFields();
const mutationsFields: any = gqlSchema.getMutationType()?.getFields();
model.api.schema = introspectionFromSchema(gqlSchema);
const introspection = introspectionFromSchema(gqlSchema);
model.api.schema = introspection;
model.api.queiresFields = [...Object.keys(queriesFields)];
model.api.mutationFields = [...Object.keys(mutationsFields)];
if (apiType === APITYPE.graphql) {
updatedPanacloudConfig = await generatePanacloudConfig(
model.api.queiresFields,
model.api.mutationFields,
model.api.architecture,
model.api.architecture
);

const mockApiCollection = buildSchemaToTypescript(gqlSchema);
const mockApiCollection = buildSchemaToTypescript(gqlSchema, introspection);
model.api.mockApiData = mockApiCollection;

}
} else {
copyFileAsync(
Expand Down
11 changes: 7 additions & 4 deletions src/lib/api/generators/MockApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class MockApiTestCollectionsFile {
this.code.openFile(this.outputFile);

ts.writeImports("./testCollectionsTypes", ["TestCollection"]);
if (this.config.api.mockApiData?.enumImports.length !== 0) {
ts.writeImports("../../editable_src/graphql/types", [
...this.config.api.mockApiData?.enumImports!,
]);
}
this.code.line();
this.code.indent(`export const testCollections: TestCollection = {
fields:
Expand All @@ -48,10 +53,8 @@ class MockApiTestCollectionsFile {
await this.code.save(this.outputDir);

fse.copy("./types", "./lambdaLayer/mockApi").then(() => {
fse.remove("./types").then(() => {})
})


fse.remove("./types").then(() => {});
});
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum DATABASE {
neptuneDB = "Neptune (Graph)",
auroraDB = "Aurora Serverless (Relational)",
documentDB = "DocumentDB (NoSQL MongoDB)",
none = "None"
none = "None",
}

export enum SAASTYPE {
Expand All @@ -23,7 +23,7 @@ export enum CONSTRUCTS {
neptuneDB = "VpcNeptuneConstruct",
auroraDB = "AuroraDBConstruct",
apigateway = "ApiGatewayConstruct",
eventBridge = "EventBridgeConstruct"
eventBridge = "EventBridgeConstruct",
}

export enum TEMPLATE {
Expand Down Expand Up @@ -56,6 +56,7 @@ export interface mockApiData {
collections: any;
types: any;
imports: string[];
enumImports: string[];
}

export interface API {
Expand All @@ -75,13 +76,13 @@ export interface API {

export enum ARCHITECTURE {
requestDriven = "Request-Driven Architecture",
eventDriven = "Event-Driven Architecture"
eventDriven = "Event-Driven Architecture",
}

export type PanacloudconfigFile = {
lambdas: any
}
lambdas: any;
};

export type PanacloudConfiglambdaParams = {
asset_path: string
}
asset_path: string;
};

0 comments on commit 8e40429

Please sign in to comment.