Skip to content

Commit

Permalink
Merge pull request #23 from nikitastupin/fix-issues-16-and-20
Browse files Browse the repository at this point in the history
Fix issues #16 and #20
  • Loading branch information
nikitastupin committed Sep 3, 2021
2 parents 871605d + 23f5181 commit ef34f53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
13 changes: 10 additions & 3 deletions clairvoyance/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ def get_typeref(error_message: str, context: str) -> Optional[graphql.TypeRef]:
'Field "[_0-9a-zA-Z\[\]!]*" of type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)" must have a selection of subfields. Did you mean "[_0-9a-zA-Z\[\]!]* \{ ... \}"\?',
'Field "[_0-9a-zA-Z\[\]!]*" must not have a selection since type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)" has no subfields.',
'Cannot query field "[_0-9a-zA-Z\[\]!]*" on type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)".',
'Field "[_0-9a-zA-Z\[\]!]*" of type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)" must not have a sub selection\.',
]
arg_regexes = [
'Field "[_0-9a-zA-Z\[\]!]*" argument "[_0-9a-zA-Z\[\]!]*" of type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)" is required, but it was not provided.',
'Field "[_0-9a-zA-Z\[\]!]*" argument "[_0-9a-zA-Z\[\]!]*" of type "(?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*)" is required.+\.',
"Expected type (?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*), found .+\.",
]
arg_skip_regexes = [
Expand Down Expand Up @@ -320,11 +321,12 @@ def probe_typeref(

for error in errors:
typeref = get_typeref(error["message"], context)
logging.debug(f"get_typeref('{error['message']}', '{context}') -> {typeref}")
if typeref:
return typeref

if not typeref:
raise Exception(f"Unable to get TypeRef for {documents}")
if not typeref and context != 'InputValue':
raise Exception(f"Unable to get TypeRef for {documents} in context {context}")

return None

Expand All @@ -348,6 +350,8 @@ def probe_arg_typeref(
input_document.replace("FUZZ", f"{field}({arg}: 7)"),
input_document.replace("FUZZ", f"{field}({arg}: {{}})"),
input_document.replace("FUZZ", f"{field}({arg[:-1]}: 7)"),
input_document.replace("FUZZ", f"{field}({arg}: \"7\")"),
input_document.replace("FUZZ", f"{field}({arg}: false)"),
]

typeref = probe_typeref(documents, "InputValue", config)
Expand Down Expand Up @@ -454,6 +458,9 @@ def clairvoyance(
arg_typeref = probe_arg_typeref(
field.name, arg_name, config, input_document
)
if not arg_typeref:
logging.warning(f'Skip argument {arg_name} because TypeRef equals {arg_typeref}')
continue
arg = graphql.InputValue(arg_name, arg_typeref)

field.args.append(arg)
Expand Down
34 changes: 34 additions & 0 deletions tests/oracle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,40 @@ def test_skip_error_message(self):
self.assertEqual(want, got)
self.assertCountEqual(["WARNING:root:Dummy warning"], cm.output)

def test_issue_16(self):
want = graphql.TypeRef(
name="ID",
kind="SCALAR",
is_list=False,
non_null_item=False,
non_null=True,
)
got = oracle.get_typeref(
'Field "node" argument "id" of type "ID!" is required but not provided.', "InputValue"
)
self.assertEqual(got.name, want.name)
self.assertEqual(got.kind, want.kind)
self.assertEqual(got.is_list, want.is_list)
self.assertEqual(got.non_null_item, want.non_null_item)
self.assertEqual(got.non_null, want.non_null)

def test_dvga(self):
want = graphql.TypeRef(
name="String",
kind="SCALAR",
is_list=False,
non_null_item=False,
non_null=False,
)
got = oracle.get_typeref(
'Field "systemHealth" of type "String" must not have a sub selection.', "Field"
)
self.assertEqual(got.name, want.name)
self.assertEqual(got.kind, want.kind)
self.assertEqual(got.is_list, want.is_list)
self.assertEqual(got.non_null_item, want.non_null_item)
self.assertEqual(got.non_null, want.non_null)


class TestTypeRef(unittest.TestCase):
def test_to_json(self):
Expand Down

0 comments on commit ef34f53

Please sign in to comment.