fix: format graphql localization input type (#932)

* fix: format locales for graphql

* fix: locale missing from graphql mutation args
This commit is contained in:
Dan Ribbens
2022-08-15 11:42:29 -04:00
committed by GitHub
parent 78630cafa2
commit 1c7445dc7f
5 changed files with 73 additions and 11 deletions

View File

@@ -177,6 +177,9 @@ function initCollectionsGraphQL(payload: Payload): void {
args: { args: {
data: { type: collection.graphQL.mutationInputType }, data: { type: collection.graphQL.mutationInputType },
draft: { type: GraphQLBoolean }, draft: { type: GraphQLBoolean },
...(payload.config.localization ? {
locale: { type: payload.types.localeInputType },
} : {}),
}, },
resolve: createResolver(collection), resolve: createResolver(collection),
}; };
@@ -188,6 +191,9 @@ function initCollectionsGraphQL(payload: Payload): void {
data: { type: collection.graphQL.updateMutationInputType }, data: { type: collection.graphQL.updateMutationInputType },
draft: { type: GraphQLBoolean }, draft: { type: GraphQLBoolean },
autosave: { type: GraphQLBoolean }, autosave: { type: GraphQLBoolean },
...(payload.config.localization ? {
locale: { type: payload.types.localeInputType },
} : {}),
}, },
resolve: updateResolver(collection), resolve: updateResolver(collection),
}; };

View File

@@ -62,6 +62,9 @@ function initGlobalsGraphQL(payload: Payload): void {
args: { args: {
data: { type: global.graphQL.mutationInputType }, data: { type: global.graphQL.mutationInputType },
draft: { type: GraphQLBoolean }, draft: { type: GraphQLBoolean },
...(payload.config.localization ? {
locale: { type: payload.types.localeInputType },
} : {}),
}, },
resolve: updateResolver(global), resolve: updateResolver(global),
}; };

View File

@@ -1,11 +1,12 @@
import { GraphQLEnumType } from 'graphql'; import { GraphQLEnumType } from 'graphql';
import { LocalizationConfig } from '../../config/types'; import { LocalizationConfig } from '../../config/types';
import formatName from '../utilities/formatName';
const buildFallbackLocaleInputType = (localization: LocalizationConfig): GraphQLEnumType => new GraphQLEnumType({ const buildFallbackLocaleInputType = (localization: LocalizationConfig): GraphQLEnumType => new GraphQLEnumType({
name: 'FallbackLocaleInputType', name: 'FallbackLocaleInputType',
values: [...localization.locales, 'none'].reduce((values, locale) => ({ values: [...localization.locales, 'none'].reduce((values, locale) => ({
...values, ...values,
[locale]: { [formatName(locale)]: {
value: locale, value: locale,
}, },
}), {}), }), {}),

View File

@@ -1,14 +1,17 @@
import { GraphQLEnumType } from 'graphql'; import { GraphQLEnumType, GraphQLScalarType } from 'graphql';
import { LocalizationConfig } from '../../config/types'; import { LocalizationConfig } from '../../config/types';
import formatName from '../utilities/formatName';
const buildLocaleInputType = (localization: LocalizationConfig): GraphQLEnumType => new GraphQLEnumType({ const buildLocaleInputType = (localization: LocalizationConfig): GraphQLEnumType | GraphQLScalarType => {
name: 'LocaleInputType', return new GraphQLEnumType({
values: localization.locales.reduce((values, locale) => ({ name: 'LocaleInputType',
...values, values: localization.locales.reduce((values, locale) => ({
[locale]: { ...values,
value: locale, [formatName(locale)]: {
}, value: locale,
}), {}), },
}); }), {}),
});
};
export default buildLocaleInputType; export default buildLocaleInputType;

View File

@@ -561,6 +561,55 @@ describe('Localization', () => {
expect(typeof result.user.relation.title).toStrictEqual('string'); expect(typeof result.user.relation.title).toStrictEqual('string');
}); });
it('should create and update collections', async () => {
const url = `${serverURL}${config.routes.api}${config.routes.graphQL}`;
const client = new GraphQLClient(url);
const create = `mutation {
createLocalizedPost(
data: {
title: "${englishTitle}"
}
locale: ${defaultLocale}
) {
id
title
}
}`;
const { createLocalizedPost: createResult } = await client.request(create, null, {
Authorization: `JWT ${token}`,
});
const update = `mutation {
updateLocalizedPost(
id: "${createResult.id}",
data: {
title: "${spanishTitle}"
}
locale: ${spanishLocale}
) {
title
}
}`;
const { updateLocalizedPost: updateResult } = await client.request(update, null, {
Authorization: `JWT ${token}`,
});
const result = await payload.findByID({
collection: slug,
id: createResult.id,
locale: 'all',
});
expect(createResult.title).toStrictEqual(englishTitle);
expect(updateResult.title).toStrictEqual(spanishTitle);
expect(result.title[defaultLocale]).toStrictEqual(englishTitle);
expect(result.title[spanishLocale]).toStrictEqual(spanishTitle);
});
}); });
}); });