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
4 changes: 2 additions & 2 deletions forward_engineering/mappers/enums.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @import {FEStatement, FEEnumDefinition, FEEnumDefinitionsSchema, EnumValue, IdToNameMap} from "../../shared/types/types"
* @import {FEStatement, FEEnumDefinition, FEEnumDefinitionsSchema, FEEnumValue, IdToNameMap} from "../../shared/types/types"
*/

const { joinInlineStatements } = require('../helpers/feStatementJoinHelper');
Expand Down Expand Up @@ -47,7 +47,7 @@ function mapEnum({ name, enumDefinition, definitionsIdToNameMap }) {
* Maps the enum values to an array of FEStatement.
*
* @param {object} param0
* @param {EnumValue[]} param0.enumValues - The enum values.
* @param {FEEnumValue[]} param0.enumValues - The enum values.
* @param {IdToNameMap} param0.definitionsIdToNameMap - The definitions id to name map.
* @returns {FEStatement[]}
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"graphql": "16.10.0"
},
"lint-staged": {
"*.{js,json}": "prettier --write"
"*.{js,ts,json}": "prettier --write"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
Expand Down
57 changes: 57 additions & 0 deletions reverse_engineering/mappers/typeDefinitions/enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @import {EnumTypeDefinitionNode, EnumValueDefinitionNode} from "graphql"
* @import {REEnumDefinition, REEnumValue} from "../../../shared/types/types"
*/

const { mapDirectivesUsage } = require('../directiveUsage');

/**
* Maps the enum type nodes to enum definitions
*
* @param {object} params
* @param {EnumTypeDefinitionNode[]} params.enums - The enums nodes
* @returns {REEnumDefinition[]} The mapped enum type definitions
*/
function getEnumTypeDefinitions({ enums = [] }) {
return enums.map(enumNode => mapEnum({ enumNode }));
}

/**
* Maps a single enum node to enum definition
*
* @param {object} params
* @param {EnumTypeDefinitionNode} params.enumNode - The enum to map
* @returns {REEnumDefinition} The mapped enum definition
*/
function mapEnum({ enumNode }) {
return {
type: 'enum',
name: enumNode.name.value,
description: enumNode.description?.value || '',
enumValues: mapEnumValues({ values: [...(enumNode.values || [])] }),
typeDirectives: mapDirectivesUsage({ directives: [...(enumNode.directives || [])] }),
};
}

/**
* Maps the enum value nodes to enum values definitions
*
* @param {object} params
* @param {EnumValueDefinitionNode[]} params.values
* @returns {REEnumValue[]}
*/
function mapEnumValues({ values }) {
return values.map(value => ({
value: value.name.value,
description: value.description?.value || '',
valueDirectives: mapDirectivesUsage({ directives: [...(value.directives || [])] }),
}));
}

module.exports = {
getEnumTypeDefinitions,

// For testing
mapEnum,
mapEnumValues,
};
28 changes: 25 additions & 3 deletions reverse_engineering/mappers/typeDefinitions/typeDefinitions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
/**
* @import {DefinitionNode} from "graphql"
* @import {REDirectiveDefinition, REDefinitionsSchema, FieldsOrder, RECustomScalarDefinition, REDefinition, REModelDefinitionsSchema, DefinitionREStructure, DirectiveStructureType, ScalarStructureType} from "../../../shared/types/types"
* @import {REDirectiveDefinition,
* REDefinitionsSchema,
* FieldsOrder,
* RECustomScalarDefinition,
* REDefinition,
* REModelDefinitionsSchema,
* DefinitionREStructure,
* DirectiveStructureType,
* REEnumDefinition,
* EnumStructureType,
* ScalarStructureType} from "../../../shared/types/types"
*/

const { astNodeKind } = require('../../constants/graphqlAST');
const { findNodesByKind } = require('../../helpers/findNodesByKind');
const { sortByName } = require('../../helpers/sortByName');
const { getCustomScalarTypeDefinitions } = require('./customScalar');
const { getDirectiveTypeDefinitions } = require('./directive');
const { getEnumTypeDefinitions } = require('./enum');

/**
* Gets the type definitions structure
Expand All @@ -24,8 +35,11 @@ function getTypeDefinitions({ typeDefinitions, fieldsOrder }) {
const customScalars = getCustomScalarTypeDefinitions({
customScalars: findNodesByKind({ nodes: typeDefinitions, kind: astNodeKind.SCALAR_TYPE_DEFINITION }),
});
const enums = getEnumTypeDefinitions({
enums: findNodesByKind({ nodes: typeDefinitions, kind: astNodeKind.ENUM_TYPE_DEFINITION }),
});

const definitions = getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars });
const definitions = getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars, enums });

return definitions;
}
Expand All @@ -37,9 +51,10 @@ function getTypeDefinitions({ typeDefinitions, fieldsOrder }) {
* @param {FieldsOrder} params.fieldsOrder - The fields order
* @param {REDirectiveDefinition[]} params.directives - The directive definitions
* @param {RECustomScalarDefinition[]} params.customScalars - The custom scalar definitions
* @param {REEnumDefinition[]} params.enums - The enum definitions
* @returns {REModelDefinitionsSchema} The type definitions structure
*/
function getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars }) {
function getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars, enums }) {
const definitions = {
['Directives']: /** @type {DirectiveStructureType} */ (
getDefinitionCategoryStructure({
Expand All @@ -55,6 +70,13 @@ function getTypeDefinitionsStructure({ fieldsOrder, directives, customScalars })
properties: customScalars,
})
),
['Enums']: /** @type {EnumStructureType} */ (
getDefinitionCategoryStructure({
fieldsOrder,
subtype: 'enum',
properties: enums,
})
),
};

return {
Expand Down
35 changes: 16 additions & 19 deletions shared/types/fe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
DirectiveDefinition,
DirectiveLocations,
DirectivePropertyData,
EnumDefinition,
EnumValue,
} from './shared';

export type FEStatement = {
Expand All @@ -20,7 +22,8 @@ export type FEStatement = {

export type IdToNameMap = Record<string, string>;

export type FEDefinitionsSchema = FEObjectLikeDefinitionsSchema
export type FEDefinitionsSchema =
| FEObjectLikeDefinitionsSchema
| FECustomScalarDefinitionsSchema
| FEEnumDefinitionsSchema
| FEUnionDefinitionsSchema
Expand All @@ -32,18 +35,9 @@ export type FECustomScalarDefinitionsSchema = Record<string, FECustomScalarDefin
export type FECustomScalarDefinition = CustomScalarDefinition<DirectivePropertyData>;

// Enum
export type EnumValue = {
value: string; // The name of the enum value
description?: string; // The description of the enum value
typeDirectives?: DirectivePropertyData[]; // The directives of the enum value
};
export type FEEnumValue = EnumValue<DirectivePropertyData>;

export type FEEnumDefinition = {
description?: string; // The description of the enum
isActivated?: boolean; // Indicates if the enum is activated
typeDirectives?: DirectivePropertyData[]; // The directives of the enum
enumValues: EnumValue[]; // The values of the enum
};
export type FEEnumDefinition = EnumDefinition<DirectivePropertyData>;

export type FEEnumDefinitionsSchema = Record<string, FEEnumDefinition>;

Expand All @@ -62,7 +56,7 @@ export type FEObjectLikeDefinition = {
// Field data type
export type FieldData = RegularFieldData | ReferenceFieldData;

export type FieldSchema = Record<string, FieldData>
export type FieldSchema = Record<string, FieldData>;

export type ArrayItems = ArrayItem | ArrayItem[];

Expand Down Expand Up @@ -109,7 +103,7 @@ export type Argument = {
export type ArgumentsResultStatement = {
argumentsStatement: string; // The formatted arguments string.
argumentsWarningComment: string; // The warning comment if any argument is missing a type.
}
};

// Directives
export type FEDirectiveLocations = DirectiveLocations & {
Expand Down Expand Up @@ -169,9 +163,9 @@ export type RootTypeNamesParameter = {
};

type EntityDetails = {
operationType?: string
operationType?: string;
typeDirectives?: DirectivePropertyData[];
}
};

export type EntityIdToJsonSchemaMap = Record<string, string>;
export type EntityIdToPropertiesMap = Record<string, [EntityDetails]>;
Expand Down Expand Up @@ -232,15 +226,18 @@ export type ValidationResponseItem = {
context?: string; // The context of the entity, typically additional information.
};

export type ValidateScriptCallback = (error: Error | null | unknown, validationErrors?: ValidationResponseItem[]) => void;
export type ValidateScriptCallback = (
error: Error | null | unknown,
validationErrors?: ValidationResponseItem[],
) => void;

export type BaseGetFieldParams = {
fields?: FieldSchema; // The fields to get
requiredFields?: string[]; // The required fields list
definitionsIdToNameMap: IdToNameMap; // The definitions id to name map
}
};

export type GetFieldsParams = BaseGetFieldParams & {
addArguments: boolean; // Indicates if arguments should be added.
addDefaultValue: boolean; // Indicates if default value should be added.
}
};
34 changes: 24 additions & 10 deletions shared/types/re.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ContainerSchemaRootTypes,
CustomScalarDefinition,
DirectiveDefinition,
EnumDefinition,
EnumValue,
StructuredDirective,
} from './shared';

Expand Down Expand Up @@ -118,36 +120,48 @@ export type REDefinitionsSchema = REDirectiveDefinitionsSchema | RECustomScalarD

export type REDirectiveDefinitionsSchema = Record<string, REDirectiveDefinition>;
export type RECustomScalarDefinitionsSchema = Record<string, RECustomScalarDefinition>;
export type REEnumDefinitionsSchema = Record<string, REEnumDefinition>;

export type REDefinition = RECustomScalarDefinition | REDirectiveDefinition;
export type REDefinition = RECustomScalarDefinition | REDirectiveDefinition | REEnumDefinition;

export type REModelDefinitionsSchema = {
definitions: {
Directives: DirectiveStructureType;
Scalars: ScalarStructureType;
}
}
};
};

export type DefinitionREStructure = DirectiveStructureType | ScalarStructureType;
export type DefinitionREStructure = DirectiveStructureType | ScalarStructureType | EnumStructureType;

type StructureType<T> = {
type: 'type';
structureType: true,
properties: T
}
structureType: true;
properties: T;
};

export type DirectiveStructureType = StructureType<REDirectiveDefinitionsSchema> & {
subtype: 'directive';
}
};

export type ScalarStructureType = StructureType<RECustomScalarDefinitionsSchema> & {
subtype: 'scalar';
}
};

export type EnumStructureType = StructureType<REEnumDefinitionsSchema> & {
subtype: 'enum';
};

export type RECustomScalarDefinition = CustomScalarDefinition<StructuredDirective> & {
type: 'scalar';
name: string;
}
};

export type REEnumDefinition = EnumDefinition<StructuredDirective> & {
type: 'enum';
name: string;
};

export type REEnumValue = EnumValue<StructuredDirective>;

export type TestConnectionCallback = (err?: Error | unknown) => void;
export type DisconnectCallback = TestConnectionCallback;
20 changes: 17 additions & 3 deletions shared/types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,27 @@ type StructuredDirective = {
export type CustomScalarDefinition<T> = {
description?: string;
isActivated?: boolean;
typeDirectives?: T[]
}
typeDirectives?: T[];
};

export type DirectiveDefinition<T, D = DirectiveLocations> = {
type: 'directive';
description?: string;
comments?: string;
arguments?: T[];
directiveLocations: D;
}
};

// Enum
export type EnumValue<T> = {
value: string; // The name of the enum value
description?: string; // The description of the enum value
typeDirectives?: T[]; // The directives of the enum value
};

export type EnumDefinition<T> = {
description?: string; // The description of the enum
isActivated?: boolean; // Indicates if the enum is activated
typeDirectives?: T[]; // The directives of the enum
enumValues: EnumValue<T>[]; // The values of the enum
};
Loading