Skip to content

Commit

Permalink
fix(facade): improve proto processor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Aug 30, 2024
1 parent 7bcb80c commit eeb340b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
28 changes: 17 additions & 11 deletions packages/facade/src/gql/protos/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const preProcessGQLInput = async (
model: GraphQLInputObjectType | GraphQLEnumType | GraphQLInputField | GraphQLInputType
): Promise<any> => {
if (data === null || data === undefined) {
return undefined;
return data;
}

if (model instanceof GraphQLEnumType) {
Expand All @@ -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,
}
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -105,20 +108,23 @@ 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,
value: decoded
};
} 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,
};
}
}

Expand Down
42 changes: 28 additions & 14 deletions packages/facade/tests/protos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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();
})
});

0 comments on commit eeb340b

Please sign in to comment.