Skip to content

Commit

Permalink
feat: add raw localizations to meta types, fields and enums
Browse files Browse the repository at this point in the history
Currently, it is only possible to query localizations by
explicitly specifying the target language for the client.
There are also use-cases where the localization is not
immediately used in a client, but needed to be processed
in another system. In this case it is convenient to be
able to query the translations in all locales in a single
request.
  • Loading branch information
Etienne-Buschong committed Mar 20, 2024
1 parent 6b45cac commit 65445d6
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 10 deletions.
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
163 changes: 163 additions & 0 deletions spec/meta-schema/meta-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ 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
}
}
}
`;

const permissionsQuery = gql`
{
rootEntityType(name: "Shipment") {
Expand Down Expand Up @@ -349,6 +374,36 @@ describe('Meta schema API', () => {
},
},
},
Shipment: {
label: 'Lieferung',
labelPlural: 'Lieferungen',
hint: 'Eine Lieferung',
fields: {
transportKind: {
label: 'Transportart',
hint: 'Transportart 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 +417,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 +1024,84 @@ describe('Meta schema API', () => {
});
});

it('can query raw localization of types', async () => {
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',
});

// 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',
});
});

it('can query permissions', async () => {
const result = await execute(permissionsQuery, { authRoles: ['user'] });
expect(result!.rootEntityType).to.deep.equal({
Expand Down
168 changes: 168 additions & 0 deletions spec/model/create-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,172 @@ describe('createModel', () => {
expect(field.isFlexSearchIndexed).to.be.true;
expect(field.isIncludedInSearch).to.be.false;
});

it('it allows to access raw label/labelPlural/hint localization on type "ObjectType" and its fields', () => {
const document: DocumentNode = gql`
type Shipment @rootEntity {
shipmentNumber: String
}
`;
const model = createSimpleModel(document, {
de: {
namespacePath: [],
types: {
Shipment: {
label: 'Lieferung',
labelPlural: 'Lieferungen',
hint: 'Eine Lieferung',
fields: {
shipmentNumber: {
label: 'Lieferungsnummer',
hint: 'Die Nummer der Lieferung',
},
},
},
},
},
en: {
namespacePath: [],
types: {
Shipment: {
label: 'Shipment',
labelPlural: 'Shipments',
hint: 'A shipment',
fields: {
shipmentNumber: {
label: 'Shipment number',
hint: 'The number of the shipment',
},
},
},
},
},
});
const shipmentType = model.getObjectTypeOrThrow('Shipment');
expect(shipmentType.label).to.deep.equal({
de: 'Lieferung',
en: 'Shipment',
});
expect(shipmentType.labelPlural).to.deep.equal({
de: 'Lieferungen',
en: 'Shipments',
});
expect(shipmentType.hint).to.deep.equal({
de: 'Eine Lieferung',
en: 'A shipment',
});
const shipmentNumberField = shipmentType.getFieldOrThrow('shipmentNumber');
expect(shipmentNumberField.label).to.deep.equal({
de: 'Lieferungsnummer',
en: 'Shipment number',
});
expect(shipmentNumberField.hint).to.deep.equal({
de: 'Die Nummer der Lieferung',
en: 'The number of the shipment',
});
});

it('it allows to access raw label/labelPlural/hint localization on type "EnumType" and its values', () => {
const document: DocumentNode = gql`
type Shipment @rootEntity {
transportKind: TransportKind
}
enum TransportKind {
AIR
SEA
ROAD
}
`;
const model = createSimpleModel(document, {
de: {
namespacePath: [],
types: {
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',
},
},
},
},
},
en: {
namespacePath: [],
types: {
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',
},
},
},
},
},
});
const transportKindType = model.getEnumTypeOrThrow('TransportKind');
expect(transportKindType.label).to.deep.equal({
de: 'Transportart',
en: 'Transport kind',
});
expect(transportKindType.labelPlural).to.deep.equal({
de: 'Transportarten',
en: 'Transport kinds',
});
expect(transportKindType.hint).to.deep.equal({
de: 'Die Art des Transports',
en: 'The kind of transport',
});
const valueAIR = transportKindType.values.find((value) => value.value === 'AIR');
const valueROAD = transportKindType.values.find((value) => value.value === 'ROAD');
const valueSEA = transportKindType.values.find((value) => value.value === 'SEA');
expect(valueAIR?.label).to.deep.equal({
de: 'Luft',
en: 'Air',
});
expect(valueAIR?.hint).to.deep.equal({
de: 'Lieferung mittels Fluchtfracht',
en: 'Delivery via airfreight',
});
expect(valueROAD?.label).to.deep.equal({
de: 'Straße',
en: 'Road',
});
expect(valueROAD?.hint).to.deep.equal({
de: 'Lieferung mittels LKW',
en: 'Delivery via truck',
});
expect(valueSEA?.label).to.deep.equal({
de: 'Übersee',
en: 'Sea',
});
expect(valueSEA?.hint).to.deep.equal({
de: 'Lieferung mittels Schiff',
en: 'Delivery via ship',
});
});
});
Loading

0 comments on commit 65445d6

Please sign in to comment.