Skip to content

Commit

Permalink
Added check for unknown GraphQL ast nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
vmagamedov committed Nov 19, 2016
1 parent b91f905 commit bd9788e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hiku/readers/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def visit(self, obj):
visit_method = getattr(self, 'visit_{}'.format(obj.__class__.__name__),
None)
if visit_method is None:
raise TypeError('Not implemented node type: {!r}'.format(obj))
raise NotImplementedError('Not implemented node type: {!r}'
.format(obj))
return visit_method(obj)


Expand All @@ -30,13 +31,15 @@ def visit_Document(self, obj):
raise NotImplementedError('Only single operation per document is '
'supported, {} operations was provided'
.format(len(obj.definitions)))
definition, = obj.definitions
if definition.operation != 'query':
return self.visit(obj.definitions[0])

def visit_OperationDefinition(self, obj):
if obj.operation != 'query':
raise NotImplementedError('Only "query" operations are supported, '
'"{}" operation was provided'
.format(definition.operation))
assert definition.operation == 'query', definition.operation
return self.visit(definition.selection_set)
.format(obj.operation))
assert obj.operation == 'query', obj.operation
return self.visit(obj.selection_set)

def visit_SelectionSet(self, obj):
return Node([self.visit(i) for i in obj.selections])
Expand Down
6 changes: 6 additions & 0 deletions tests/test_read_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ def test_mutation_operation():
read('mutation { doSomething(kokam: "screens") }')
err.match('Only "query" operations are supported, "mutation" operation '
'was provided')


def test_unknown_node():
with pytest.raises(NotImplementedError) as err:
read('fragment bays on sweats { apollo }')
err.match('Not implemented node type: FragmentDefinition')

0 comments on commit bd9788e

Please sign in to comment.