Skip to content

Commit

Permalink
normal enums and nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Nov 1, 2024
1 parent 5ab016c commit f63b57a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 98 deletions.
2 changes: 1 addition & 1 deletion examples/typescript-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-node",
"version": "2.0.0",
"version": "3.0.0",
"description": "",
"private": true,
"main": "index.js",
Expand Down
60 changes: 30 additions & 30 deletions examples/typescript-node/src/zeus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,15 +1107,15 @@ export type ModelTypes = {
["Nameable"]: ModelTypes["CardStack"] | ModelTypes["Card"] | ModelTypes["SpecialCard"] | ModelTypes["EffectCard"];
["SpecialSkills"]:SpecialSkills;
["Query"]: {
cardById?: ModelTypes["Card"] | undefined,
cardById?: ModelTypes["Card"] | undefined | null,
/** Draw a card<br> */
drawCard: ModelTypes["Card"],
drawChangeCard: ModelTypes["ChangeCard"],
/** list All Cards availble<br> */
listCards: Array<ModelTypes["Card"]>,
myStacks?: Array<ModelTypes["CardStack"]> | undefined,
myStacks?: Array<ModelTypes["CardStack"]> | undefined | null,
nameables: Array<ModelTypes["Nameable"]>,
public?: ModelTypes["Public"] | undefined
public?: ModelTypes["Public"] | undefined | null
};
/** create card inputs<br> */
["createCard"]: {
Expand All @@ -1124,39 +1124,39 @@ export type ModelTypes = {
/** Description of a card<br> */
description: string,
/** <div>How many children the greek god had</div> */
Children?: number | undefined,
Children?: number | undefined | null,
/** The attack power<br> */
Attack: number,
/** The defense power<br> */
Defense: number,
/** input skills */
skills?: Array<ModelTypes["SpecialSkills"]> | undefined
skills?: Array<ModelTypes["SpecialSkills"]> | undefined | null
};
/** Stack of cards */
["CardStack"]: {
cards?: Array<ModelTypes["Card"]> | undefined,
cards?: Array<ModelTypes["Card"]> | undefined | null,
name: string
};
/** Card used in card game<br> */
["Card"]: {
/** The attack power<br> */
Attack: number,
/** <div>How many children the greek god had</div> */
Children?: number | undefined,
Children?: number | undefined | null,
/** The defense power<br> */
Defense: number,
/** Attack other cards on the table , returns Cards after attack<br> */
attack?: Array<ModelTypes["Card"]> | undefined,
attack?: Array<ModelTypes["Card"]> | undefined | null,
/** Put your description here */
cardImage?: ModelTypes["S3Object"] | undefined,
cardImage?: ModelTypes["S3Object"] | undefined | null,
/** Description of a card<br> */
description: string,
id: string,
image: string,
info: ModelTypes["JSON"],
/** The name of a card<br> */
name: string,
skills?: Array<ModelTypes["SpecialSkills"]> | undefined
skills?: Array<ModelTypes["SpecialSkills"]> | undefined | null
};
["SpecialCard"]: {
effect: string,
Expand All @@ -1178,19 +1178,19 @@ export type ModelTypes = {
region: string
};
["Public"]: {
powerups?: Array<ModelTypes["Powerup"]> | undefined
powerups?: Array<ModelTypes["Powerup"]> | undefined | null
};
["Powerup"]: {
name?: string | undefined
name?: string | undefined | null
};
["ChangeCard"]:ModelTypes["SpecialCard"] | ModelTypes["EffectCard"];
["Subscription"]: {
deck?: Array<ModelTypes["Card"]> | undefined
deck?: Array<ModelTypes["Card"]> | undefined | null
};
["schema"]: {
query?: ModelTypes["Query"] | undefined,
mutation?: ModelTypes["Mutation"] | undefined,
subscription?: ModelTypes["Subscription"] | undefined
query?: ModelTypes["Query"] | undefined | null,
mutation?: ModelTypes["Mutation"] | undefined | null,
subscription?: ModelTypes["Subscription"] | undefined | null
}
}

Expand All @@ -1206,15 +1206,15 @@ export type GraphQLTypes = {
["SpecialSkills"]: SpecialSkills;
["Query"]: {
__typename: "Query",
cardById?: GraphQLTypes["Card"] | undefined,
cardById?: GraphQLTypes["Card"] | undefined | null,
/** Draw a card<br> */
drawCard: GraphQLTypes["Card"],
drawChangeCard: GraphQLTypes["ChangeCard"],
/** list All Cards availble<br> */
listCards: Array<GraphQLTypes["Card"]>,
myStacks?: Array<GraphQLTypes["CardStack"]> | undefined,
myStacks?: Array<GraphQLTypes["CardStack"]> | undefined | null,
nameables: Array<GraphQLTypes["Nameable"]>,
public?: GraphQLTypes["Public"] | undefined
public?: GraphQLTypes["Public"] | undefined | null
};
/** create card inputs<br> */
["createCard"]: {
Expand All @@ -1223,18 +1223,18 @@ export type GraphQLTypes = {
/** Description of a card<br> */
description: string,
/** <div>How many children the greek god had</div> */
Children?: number | undefined,
Children?: number | undefined | null,
/** The attack power<br> */
Attack: number,
/** The defense power<br> */
Defense: number,
/** input skills */
skills?: Array<GraphQLTypes["SpecialSkills"]> | undefined
skills?: Array<GraphQLTypes["SpecialSkills"]> | undefined | null
};
/** Stack of cards */
["CardStack"]: {
__typename: "CardStack",
cards?: Array<GraphQLTypes["Card"]> | undefined,
cards?: Array<GraphQLTypes["Card"]> | undefined | null,
name: string
};
/** Card used in card game<br> */
Expand All @@ -1243,21 +1243,21 @@ export type GraphQLTypes = {
/** The attack power<br> */
Attack: number,
/** <div>How many children the greek god had</div> */
Children?: number | undefined,
Children?: number | undefined | null,
/** The defense power<br> */
Defense: number,
/** Attack other cards on the table , returns Cards after attack<br> */
attack?: Array<GraphQLTypes["Card"]> | undefined,
attack?: Array<GraphQLTypes["Card"]> | undefined | null,
/** Put your description here */
cardImage?: GraphQLTypes["S3Object"] | undefined,
cardImage?: GraphQLTypes["S3Object"] | undefined | null,
/** Description of a card<br> */
description: string,
id: string,
image: string,
info: GraphQLTypes["JSON"],
/** The name of a card<br> */
name: string,
skills?: Array<GraphQLTypes["SpecialSkills"]> | undefined
skills?: Array<GraphQLTypes["SpecialSkills"]> | undefined | null
};
["SpecialCard"]: {
__typename: "SpecialCard",
Expand All @@ -1284,11 +1284,11 @@ export type GraphQLTypes = {
};
["Public"]: {
__typename: "Public",
powerups?: Array<GraphQLTypes["Powerup"]> | undefined
powerups?: Array<GraphQLTypes["Powerup"]> | undefined | null
};
["Powerup"]: {
__typename: "Powerup",
name?: string | undefined
name?: string | undefined | null
};
["ChangeCard"]:{
__typename:"SpecialCard" | "EffectCard"
Expand All @@ -1297,10 +1297,10 @@ export type GraphQLTypes = {
};
["Subscription"]: {
__typename: "Subscription",
deck?: Array<GraphQLTypes["Card"]> | undefined
deck?: Array<GraphQLTypes["Card"]> | undefined | null
}
}
export const enum SpecialSkills {
export enum SpecialSkills {
THUNDER = "THUNDER",
RAIN = "RAIN",
FIRE = "FIRE"
Expand Down
8 changes: 5 additions & 3 deletions packages/graphql-zeus-core/TranslateGraphQL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export interface TranslateOptions {
host?: string;
esModule?: boolean;
subscriptions?: 'legacy' | 'graphql-ws';
constEnums?: boolean;
}

export class TranslateGraphQL {
static typescript = ({ schema, env = 'browser', host, subscriptions = 'legacy' }: TranslateOptions) => {
static typescript = ({ schema, env = 'browser', host, subscriptions = 'legacy', constEnums }: TranslateOptions) => {
const tree = Parser.parseAddExtensions(schema);
const ts = TreeToTS.resolveTree({ tree, env, host, subscriptions });
const ts = TreeToTS.resolveTree({ tree, env, host, subscriptions, constEnums });
return ts;
};

Expand All @@ -23,9 +24,10 @@ export class TranslateGraphQL {
host,
esModule,
subscriptions = 'legacy',
constEnums,
}: TranslateOptions) => {
const tree = Parser.parseAddExtensions(schema);
const ts = TreeToTS.resolveTreeSplit({ tree, env, host, esModule, subscriptions });
const ts = TreeToTS.resolveTreeSplit({ tree, env, host, esModule, subscriptions, constEnums });
return {
const: TreeToTS.resolveBasisHeader().concat(ts.const),
index: TreeToTS.resolveBasisHeader().concat(ts.indexImports).concat('\n').concat(ts.index),
Expand Down
12 changes: 9 additions & 3 deletions packages/graphql-zeus-core/TreeToTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ResolveOptions {
headers?: Record<string, string>;
esModule?: boolean;
subscriptions?: 'legacy' | 'graphql-ws';
constEnums?: boolean;
}

const disableLintersComments = ['eslint-disable'];
Expand Down Expand Up @@ -67,8 +68,8 @@ export class TreeToTS {
return propTypes.concat('\n\n').concat(returnTypes).concat('\n\n').concat(opsString);
}

static resolveBasisTypes(tree: ParserTree): string {
const rootTypes = resolveTypes(tree.nodes);
static resolveBasisTypes(tree: ParserTree, options?: { constEnums?: boolean }): string {
const rootTypes = resolveTypes(tree.nodes, options);
const valueTypes = resolveValueTypes(tree.nodes);
const inputTypes = resolveInputTypes(tree.nodes);
const modelTypes = resolveModelTypes(tree.nodes);
Expand Down Expand Up @@ -103,6 +104,7 @@ export class TreeToTS {
esModule,
headers,
subscriptions = 'legacy',
constEnums,
}: ResolveOptions) {
return {
indexImports: `import { AllTypesProps, ReturnTypes, Ops } from './const${
Expand All @@ -124,7 +126,11 @@ import WebSocket from 'ws';`
.concat('\n')
.concat(typescriptFunctions)
.concat('\n')
.concat(TreeToTS.resolveBasisTypes(tree)),
.concat(
TreeToTS.resolveBasisTypes(tree, {
constEnums,
}),
),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
import { resolveEnum } from '@/TreeToTS/templates/returnedTypes/enum';
import { createParserField, Options, TypeDefinition, ValueDefinition } from 'graphql-js-tree';

describe('It creates correct TypeScript const enums', () => {
test('Simple enum with 2 fields', () => {
const enumString = resolveEnum(
createParserField({
args: [
createParserField({
type: {
fieldType: {
name: ValueDefinition.EnumValueDefinition,
type: Options.name,
},
},
args: [],
directives: [],
interfaces: [],
name: 'CREATED',
data: {
type: ValueDefinition.EnumValueDefinition,
},
}),
createParserField({
type: {
fieldType: {
name: ValueDefinition.EnumValueDefinition,
type: Options.name,
},
},
args: [],
directives: [],
interfaces: [],
name: 'DELETED',
data: {
type: ValueDefinition.EnumValueDefinition,
},
}),
],
data: {
type: TypeDefinition.EnumTypeDefinition,
const mockEnum = createParserField({
args: [
createParserField({
type: {
fieldType: {
name: ValueDefinition.EnumValueDefinition,
type: Options.name,
},
directives: [],
interfaces: [],
name: 'Status',
type: {
fieldType: {
name: 'enum',
type: Options.name,
},
},
args: [],
directives: [],
interfaces: [],
name: 'CREATED',
data: {
type: ValueDefinition.EnumValueDefinition,
},
}),
createParserField({
type: {
fieldType: {
name: ValueDefinition.EnumValueDefinition,
type: Options.name,
},
}),
);
expect(enumString).toEqual(`export const enum Status {
},
args: [],
directives: [],
interfaces: [],
name: 'DELETED',
data: {
type: ValueDefinition.EnumValueDefinition,
},
}),
],
data: {
type: TypeDefinition.EnumTypeDefinition,
},
directives: [],
interfaces: [],
name: 'Status',
type: {
fieldType: {
name: 'enum',
type: Options.name,
},
},
});

describe('It creates correct TypeScript const enums', () => {
test('Simple enum with 2 fields', () => {
const enumString = resolveEnum(mockEnum);
const enumConstString = resolveEnum(mockEnum, true);
expect(enumString).toEqual(`export enum Status {
\tCREATED = "CREATED",
\tDELETED = "DELETED"
}`);
expect(enumConstString).toEqual(`export const enum Status {
\tCREATED = "CREATED",
\tDELETED = "DELETED"
}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { plusDescription } from '@/TreeToTS/templates/shared/description';
import { ParserField } from 'graphql-js-tree';

export const resolveEnum = (i: ParserField): string => {
export const resolveEnum = (i: ParserField, constEnum?: boolean): string => {
if (!i.args) {
throw new Error('Empty enum error');
}
return `${plusDescription(i.description)}export const enum ${i.name} {\n${i.args
return `${plusDescription(i.description)}export ${constEnum ? 'const ' : ''}enum ${i.name} {\n${i.args
.map((f) => `\t${f.name} = "${f.name}"`)
.join(',\n')}\n}`;
};
Loading

0 comments on commit f63b57a

Please sign in to comment.