diff --git a/packages/facade/src/gql/protos/graphql.ts b/packages/facade/src/gql/protos/graphql.ts index c784ca0e..1dc961b2 100644 --- a/packages/facade/src/gql/protos/graphql.ts +++ b/packages/facade/src/gql/protos/graphql.ts @@ -22,7 +22,7 @@ export const preProcessGQLInput = async ( model: GraphQLInputObjectType | GraphQLEnumType | GraphQLInputField | GraphQLInputType ): Promise => { if (data === null || data === undefined) { - return undefined; + return data; } if (model instanceof GraphQLEnumType) { @@ -47,14 +47,17 @@ export const preProcessGQLInput = async ( }; } else { const fields = model.getFields(); - await Promise.all( + const converted = Object.assign({}, ...(await Promise.all( Object.keys(fields).filter( key => key in data ).map( - async key => data[key] = await preProcessGQLInput(data[key], fields[key].type) + async key => ({ [key]: await preProcessGQLInput(data[key], fields[key].type) }) ) - ); - return data; + ))); + return { + ...data, + ...converted, + } } } @@ -95,7 +98,7 @@ export const preProcessGQLInput = async ( export const postProcessGQLOutput = (data: any, model: GraphQLOutputType): any => { if (data === null || data === undefined) { - return undefined; + return data; } if (model instanceof GraphQLEnumType) { @@ -105,7 +108,7 @@ export const postProcessGQLOutput = (data: any, model: GraphQLOutputType): any = if (model instanceof GraphQLObjectType) { if (model.name === 'GoogleProtobufAny') { // TODO Use encoded once resource base supports it - const decoded = JSON.parse((data?.value as Buffer).toString()); + const decoded = JSON.parse(data?.value?.toString()); return { ...data, @@ -113,12 +116,15 @@ export const postProcessGQLOutput = (data: any, model: GraphQLOutputType): any = }; } else { const fields = model.getFields(); - Object.keys(fields).filter( + const converted = Object.assign({}, ...Object.keys(fields).filter( key => key in data ).map( - key => data[key] = postProcessGQLOutput(data[key], fields[key].type) - ); - return data; + key => ({ [key]: postProcessGQLOutput(data[key], fields[key].type) }) + )); + return { + ...data, + ...converted, + }; } } diff --git a/packages/facade/tests/protos.spec.ts b/packages/facade/tests/protos.spec.ts index d04c256b..43cccd0d 100644 --- a/packages/facade/tests/protos.spec.ts +++ b/packages/facade/tests/protos.spec.ts @@ -2,8 +2,8 @@ import { protoMetadata, } from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/order.js'; import { - Meta -} from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/meta.js'; + User +} from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/user.js'; import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLScalarType } from 'graphql'; import { getTyping, registerPackagesRecursive } from '../src/gql/protos/index.js'; import { preProcessGQLInput, postProcessGQLOutput } from '../src/gql/protos/graphql.js' @@ -39,22 +39,36 @@ describe('proto-meta', () => { }); it('should pre/post-process input/output correctly', async () =>{ - const model = getTyping('.io.restorecommerce.meta.Meta'); - const input: Meta = { - created: new Date(), - createdBy: 'user', - owners: [ - { - id: 'some-type-identifier', - value: 'user' - } - ], - acls: null, + const model = getTyping('.io.restorecommerce.user.User'); + const input: User = { + id: 'user_1_test', + firstName: 'Markus', + lastName: 'Muster', + data: { + typeUrl: 'to.neverland', + value: { + birthday: new Date('1987-06-05'), + } as any + }, + meta: { + created: '2024-08-30' as any, + createdBy: 'user', + owners: [ + { + id: 'some-type-identifier', + value: 'user' + } + ], + acls: null, + } + }; const preProcessed = await preProcessGQLInput(input, model.input); const postProcessed = postProcessGQLOutput(preProcessed, model.output); - expect(preProcessed).toEqual(postProcessed); + expect(preProcessed.meta.created).toBeInstanceOf(Date); + expect(preProcessed.data.value).toBeInstanceOf(Buffer); + expect(postProcessed.data.value.birthday).toBeDefined(); }) });