Skip to content

Throw an exception if fields are empty or null #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions gql_query_builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from typing import Dict, List, Union


class GqlQueryException(Exception):
pass


class GqlQuery():
def __init__(self) -> None:
self.object: str = ''
Expand All @@ -16,6 +20,8 @@ def remove_duplicate_spaces(self, query: str) -> str:
return " ".join(query.split())

def fields(self, fields: List, name: str = '', condition_expression: str = ''):
if not fields:
raise GqlQueryException('fields cannot be empty or null')
query = '{ ' + " ".join(fields) + ' }'
if name != '':
if condition_expression != '':
Expand Down
8 changes: 6 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from unittest import TestCase
from gql_query_builder import GqlQuery
from gql_query_builder import GqlQuery, GqlQueryException


class TestGqlQuery(TestCase):
def test_query_empty_fields(self):
with self.assertRaises(GqlQueryException):
GqlQuery().fields([]).query('hero').generate()

def test_query_a_single_field(self):
expected = 'query { hero { name } }'
actual = GqlQuery().fields(['name']).query('hero').operation().generate()
self.assertEqual(expected, actual)

def test_query_neting_fields(self):
def test_query_nesting_fields(self):
expected = 'query { hero { name friends { name } } }'
field_friends = GqlQuery().fields(['name'], name='friends').generate()
actual = GqlQuery().fields(['name', field_friends]).query('hero').operation('query').generate()
Expand Down