Skip to content

Commit

Permalink
refactor(test): add test for GQL pre and post processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Aug 29, 2024
1 parent f7ddb3a commit dd43f11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
12 changes: 6 additions & 6 deletions packages/facade/src/gql/protos/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const preProcessGQLInput = async (
data: any,
model: GraphQLInputObjectType | GraphQLEnumType | GraphQLInputField | GraphQLInputType
): Promise<any> => {
if (data === undefined) {
if (data === null || data === undefined) {
return undefined;
}

Expand Down Expand Up @@ -96,8 +96,8 @@ export const preProcessGQLInput = async (
return data;
};

export const postProcessGQLValue = (data: any, model: GraphQLOutputType): any => {
if (data === undefined) {
export const postProcessGQLOutput = (data: any, model: GraphQLOutputType): any => {
if (data === null || data === undefined) {
return undefined;
}

Expand All @@ -120,7 +120,7 @@ export const postProcessGQLValue = (data: any, model: GraphQLOutputType): any =>
const converted = Object.keys(fields).filter(
key => key in data
).map(
key => postProcessGQLValue(data[key], fields[key].type)
key => postProcessGQLOutput(data[key], fields[key].type)
);
return {
...data,
Expand All @@ -130,12 +130,12 @@ export const postProcessGQLValue = (data: any, model: GraphQLOutputType): any =>
}

if (model instanceof GraphQLNonNull) {
return postProcessGQLValue(data, model.ofType);
return postProcessGQLOutput(data, model.ofType);
}

if (model instanceof GraphQLList) {
return data.map(
(d: any) => postProcessGQLValue(d, model.ofType)
(d: any) => postProcessGQLOutput(d, model.ofType)
);
}

Expand Down
8 changes: 5 additions & 3 deletions packages/facade/src/gql/protos/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
import flat from 'array.prototype.flat';
import { getTyping } from './registry.js';
import {
getWhitelistBlacklistConfig, Mutate, postProcessGQLValue,
getWhitelistBlacklistConfig,
Mutate,
postProcessGQLOutput,
preProcessGQLInput,
} from './graphql.js';
import {
Expand All @@ -27,7 +29,7 @@ import {
import { ReadRequest } from '@restorecommerce/rc-grpc-clients';
import {
Filter_Operation,
Filter_ValueType, FilterOp_Operator
Filter_ValueType
} from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/resource_base.js';
import { type DescriptorProto, type ServiceDescriptorProto } from 'ts-proto-descriptors';
import { type GraphQLResolveInfo } from 'graphql';
Expand Down Expand Up @@ -196,7 +198,7 @@ export const getGQLResolverFunctions = <
'origin-ip': contextRequest.client.address().address
})
});
const result = postProcessGQLValue(rawResult, outputTyping.output);
const result = postProcessGQLOutput(rawResult, outputTyping.output);

const grpcClientConfig = cfg.client;
const bufferFields = getKeys((grpcClientConfig as any)?.bufferFields);
Expand Down
24 changes: 24 additions & 0 deletions packages/facade/tests/protos.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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';
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'

describe('proto-meta', () => {
it('should register typings', () => {
Expand Down Expand Up @@ -33,4 +37,24 @@ describe('proto-meta', () => {

expect(fields.meta.type).toEqual(getTyping('.io.restorecommerce.meta.Meta')!.output);
});

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 preProcessed = await preProcessGQLInput(input, model.input);
const postProcessed = postProcessGQLOutput(preProcessed, model.output);

expect(preProcessed).toEqual(postProcessed);
})
});

0 comments on commit dd43f11

Please sign in to comment.