Skip to content

Commit

Permalink
Fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
pigulla authored and khaosdoctor committed Dec 4, 2020
1 parent 0ad7825 commit a608ef1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modules/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const info = debug('gotql:info:parser')
*/
function getQueryVars (variables: QueryType['variables']): string {
info('Getting query variables')
if (!variables) return ''
if (!variables || Object.keys(variables).length === 0) return ''

let queryVars = '('
for (let varName in variables) {
Expand Down Expand Up @@ -236,7 +236,7 @@ function parseOperation (query: QueryType, allowEmptyFields: boolean = false): s

try {
let operationArgs = ''
if (operation.args) {
if (operation.args && Object.keys(operation.args).length > 0) {
info('Parsing args')
operationArgs = parseArgs(query.operation.args, query.variables)
info('Operation args are: %s', operationArgs)
Expand Down
30 changes: 30 additions & 0 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,34 @@ describe('parser', () => {
const mutationResult = parse(query, ExecutionType.MUTATION)
expect(mutationResult).toEqual(mutationReturn)
})

it('should not add parenthesis if the args object is empty (#TBD)', () => {
const query = {
name: 'TestQuery',
operation: {
name: 'TestOp',
args: {},
fields: ['field1', 'field2']
}
}

const testReturn = 'query TestQuery { TestOp { field1 field2 } }'
const queryResult = parse(query, ExecutionType.QUERY)
expect(queryResult).toEqual(testReturn)
});

it('should handle the empty object for variables (#TBD)', () => {
const query = {
name: 'TestQuery',
operation: {
name: 'TestOp',
fields: ['field1', 'field2']
},
variables: {}
}

const testReturn = 'query TestQuery { TestOp { field1 field2 } }'
const queryResult = parse(query, ExecutionType.QUERY)
expect(queryResult).toEqual(testReturn)
});
})

0 comments on commit a608ef1

Please sign in to comment.