Skip to content

Commit

Permalink
Improved DSL selection queries
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Dec 19, 2016
1 parent 269d2e9 commit 13fa71f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
4 changes: 2 additions & 2 deletions gql/dsl.py
Expand Up @@ -70,14 +70,14 @@ def __init__(self, name, field):
self.ast_field = ast.Field(name=ast.Name(value=name), arguments=[])
self.selection_set = None

def get(self, *fields):
def select(self, *fields):
if not self.ast_field.selection_set:
self.ast_field.selection_set = ast.SelectionSet(selections=[])
self.ast_field.selection_set.selections.extend(selections(*fields))
return self

def __call__(self, *args, **kwargs):
return self.get(*args, **kwargs)
return self.args(*args, **kwargs)

def alias(self, alias):
self.ast_field.alias = ast.Name(value=alias)
Expand Down
37 changes: 8 additions & 29 deletions tests/starwars/test_dsl.py
Expand Up @@ -3,27 +3,6 @@
from gql import Client
from gql.dsl import DSLSchema

from .schema import characterInterface, humanType, queryType


# We construct a Simple DSL objects for easy field referencing

# class Query(object):
# hero = queryType.fields['hero']
# human = queryType.fields['human']


# class Character(object):
# id = characterInterface.fields['id']
# name = characterInterface.fields['name']
# friends = characterInterface.fields['friends']
# appears_in = characterInterface.fields['appearsIn']


# class Human(object):
# name = humanType.fields['name']


from .schema import StarWarsSchema


Expand All @@ -40,7 +19,7 @@ def test_hero_name_query(ds):
name
}
'''.strip()
query_dsl = ds.Query.hero(
query_dsl = ds.Query.hero.select(
ds.Character.name
)
assert query == str(query_dsl)
Expand All @@ -56,10 +35,10 @@ def test_hero_name_and_friends_query(ds):
}
}
'''.strip()
query_dsl = ds.Query.hero(
query_dsl = ds.Query.hero.select(
ds.Character.id,
ds.Character.name,
ds.Character.friends(
ds.Character.friends.select(
ds.Character.name,
)
)
Expand All @@ -79,12 +58,12 @@ def test_nested_query(ds):
}
}
'''.strip()
query_dsl = ds.Query.hero(
query_dsl = ds.Query.hero.select(
ds.Character.name,
ds.Character.friends(
ds.Character.friends.select(
ds.Character.name,
ds.Character.appears_in,
ds.Character.friends(
ds.Character.friends.select(
ds.Character.name
)
)
Expand All @@ -98,7 +77,7 @@ def test_fetch_luke_query(ds):
name
}
'''.strip()
query_dsl = ds.Query.human.args(id="1000").get(
query_dsl = ds.Query.human(id="1000").select(
ds.Human.name,
)

Expand Down Expand Up @@ -172,7 +151,7 @@ def test_fetch_luke_aliased(ds):
name
}
'''.strip()
query_dsl = ds.Query.human.args(id=1000).alias('luke').get(
query_dsl = ds.Query.human.args(id=1000).alias('luke').select(
ds.Character.name,
)
assert query == str(query_dsl)
Expand Down

0 comments on commit 13fa71f

Please sign in to comment.