Skip to content

fix: Handle deep nested falsy values in result #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions src/__tests__/falsykeys.tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import { expect } from 'chai';
import { jsonToGraphQLQuery } from '../';

describe('jsonToGraphQLQuery() - falsy keys', () => {

it('does not include fields which value is false', () => {
const query = {
query: {
Expand Down Expand Up @@ -82,4 +80,86 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
);
});

it('Includes the nested object if includeFalsyKeys is true', () => {
const query = {
query: {
Posts: {
id: true,
name: false
},
Lorem: {
Ipsum: {
name: false
}
}
}
};
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
'query { Posts { id name } Lorem { Ipsum { name } } }'
);
});

it('does not include the object if nested object has falsy values', () => {
const query = {
query: {
Posts: {
id: true,
name: false
},
Lorem: {
Ipsum: {
name: false
}
}
}
};
expect(jsonToGraphQLQuery(query)).to.equal('query { Posts { id } }');
});

it('skip objects when deeply nested keys contain falsy values', () => {
const query = {
query: {
id: true,
Posts: {
id: true,
name: false
},
Lorem: {
Ipsum: {
Dolor: {
Sit: {
amet: false
}
}
},
details: {
name: false,
address: true
}
}
}
};
expect(jsonToGraphQLQuery(query)).to.equal(
'query { id Posts { id } Lorem { details { address } } }'
);
});

it('Include values if nested object has falsy values and includeFalsyKeys is true', () => {
const query = {
query: {
Posts: {
id: true,
name: false
},
Lorem: {
Ipsum: {
name: false
}
}
}
};
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
'query { Posts { id name } Lorem { Ipsum { name } } }'
);
});
});
15 changes: 7 additions & 8 deletions src/__tests__/mutations.tests.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@

import { expect } from 'chai';
import { jsonToGraphQLQuery } from '../';

describe('jsonToGraphQLQuery() - mutations', () => {

it('simple mutation', () => {
const mutation = {
mutation: {
delete_post: {
__args: { id: 1234 },
id: true,
id: true
}
}
};
expect(jsonToGraphQLQuery(mutation, { pretty: true })).to.equal(
`mutation {
`mutation {
delete_post (id: 1234) {
id
}
}`);
})
}`
);
});

it('correctly converts mutations with no specified return fields', () => {
const query = {
Expand All @@ -35,7 +34,7 @@ describe('jsonToGraphQLQuery() - mutations', () => {
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`mutation {
create_post (title: "My Awesome Post", body: "This post is awesome!")
}`);
}`
);
});

});
75 changes: 37 additions & 38 deletions src/__tests__/name.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ describe('jsonToGraphQLQuery() - name', () => {
lorem: {
__aliasFor: 'Posts',
__args: {
arg1: 20,
arg1: 20
},
id: true,
id: true
},
larem: {
__aliasFor: 'Posts',
__args: {
arg2: 10,
arg2: 10
},
id: true,
},
},
id: true
}
}
};
expect(jsonToGraphQLQuery(query)).to.equal(
'query NewName { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
Expand All @@ -34,18 +34,18 @@ describe('jsonToGraphQLQuery() - name', () => {
one: {
__aliasFor: 'Posts',
__args: {
arg1: 20,
arg1: 20
},
id: true,
id: true
},
two: {
__aliasFor: 'Posts',
__args: {
arg2: 10,
arg2: 10
},
id: true,
},
},
id: true
}
}
};
expect(jsonToGraphQLQuery(query)).to.equal(
'mutation NewName { one: Posts (arg1: 20) { id } two: Posts (arg2: 10) { id } }'
Expand All @@ -62,12 +62,12 @@ describe('jsonToGraphQLQuery() - combinations', () => {
Posts: {
__args: {
arg1: 20,
arg2: new VariableType('variable1'),
arg2: new VariableType('variable1')
},
id: true,
title: true,
},
},
title: true
}
}
};
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query NewName {
Expand All @@ -79,24 +79,23 @@ describe('jsonToGraphQLQuery() - combinations', () => {
);
});


it('correctly converts query with name/variables', () => {
const query = {
query: {
__name: 'NewName',
__variables: {
variable1: 'String!',
variableWithDefault: 'String = "default_value"',
variableWithDefault: 'String = "default_value"'
},
Posts: {
__args: {
arg1: 20,
arg2: new VariableType('variable1'),
arg2: new VariableType('variable1')
},
id: true,
title: true,
},
},
title: true
}
}
};
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query NewName ($variable1: String!, $variableWithDefault: String = "default_value") {
Expand All @@ -114,65 +113,65 @@ describe('jsonToGraphQLQuery() - combinations', () => {
__name: 'NewName',
__variables: {
someString: 'String!',
varWithDefault: 'String = "default_value"',
varWithDefault: 'String = "default_value"'
},
one: {
__aliasFor: 'Posts',
__args: {
arg1: 20,
arg2: new VariableType('someString'),
status: new EnumType('PUBLISHED'),
status: new EnumType('PUBLISHED')
},
name: false,
id: true,
title: true,
comments: {
__args: {
offensiveOnly: true,
offensiveOnly: true
},
id: true,
comment: true,
user: true,
},
user: true
}
},
Post: {
__args: {
arg1: 20,
arg2: new VariableType('someString'),
arg2: new VariableType('someString')
},
__on: {
__typeName: 'ConfigurablePost',
id: true,
id: true
},
name: false,
title: true,
comments: {
__args: {
offensiveOnly: true,
offensiveOnly: true
},
id: true,
comment: true,
user: true,
},
user: true
}
},
Posts: {
__args: {
arg1: 20,
arg2: new VariableType('someString'),
arg2: new VariableType('someString')
},
name: false,
id: true,
title: true,
comments: {
__args: {
offensiveOnly: true,
offensiveOnly: true
},
id: true,
comment: true,
user: true,
},
},
},
user: true
}
}
}
};
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query NewName ($someString: String!, $varWithDefault: String = "default_value") {
Expand Down
Loading