Skip to content

Commit fb50bb2

Browse files
MorganDbsMorgan DUBOIS
andauthored
Fixing empty variables, empty directives (#47)
* Fix empty var, empty directives Co-authored-by: Morgan DUBOIS <[email protected]>
1 parent 00013a6 commit fb50bb2

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/__tests__/directives.tests.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ describe('jsonToGraphQLQuery() - directives', () => {
105105
);
106106
});
107107

108+
it('converts a simple query with args and multiple directives but a directive has an empty object', () => {
109+
const query = {
110+
query: {
111+
Posts: {
112+
__args: {
113+
where: {
114+
id: 10,
115+
},
116+
orderBy: 'flibble',
117+
},
118+
__directives: {
119+
client: true,
120+
withArgs: {},
121+
},
122+
id: true,
123+
title: true,
124+
post_date: true,
125+
},
126+
},
127+
} as any;
128+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
129+
`query {
130+
Posts (where: {id: 10}, orderBy: "flibble") @client @withArgs {
131+
id
132+
title
133+
post_date
134+
}
135+
}`
136+
);
137+
});
138+
108139
it('converts a complex query with multiple directives', () => {
109140
const query = {
110141
query: {

src/__tests__/name.tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ describe('jsonToGraphQLQuery() - name', () => {
5454
});
5555

5656
describe('jsonToGraphQLQuery() - combinations', () => {
57+
it('correctly converts query with name/ empty variables', () => {
58+
const query = {
59+
query: {
60+
__name: 'NewName',
61+
__variables: {},
62+
Posts: {
63+
__args: {
64+
arg1: 20,
65+
arg2: new VariableType('variable1'),
66+
},
67+
id: true,
68+
title: true,
69+
},
70+
},
71+
};
72+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
73+
`query NewName {
74+
Posts (arg1: 20, arg2: $variable1) {
75+
id
76+
title
77+
}
78+
}`
79+
);
80+
});
81+
82+
5783
it('correctly converts query with name/variables', () => {
5884
const query = {
5985
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function buildVariables(varsObj: any): string {
5050
function buildDirectives(dirsObj: any): string {
5151
const directiveName = Object.keys(dirsObj)[0];
5252
const directiveValue = dirsObj[directiveName];
53-
if (typeof directiveValue === 'boolean') {
53+
if (typeof directiveValue === 'boolean' || (typeof directiveValue === 'object' && Object.keys(directiveValue).length === 0)) {
5454
return directiveName;
5555
}
5656
else if (typeof directiveValue === 'object') {
@@ -108,7 +108,7 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
108108
token = `${token}: ${value.__aliasFor}`;
109109
}
110110

111-
if (typeof value.__variables === 'object') {
111+
if (typeof value.__variables === 'object' && Object.keys(value.__variables).length > 0) {
112112
token = `${token} (${buildVariables(value.__variables)})`;
113113
}
114114
else if (argsExist || directivesExist) {

0 commit comments

Comments
 (0)