def test_field_or_arg_type_from_json(self):
     name = "TestObject"
     kind = "OBJECT"
     want = graphql.TypeRef(
         name=name,
         kind=kind,
         is_list=True,
         non_null_item=True,
         non_null=True,
     )
     got = graphql.field_or_arg_type_from_json({
         "kind": "NON_NULL",
         "name": None,
         "ofType": {
             "kind": "LIST",
             "name": None,
             "ofType": {
                 "kind": "NON_NULL",
                 "name": None,
                 "ofType": {
                     "kind": kind,
                     "name": name,
                     "ofType": None
                 },
             },
         },
     })
     self.assertEqual(got, want)
Exemple #2
0
def get_typeref(error_message: str, context: str) -> Optional[graphql.TypeRef]:
    typeref = None

    field_regexes = [
        '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.+\.',
        "Expected type (?P<typeref>[_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*), found .+\.",
    ]
    arg_skip_regexes = [
        'Field [\'"][_0-9a-zA-Z\[\]!]*[\'"] of type [\'"][_A-Za-z\[\]!][_0-9a-zA-Z\[\]!]*[\'"] must have a selection of subfields\. Did you mean [\'"][_0-9a-zA-Z\[\]!]* \{ \.\.\. \}[\'"]\?'
    ]

    match = None

    if context == "Field":
        for regex in field_regexes:
            if re.fullmatch(regex, error_message):
                match = re.fullmatch(regex, error_message)
                break
    elif context == "InputValue":
        for regex in arg_skip_regexes:
            if re.fullmatch(regex, error_message):
                return None

        for regex in arg_regexes:
            if re.fullmatch(regex, error_message):
                match = re.fullmatch(regex, error_message)
                break

    if match:
        tk = match.group("typeref")

        name = tk.replace("!", "").replace("[", "").replace("]", "")
        kind = ""
        if name.endswith("Input"):
            kind = "INPUT_OBJECT"
        elif name in ["Int", "Float", "String", "Boolean", "ID"]:
            kind = "SCALAR"
        else:
            kind = "OBJECT"
        is_list = True if "[" and "]" in tk else False
        non_null_item = True if is_list and "!]" in tk else False
        non_null = True if tk.endswith("!") else False

        typeref = graphql.TypeRef(
            name=name,
            kind=kind,
            is_list=is_list,
            non_null_item=non_null_item,
            non_null=non_null,
        )
    else:
        logging.warning(f"Unknown error message: '{error_message}'")

    return typeref
    def test_to_json(self):
        name = "TestObject"
        kind = "OBJECT"

        want = {
            "kind": "NON_NULL",
            "name": None,
            "ofType": {
                "kind": "LIST",
                "name": None,
                "ofType": {
                    "kind": "NON_NULL",
                    "name": None,
                    "ofType": {
                        "kind": kind,
                        "name": name,
                        "ofType": None
                    },
                },
            },
        }
        got = graphql.TypeRef(
            name=name,
            kind=kind,
            is_list=True,
            non_null_item=True,
            non_null=True,
        ).to_json()
        self.assertEqual(got, want)
 def test_inputfield_object_non_nullable(self):
     want = graphql.TypeRef(
         name="SetArmedStateForHomeInput",
         kind="INPUT_OBJECT",
         is_list=False,
         non_null_item=False,
         non_null=True,
     )
     got = oracle.get_typeref(
         "Expected type SetArmedStateForHomeInput!, found 7.", "InputValue")
     self.assertEqual(got, want)
 def test_field_regex_3(self):
     want = graphql.TypeRef(
         name="HomeSettings",
         kind="OBJECT",
         is_list=False,
         non_null_item=False,
         non_null=False,
     )
     got = oracle.get_typeref(
         'Cannot query field "imwrongfield" on type "HomeSettings".',
         "Field")
     self.assertEqual(got, want)
 def test_non_nullable_object(self):
     want = graphql.TypeRef(
         name="SetArmedStateForHomeInput",
         kind="INPUT_OBJECT",
         is_list=False,
         non_null_item=False,
         non_null=True,
     )
     got = oracle.get_typeref(
         'Field "setArmedStateForHome" argument "input" of type "SetArmedStateForHomeInput!" is required, but it was not provided.',
         "InputValue",
     )
     self.assertEqual(got, want)
 def test_via_wrong_field(self):
     want = graphql.TypeRef(
         name="Boolean",
         kind="SCALAR",
         is_list=False,
         non_null_item=False,
         non_null=True,
     )
     got = oracle.get_typeref(
         'Field "isMfaEnabled" must not have a selection since type "Boolean!" has no subfields.',
         "Field",
     )
     self.assertEqual(got, want)
 def test_object_field(self):
     want = graphql.TypeRef(
         name="SetArmedStateForHomePayload",
         kind="OBJECT",
         is_list=False,
         non_null_item=False,
         non_null=False,
     )
     got = oracle.get_typeref(
         'Field "setArmedStateForHome" of type "SetArmedStateForHomePayload" must have a selection of subfields. Did you mean "setArmedStateForHome { ... }"?',
         "Field",
     )
     self.assertEqual(got, want)
Exemple #9
0
    def test_typeref_from_json(self):
        want = graphql.TypeRef("Launch", "OBJECT", True, False, True)

        typeref = {
            "kind": "NON_NULL",
            "name": None,
            "ofType": {
                "kind": "LIST",
                "name": None,
                "ofType": {"kind": "OBJECT", "name": "Launch", "ofType": None},
            },
        }

        got = graphql.field_or_arg_type_from_json(typeref)

        self.assertEqual(got.to_json(), want.to_json())
 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)
 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)
Exemple #12
0
    def test_typeref_to_json(self):
        want = {
            "name": None,
            "kind": "NON_NULL",
            "ofType": {
                "name": None,
                "kind": "LIST",
                "ofType": {"name": "String", "kind": "SCALAR", "ofType": None},
            },
        }

        typeref = graphql.TypeRef(
            name="String",
            kind="SCALAR",
            is_list=True,
            non_null_item=False,
            non_null=True,
        )
        got = typeref.to_json()

        # https://github.com/nikitastupin/clairvoyance/issues/9
        self.assertEqual(got, want)