Skip to content

Commit

Permalink
WIP: Manage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
martmull committed Sep 3, 2024
1 parent e8c98fe commit d85c3a0
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { BadRequestException } from '@nestjs/common';

import { BaseGraphQLError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';

const formatMessage = (message: BaseGraphQLError) => {
let formattedMessage = message.extensions
? message.extensions.response.message || message.extensions.response
: message.message;
const formatMessage = (error: BaseGraphQLError) => {
let formattedMessage = error.extensions
? error.extensions.response?.error ||
error.extensions.response ||
error.message
: error.error;

formattedMessage = formattedMessage
.replace(/"/g, "'")
.replace("Variable '$data' got i", 'I');
.replace("Variable '$data' got i", 'I')
.replace("Variable '$input' got i", 'I');

const regex = /Field '[^']+' is not defined by type .*/;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('computeSchemaComponents', () => {
},
fieldDateTime: {
type: 'string',
format: 'date',
format: 'date-time',
},
fieldDate: {
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { OpenAPIV3_1 } from 'openapi-types';

import { FieldMetadataOptions } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-options.interface';

import {
computeDepthParameters,
computeEndingBeforeParameters,
Expand All @@ -10,7 +12,10 @@ import {
computeStartingAfterParameters,
} from 'src/engine/core-modules/open-api/utils/parameters.utils';
import { compositeTypeDefinitions } from 'src/engine/metadata-modules/field-metadata/composite-types';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { capitalize } from 'src/utils/capitalize';

Expand All @@ -20,11 +25,33 @@ type Properties = {
[name: string]: Property;
};

const isFieldAvailable = (field: FieldMetadataEntity, forResponse: boolean) => {
if (forResponse) {
return true;
}
switch (field.name) {
case 'id':
case 'createdAt':
case 'updatedAt':
case 'deletedAt':
return false;
default:
return true;
}
};

const getFieldProperties = (
type: FieldMetadataType,
propertyName?: string,
options?: FieldMetadataOptions,
): Property => {
switch (type) {
case FieldMetadataType.SELECT:
case FieldMetadataType.MULTI_SELECT:
return {
type: 'string',
enum: options?.map((option: { value: string }) => option.value),
};
case FieldMetadataType.UUID:
return { type: 'string', format: 'uuid' };
case FieldMetadataType.TEXT:
Expand All @@ -34,6 +61,7 @@ const getFieldProperties = (
case FieldMetadataType.EMAIL:
return { type: 'string', format: 'email' };
case FieldMetadataType.DATE_TIME:
return { type: 'string', format: 'date-time' };
case FieldMetadataType.DATE:
return { type: 'string', format: 'date' };
case FieldMetadataType.NUMBER:
Expand All @@ -60,16 +88,24 @@ const getFieldProperties = (
}

return { type: 'object' };

default:
return { type: 'string' };
}
};

const getSchemaComponentsProperties = (
item: ObjectMetadataEntity,
): Properties => {
const getSchemaComponentsProperties = ({
item,
forResponse,
}: {
item: ObjectMetadataEntity;
forResponse: boolean;
}): Properties => {
return item.fields.reduce((node, field) => {
if (field.type == FieldMetadataType.RELATION) {
if (
!isFieldAvailable(field, forResponse) ||
field.type == FieldMetadataType.RELATION
) {
return node;
}

Expand All @@ -95,9 +131,17 @@ const getSchemaComponentsProperties = (
properties: compositeTypeDefinitions
.get(field.type)
?.properties?.reduce((properties, property) => {
if (
property.hidden === true ||
(property.hidden === 'input' && !forResponse) ||
(property.hidden === 'output' && forResponse)
) {
return properties;
}
properties[property.name] = getFieldProperties(
property.type,
property.name,
property.options,
);

return properties;
Expand Down Expand Up @@ -166,76 +210,58 @@ const getRequiredFields = (item: ObjectMetadataEntity): string[] => {

const computeSchemaComponent = ({
item,
withRequired,
forResponse,
}: {
item: ObjectMetadataEntity;
withRequired: boolean;
forResponse: boolean;
}): OpenAPIV3_1.SchemaObject => {
const result = {
type: 'object',
description: item.description,
properties: getSchemaComponentsProperties(item),
example: {},
properties: getSchemaComponentsProperties({ item, forResponse }),
} as OpenAPIV3_1.SchemaObject;

if (!withRequired) {
if (forResponse) {
return result;
}

const requiredFields = getRequiredFields(item);

if (requiredFields?.length) {
result.required = requiredFields;
result.example = requiredFields.reduce(
(example, requiredField) => {
example[requiredField] = '';

return example;
},
{} as Record<string, string>,
);
}

return result;
};

const computeRelationSchemaComponent = ({
item,
withRequired,
forResponse,
}: {
item: ObjectMetadataEntity;
withRequired: boolean;
forResponse: boolean;
}): OpenAPIV3_1.SchemaObject => {
const result = {
description: item.description,
allOf: [
{
$ref: `#/components/schemas/${capitalize(item.nameSingular)}${!withRequired ? ' for Responses' : ''}`,
$ref: `#/components/schemas/${capitalize(item.nameSingular)}${forResponse ? ' for Response' : ''}`,
},
{
type: 'object',
properties: getSchemaComponentsRelationProperties(item),
},
],
example: {},
} as OpenAPIV3_1.SchemaObject;

if (!withRequired) {
if (forResponse) {
return result;
}

const requiredFields = getRequiredFields(item);

if (requiredFields?.length) {
result.required = requiredFields;
result.example = requiredFields.reduce(
(example, requiredField) => {
example[requiredField] = '';

return example;
},
{} as Record<string, string>,
);
}

return result;
Expand All @@ -248,12 +274,12 @@ export const computeSchemaComponents = (
(schemas, item) => {
schemas[capitalize(item.nameSingular)] = computeSchemaComponent({
item,
withRequired: true,
forResponse: false,
});
schemas[capitalize(item.nameSingular) + ' for Responses'] =
computeSchemaComponent({ item, withRequired: false });
schemas[capitalize(item.nameSingular) + ' with Relations for Responses'] =
computeRelationSchemaComponent({ item, withRequired: false });
schemas[capitalize(item.nameSingular) + ' for Response'] =
computeSchemaComponent({ item, forResponse: true });
schemas[capitalize(item.nameSingular) + ' with Relations for Response'] =
computeRelationSchemaComponent({ item, forResponse: true });

return schemas;
},
Expand Down Expand Up @@ -286,7 +312,7 @@ export const computeMetadataSchemaComponents = (
type: 'object',
description: `An object`,
properties: {
dataSourceId: { type: 'string' },
dataSourceId: { type: 'string', format: 'uuid' },
nameSingular: { type: 'string' },
namePlural: { type: 'string' },
labelSingular: { type: 'string' },
Expand All @@ -297,10 +323,16 @@ export const computeMetadataSchemaComponents = (
isRemote: { type: 'boolean' },
isActive: { type: 'boolean' },
isSystem: { type: 'boolean' },
createdAt: { type: 'string' },
updatedAt: { type: 'string' },
labelIdentifierFieldMetadataId: { type: 'string' },
imageIdentifierFieldMetadataId: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' },
updatedAt: { type: 'string', format: 'date-time' },
labelIdentifierFieldMetadataId: {
type: 'string',
format: 'uuid',
},
imageIdentifierFieldMetadataId: {
type: 'string',
format: 'uuid',
},
fields: {
type: 'object',
properties: {
Expand All @@ -318,15 +350,13 @@ export const computeMetadataSchemaComponents = (
},
},
},
example: {},
};
schemas[`${capitalize(item.namePlural)}`] = {
type: 'array',
description: `A list of ${item.namePlural}`,
items: {
$ref: `#/components/schemas/${capitalize(item.nameSingular)}`,
},
example: [{}],
};

return schemas;
Expand All @@ -345,56 +375,54 @@ export const computeMetadataSchemaComponents = (
isActive: { type: 'boolean' },
isSystem: { type: 'boolean' },
isNullable: { type: 'boolean' },
createdAt: { type: 'string' },
updatedAt: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' },
updatedAt: { type: 'string', format: 'date-time' },
fromRelationMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
id: { type: 'string', format: 'uuid' },
relationType: { type: 'string' },
toObjectMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
dataSourceId: { type: 'string' },
id: { type: 'string', format: 'uuid' },
dataSourceId: { type: 'string', format: 'uuid' },
nameSingular: { type: 'string' },
namePlural: { type: 'string' },
isSystem: { type: 'boolean' },
},
},
toFieldMetadataId: { type: 'string' },
toFieldMetadataId: { type: 'string', format: 'uuid' },
},
},
toRelationMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
id: { type: 'string', format: 'uuid' },
relationType: { type: 'string' },
fromObjectMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
dataSourceId: { type: 'string' },
id: { type: 'string', format: 'uuid' },
dataSourceId: { type: 'string', format: 'uuid' },
nameSingular: { type: 'string' },
namePlural: { type: 'string' },
isSystem: { type: 'boolean' },
},
},
fromFieldMetadataId: { type: 'string' },
fromFieldMetadataId: { type: 'string', format: 'uuid' },
},
},
defaultValue: { type: 'object' },
options: { type: 'object' },
},
example: {},
};
schemas[`${capitalize(item.namePlural)}`] = {
type: 'array',
description: `A list of ${item.namePlural}`,
items: {
$ref: `#/components/schemas/${capitalize(item.nameSingular)}`,
},
example: [{}],
};

return schemas;
Expand All @@ -408,8 +436,8 @@ export const computeMetadataSchemaComponents = (
fromObjectMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
dataSourceId: { type: 'string' },
id: { type: 'string', format: 'uuid' },
dataSourceId: { type: 'string', format: 'uuid' },
nameSingular: { type: 'string' },
namePlural: { type: 'string' },
isSystem: { type: 'boolean' },
Expand All @@ -419,26 +447,24 @@ export const computeMetadataSchemaComponents = (
toObjectMetadata: {
type: 'object',
properties: {
id: { type: 'string' },
dataSourceId: { type: 'string' },
id: { type: 'string', format: 'uuid' },
dataSourceId: { type: 'string', format: 'uuid' },
nameSingular: { type: 'string' },
namePlural: { type: 'string' },
isSystem: { type: 'boolean' },
},
},
toObjectMetadataId: { type: 'string' },
fromFieldMetadataId: { type: 'string' },
toFieldMetadataId: { type: 'string' },
toObjectMetadataId: { type: 'string', format: 'uuid' },
fromFieldMetadataId: { type: 'string', format: 'uuid' },
toFieldMetadataId: { type: 'string', format: 'uuid' },
},
example: {},
};
schemas[`${capitalize(item.namePlural)}`] = {
type: 'array',
description: `A list of ${item.namePlural}`,
items: {
$ref: `#/components/schemas/${capitalize(item.nameSingular)}`,
},
example: [{}],
};
}
}
Expand Down
Loading

0 comments on commit d85c3a0

Please sign in to comment.