コード例 #1
0
    def test_simple_query_with_complex_fragment(self):
        fragment_query = """
            fragment CharacterFields on Person {
                name

                home: homeworld {
                    name
                }
            }
        """
        query = """
            query GetPerson {
              luke: character(id: "luke") {
                ...CharacterFields
              }
            }
        """

        parsed_fragment = self.swapi_parser.parse(fragment_query,
                                                  is_fragment=True)
        rendered_fragment = self.swapi_dataclass_renderer.render(
            parsed_fragment)

        self.load_module(rendered_fragment,
                         module_name=get_fragment_filename("CharacterFields"))

        parsed = self.swapi_parser.parse(query, fragment_query)
        rendered = self.swapi_dataclass_renderer.render(parsed)
        # TODO T66492306
        rendered = remove_dirname_in_import(dirname=FRAGMENT_DIRNAME,
                                            rendered=rendered)
        m = self.load_module(rendered)
        response = m.GetPerson.from_json("""
        {
            "data": {
                "luke": {
                    "name": "Luke Skywalker",
                    "home": {
                        "name": "Arakis"
                    }
                }
            }
        }
        """)

        assert response

        data = response.data
        assert data.luke.name == "Luke Skywalker"
        assert data.luke.home.name == "Arakis"
コード例 #2
0
    def test_simple_query_with_fragment(self):
        fragment_query = """
            fragment FilmFields on Film {
                title
                director
            }
        """

        query = """
            query GetFilm {
              returnOfTheJedi: film(id: "1") {
                ...FilmFields
                openingCrawl
              }
            }
        """

        parsed_fragment = self.swapi_parser.parse(fragment_query,
                                                  is_fragment=True)
        rendered_fragment = self.swapi_dataclass_renderer.render(
            parsed_fragment)

        self.load_module(rendered_fragment,
                         module_name=get_fragment_filename("FilmFields"))

        parsed = self.swapi_parser.parse(query, fragment_query)
        rendered = self.swapi_dataclass_renderer.render(parsed)
        # TODO T66492306
        rendered = remove_dirname_in_import(dirname=FRAGMENT_DIRNAME,
                                            rendered=rendered)
        m = self.load_module(rendered)
        response = m.GetFilm.from_json("""
        {
            "data": {
                "returnOfTheJedi": {
                    "title": "Return of the Jedi",
                    "director": "George Lucas",
                    "openingCrawl": "la la la"
                }
            }
        }
        """)

        assert response

        data = response.data
        assert data.returnOfTheJedi.title == "Return of the Jedi"
        assert data.returnOfTheJedi.director == "George Lucas"
        assert data.returnOfTheJedi.openingCrawl == "la la la"
コード例 #3
0
    def test_simple_query_with_complex_fragments(self):
        fragment_query1 = """
            fragment PlanetFields on Planet {
              name
              population
              terrains
            }
        """
        fragment_query2 = """
            fragment CharacterFields on Person {
              name
              home: homeworld {
                ...PlanetFields
              }
            }
        """
        query = """
            query GetPerson {
              luke: character(id: "luke") {
                ...CharacterFields
              }
            }
        """

        parsed_fragment1 = self.swapi_parser.parse(fragment_query1,
                                                   is_fragment=True)
        rendered_fragment1 = self.swapi_dataclass_renderer.render(
            parsed_fragment1)
        # TODO T66492306
        rendered_fragment1 = remove_dirname_in_import(
            dirname=FRAGMENT_DIRNAME, rendered=rendered_fragment1)
        self.load_module(rendered_fragment1,
                         module_name=get_fragment_filename("PlanetFields"))

        parsed_fragment2 = self.swapi_parser.parse(fragment_query2,
                                                   fragment_query1,
                                                   is_fragment=True)
        rendered_fragment2 = self.swapi_dataclass_renderer.render(
            parsed_fragment2)
        # TODO T66492306
        rendered_fragment2 = remove_dirname_in_import(
            dirname=FRAGMENT_DIRNAME, rendered=rendered_fragment2)
        self.load_module(rendered_fragment2,
                         module_name=get_fragment_filename("CharacterFields"))

        parsed = self.swapi_parser.parse(query,
                                         fragment_query1 + fragment_query2)
        rendered = self.swapi_dataclass_renderer.render(parsed)
        # TODO T66492306
        rendered = remove_dirname_in_import(dirname=FRAGMENT_DIRNAME,
                                            rendered=rendered)
        m = self.load_module(rendered)
        response = m.GetPerson.from_json("""
        {
            "data": {
                "luke": {
                    "name": "Luke Skywalker",
                    "home": {
                        "name": "Arakis",
                        "population": "1,000,000",
                        "terrains": ["Desert"]
                    }
                }
            }
        }
        """)

        assert response

        data = response.data
        assert data.luke.name == "Luke Skywalker"
        assert data.luke.home.name == "Arakis"