Skip to content

Commit

Permalink
Write null & undefined fields (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmarks committed Jun 21, 2023
1 parent 478d439 commit c8c62d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/graphql/query-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {EnumType, jsonToGraphQLQuery} from 'json-to-graphql-query';
import {isNil} from 'lodash';

import {ConflictClause, Mutation, MutationObject} from './types';

Expand All @@ -8,7 +9,9 @@ type MutationFieldValue =
| boolean
| any[]
| {category: string; detail: string}
| Ref;
| Ref
| undefined
| null;

interface MutationFields {
[field: string]: MutationFieldValue;
Expand Down Expand Up @@ -57,13 +60,14 @@ export class QueryBuilder {
*/
private mutationObj(model: FarosModel, ref = false): MutationObject {
const [modelName, fields] = Object.entries(model)[0];
const cleanObj = removeUndefinedProperties(fields ?? {});
const mutObj: any = {};
const mask = ['refreshedAt'];

for (const [k, v] of Object.entries(cleanObj)) {
for (const [k, v] of Object.entries(fields ?? {})) {
let maskKey = k;
if (v instanceof Ref) {
if (isNil(v)) {
mutObj[k] = null;
} else if (v instanceof Ref) {
mutObj[k] = this.mutationObj(v.model, true);
// ref's key should be suffixed with Id for onConflict field
maskKey += 'Id';
Expand Down Expand Up @@ -112,15 +116,7 @@ export class QueryBuilder {
}

export function mask(object: any): string[] {
return Object.keys(removeUndefinedProperties(object));
}

function removeUndefinedProperties(object: MutationFields): MutationFields {
const result = {...object};
Object.keys(result).forEach(
(key) => result[key] === undefined && delete result[key]
);
return result;
return Object.keys(object);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/query-builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports[`query builder creates mutations 1`] = `"mutation { m0: insert_compute_Application_one (object: {name: \\"<application_name>\\", platform: \\"<application_platform>\\", origin: \\"test-origin\\"}, on_conflict: {constraint: compute_Application_pkey, update_columns: [refreshedAt, name, platform, origin]}) { id } m1: insert_cicd_Organization_one (object: {uid: \\"<organization_uid>\\", source: \\"<organization_source>\\", origin: \\"test-origin\\"}, on_conflict: {constraint: cicd_Organization_pkey, update_columns: [refreshedAt, uid, source, origin]}) { id } m2: insert_cicd_Pipeline_one (object: {uid: \\"<pipeline_uid>\\", organization: {data: {uid: \\"<organization_uid>\\", source: \\"<organization_source>\\"}, on_conflict: {constraint: cicd_Organization_pkey, update_columns: [refreshedAt]}}, origin: \\"test-origin\\"}, on_conflict: {constraint: cicd_Pipeline_pkey, update_columns: [refreshedAt, uid, organizationId, origin]}) { id } m3: insert_cicd_Build_one (object: {uid: \\"<cicd_Build>\\", pipeline: {data: {uid: \\"<pipeline_uid>\\", organization: {data: {uid: \\"<organization_uid>\\", source: \\"<organization_source>\\"}, on_conflict: {constraint: cicd_Organization_pkey, update_columns: [refreshedAt]}}}, on_conflict: {constraint: cicd_Pipeline_pkey, update_columns: [refreshedAt]}}, name: \\"<build_name>\\", origin: \\"test-origin\\"}, on_conflict: {constraint: cicd_Build_pkey, update_columns: [refreshedAt, uid, pipelineId, name, origin]}) { id } m4: insert_cicd_Deployment_one (object: {uid: \\"<deployment_uid\\", source: \\"<deployment_source>\\", application: {data: {name: \\"<application_name>\\", platform: \\"<application_platform>\\"}, on_conflict: {constraint: compute_Application_pkey, update_columns: [refreshedAt]}}, build: {data: {uid: \\"<cicd_Build>\\", pipeline: {data: {uid: \\"<pipeline_uid>\\", organization: {data: {uid: \\"<organization_uid>\\", source: \\"<organization_source>\\"}, on_conflict: {constraint: cicd_Organization_pkey, update_columns: [refreshedAt]}}}, on_conflict: {constraint: cicd_Pipeline_pkey, update_columns: [refreshedAt]}}, name: \\"<build_name>\\"}, on_conflict: {constraint: cicd_Build_pkey, update_columns: [refreshedAt]}}, status: {category: \\"Success\\", detail: \\"<status_detail>\\"}, origin: \\"test-origin\\"}, on_conflict: {constraint: cicd_Deployment_pkey, update_columns: [refreshedAt, uid, source, applicationId, buildId, status, origin]}) { id } }"`;

exports[`query builder creates mutations with non-model objects 1`] = `"mutation { m0: insert_qa_TestCase_one (object: {uid: \\"<uid>\\", source: \\"<source>\\", name: \\"<name>\\", before: [{description: \\"<description>\\", condition: \\"<condition>\\"}], after: [{description: \\"<description>\\", condition: \\"<condition>\\"}], tags: \\"{tag1,tag2}\\", origin: \\"test-origin\\"}, on_conflict: {constraint: qa_TestCase_pkey, update_columns: [refreshedAt, uid, source, name, before, after, tags, origin]}) { id } }"`;

exports[`query builder creates mutations with undefined and null fields 1`] = `"mutation { m0: insert_qa_TestCase_one (object: {uid: \\"<uid>\\", source: \\"<source>\\", name: \\"<name>\\", before: null, after: null, tags: \\"{tag1,tag2}\\", origin: \\"test-origin\\"}, on_conflict: {constraint: qa_TestCase_pkey, update_columns: [refreshedAt, uid, source, name, before, after, tags, origin]}) { id } }"`;
17 changes: 17 additions & 0 deletions test/query-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ describe('query builder', () => {
const queryString = sut.batchMutation(mutations);
expect(queryString).toMatchSnapshot();
});

test('creates mutations with undefined and null fields', () => {
const qb = new sut.QueryBuilder(ORIGIN);

const qa_TestCase = {
uid: '<uid>',
source: '<source>',
name: '<name>',
before: null,
after: undefined,
tags: ['tag1', 'tag2'],
};

const mutations = [qb.upsert({qa_TestCase})];
const queryString = sut.batchMutation(mutations);
expect(queryString).toMatchSnapshot();
});
});

0 comments on commit c8c62d2

Please sign in to comment.