Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add raw localizations to meta types, fields and enums #314

Merged
merged 1 commit into from
Mar 21, 2024
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
15 changes: 15 additions & 0 deletions spec/dev/model/i18n.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
i18n:
en:
types:
Morality:
label: Morality
labelPlural: Morality
hint: The morality of the hero
values:
GOOD: Good
EVIL: Evil
Hero:
label: Hero
labelPlural: Heroes
Expand All @@ -22,6 +29,14 @@ i18n:
id: ID
de:
types:
Morality:
label: Moral
labelPlural: Moral
hint: Die Moral des Helden
values:
GOOD: Gut
EVIL: Böse

Hero:
label: Held
labelPlural: Helden
Expand Down
194 changes: 193 additions & 1 deletion spec/meta-schema/meta-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { DocumentNode, graphql, print } from 'graphql';
import gql from 'graphql-tag';
import { CRUDDL_VERSION } from '../../src/cruddl-version';
import {
ExecutionOptions,
ExecutionOptionsCallbackArgs,
Expand All @@ -9,7 +10,6 @@ import { getMetaSchema } from '../../src/meta-schema/meta-schema';
import { AggregationOperator, Model, TypeKind } from '../../src/model';
import { Project } from '../../src/project/project';
import { stopMetaServer } from '../dev/server';
import { CRUDDL_VERSION } from '../../src/cruddl-version';

describe('Meta schema API', () => {
const introQuery = gql`
Expand Down Expand Up @@ -143,6 +143,36 @@ describe('Meta schema API', () => {
}
`;

const rawI18nQuery = gql`
{
rootEntityType(name: "Shipment") {
label
labelPlural
hint
fields {
name
label
hint
}
}
enumType(name: "TransportKind") {
label
labelPlural
hint
values {
value
label
hint
}
}
valueObjectType(name: "Address") {
label
labelPlural
hint
}
}
`;

const permissionsQuery = gql`
{
rootEntityType(name: "Shipment") {
Expand Down Expand Up @@ -349,6 +379,40 @@ describe('Meta schema API', () => {
},
},
},
Shipment: {
label: 'Lieferung',
labelPlural: 'Lieferungen',
hint: 'Eine Lieferung',
fields: {
transportKind: {
label: 'Transportart',
hint: 'Transportart der Lieferung',
},
handlingUnits: {
label: 'Packstücke',
hint: 'Die Packstücke der Lieferung'
}
},
},
TransportKind: {
label: 'Transportart',
labelPlural: 'Transportarten',
hint: 'Die Art des Transports',
values: {
AIR: {
label: 'Luft',
hint: 'Lieferung mittels Fluchtfracht',
},
ROAD: {
label: 'Straße',
hint: 'Lieferung mittels LKW',
},
SEA: {
label: 'Übersee',
hint: 'Lieferung mittels Schiff',
},
},
},
},
},
{
Expand All @@ -362,6 +426,36 @@ describe('Meta schema API', () => {
},
},
},
Shipment: {
label: 'Shipment',
labelPlural: 'Shipments',
hint: 'A shipment',
fields: {
transportKind: {
label: 'Transport kind',
hint: 'The kind of transport for the shipment',
},
},
},
TransportKind: {
label: 'Transport kind',
labelPlural: 'Transport kinds',
hint: 'The kind of transport',
values: {
AIR: {
label: 'Air',
hint: 'Delivery via airfreight',
},
ROAD: {
label: 'Road',
hint: 'Delivery via truck',
},
SEA: {
label: 'Sea',
hint: 'Delivery via ship',
},
},
},
},
},
],
Expand Down Expand Up @@ -939,6 +1033,104 @@ describe('Meta schema API', () => {
});
});

it('can query raw localization of types', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test for a field where only one of the languages is present, to make sure the value omits this key then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test for a field on the root entity type and a value object, both with only german translations

const result = (await execute(rawI18nQuery)) as any;

// test for object types including fields
const shipmentType = result.rootEntityType;
expect(shipmentType.label).to.deep.equal({
en: 'Shipment',
de: 'Lieferung',
});
expect(shipmentType.labelPlural).to.deep.equal({
en: 'Shipments',
de: 'Lieferungen',
});
expect(shipmentType.hint).to.deep.equal({
en: 'A shipment',
de: 'Eine Lieferung',
});
const transportKindField = shipmentType.fields.find(
(field: any) => field.name === 'transportKind',
);
expect(transportKindField.label).to.deep.equal({
en: 'Transport kind',
de: 'Transportart',
});
expect(transportKindField.hint).to.deep.equal({
en: 'The kind of transport for the shipment',
de: 'Transportart der Lieferung',
});
const handlingUnitsField = shipmentType.fields.find(
(field: any) => field.name === 'handlingUnits'
);
expect(handlingUnitsField.label).to.deep.equal({
de: 'Packstücke'
});
expect(handlingUnitsField.hint).to.deep.equal({
de: 'Die Packstücke der Lieferung'
});

// test for enumType including values
const transportKindType = result.enumType;
expect(transportKindType.label).to.deep.equal({
en: 'Transport kind',
de: 'Transportart',
});
expect(transportKindType.labelPlural).to.deep.equal({
en: 'Transport kinds',
de: 'Transportarten',
});
expect(transportKindType.hint).to.deep.equal({
en: 'The kind of transport',
de: 'Die Art des Transports',
});
const transportKindValueAir = transportKindType.values.find(
(value: any) => value.value === 'AIR',
);
const transportKindValueRoad = transportKindType.values.find(
(value: any) => value.value === 'ROAD',
);
const transportKindValueSea = transportKindType.values.find(
(value: any) => value.value === 'SEA',
);
expect(transportKindValueAir.label).to.deep.equal({
en: 'Air',
de: 'Luft',
});
expect(transportKindValueAir.hint).to.deep.equal({
en: 'Delivery via airfreight',
de: 'Lieferung mittels Fluchtfracht',
});
expect(transportKindValueRoad.label).to.deep.equal({
de: 'Straße',
en: 'Road',
});
expect(transportKindValueRoad.hint).to.deep.equal({
de: 'Lieferung mittels LKW',
en: 'Delivery via truck',
});
expect(transportKindValueSea.label).to.deep.equal({
de: 'Übersee',
en: 'Sea',
});
expect(transportKindValueSea.hint).to.deep.equal({
de: 'Lieferung mittels Schiff',
en: 'Delivery via ship',
});

const adressType = result.valueObjectType;
expect(adressType.label).to.deep.equal({
de: 'Adresse'
});
expect(adressType.labelPlural).to.deep.equal({
de: 'Adressen'
});
expect(adressType.hint).to.deep.equal({
de: 'Eine Adresse'
});
});

it('can query permissions', async () => {
const result = await execute(permissionsQuery, { authRoles: ['user'] });
expect(result!.rootEntityType).to.deep.equal({
Expand Down
Loading
Loading