def test_non_nullable_list(self):

        PersonType = GraphQLObjectType(
            "Person", lambda: {"name": GraphQLField(GraphQLString)}
        )

        schema = GraphQLSchema(
            query=GraphQLObjectType(
                name="RootQueryType",
                fields={
                    "people": GraphQLField(
                        GraphQLList(GraphQLNonNull(PersonType)),
                        resolve=lambda obj, info: {"name": "eran"},
                    )
                },
            )
        )

        query = """
                query GetPeople {
                  people {
                    name
                  }
                }
            """

        parser = QueryParser(schema)
        dataclass_renderer = DataclassesRenderer(schema)

        parsed = parser.parse(query)
        rendered = dataclass_renderer.render(parsed)

        m = self.load_module(rendered)

        mock_client = MagicMock()
        mock_client.call = MagicMock(
            return_value="""
           {
               "data": {
                   "people": [
                      {
                        "name": "eran"
                      },
                      {
                        "name": "eran1"
                      }
                   ]
               }
           }
        """
        )

        result = m.GetPeople.execute(mock_client)
        assert result
        assert isinstance(result, m.GetPeople.GetPeopleData)

        assert len(result.people) == 2
        assert result.people[0].name == "eran"
        assert result.people[1].name == "eran1"
Ejemplo n.º 2
0
    def setUp(self) -> None:
        if 'SWAPI_SCHEMA' not in globals():
            filename = os.path.join(os.path.dirname(__file__),
                                    'fixtures/swapi-schema.graphql')
            globals()['SWAPI_SCHEMA'] = load_schema(filename)
        self.swapi_schema = globals()['SWAPI_SCHEMA']
        self.swapi_parser = QueryParser(self.swapi_schema)

        if 'GITHUB_SCHEMA' not in globals():
            filename = os.path.join(os.path.dirname(__file__),
                                    'fixtures/github-schema.graphql')
            globals()['GITHUB_SCHEMA'] = load_schema(filename)

        self.github_schema = globals()['GITHUB_SCHEMA']
        self.github_parser = QueryParser(self.github_schema)

        self.swapi_dataclass_renderer = DataclassesRenderer(self.swapi_schema)
        self.github_dataclass_renderer = DataclassesRenderer(
            self.github_schema)
    def test_graphql_compilation(self, mock_popen, mock_path_exists):
        graphql_library = pkg_resources.resource_filename(__name__, "graphql")
        schema_filepath = pkg_resources.resource_filename(
            __name__, "schema/symphony.graphql")

        schema = build_ast_schema(parse((open(schema_filepath).read())))
        filenames = glob.glob(os.path.join(graphql_library, "**/*.graphql"),
                              recursive=True)
        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)

        for filename in filenames:
            self.verify_file(filename, query_parser, query_renderer)
Ejemplo n.º 4
0
    def test_graphql_compilation(self, mock_popen, mock_path_exists):
        graphql_library = pkg_resources.resource_filename(__name__, "graphql")
        schema_filepath = pkg_resources.resource_filename(
            __name__, "schema/symphony.graphql")

        schema = build_ast_schema(parse((open(schema_filepath).read())))
        filenames = glob.glob(os.path.join(graphql_library, "**/*.graphql"),
                              recursive=True)
        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)

        for filename in filenames:
            with open(filename) as f:
                query = parse(f.read())
                usages = find_deprecated_usages(schema, query)
                assert len(usages) == 0, (f"Graphql file name {filename} uses "
                                          f"deprecated fields {usages}")
            self.verify_file(filename, query_parser, query_renderer)
    def test_simple_query_with_enums_default_value(self):
        """
            enum LengthUnit {
              METER
              KM
            }

            type Starship {
              id: ID!
              name: String!
              length(unit: LengthUnit = METER): Float
            }

            type Query {
                ship(id: String!): Starship
            }
        """

        length_unit_enum = GraphQLEnumType(
            "LengthUnit",
            {"METER": GraphQLEnumValue("METER"), "KM": GraphQLEnumValue("KM")},
            description="One of the films in the Star Wars Trilogy",
        )

        starship_type = GraphQLObjectType(
            "Starship",
            lambda: {
                "id": GraphQLField(
                    GraphQLNonNull(GraphQLString), description="The id of the ship."
                ),
                "name": GraphQLField(
                    GraphQLString, description="The name of the ship."
                ),
                "length": GraphQLField(
                    GraphQLInt,
                    args={
                        "unit": GraphQLArgument(
                            GraphQLNonNull(length_unit_enum),
                            default_value="METER",
                            description="id of the droid",
                        )
                    },
                ),
            },
        )

        query_type = GraphQLObjectType(
            "Query",
            lambda: {
                "ship": GraphQLField(
                    starship_type,
                    args={
                        "id": GraphQLArgument(
                            GraphQLNonNull(GraphQLString), description="id of the ship"
                        )
                    },
                )
            },
        )

        schema = GraphQLSchema(query_type, types=[length_unit_enum, starship_type])

        query = """
            query GetStarship {
                ship(id: "Enterprise") {
                    id
                    name
                    length(unit: METER)
                }
            }
        """
        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)
        parsed = query_parser.parse(query)

        rendered_enums = query_renderer.render_enums(parsed)
        for rendered_enum in rendered_enums:
            self.load_module(rendered_enum)

        rendered = query_renderer.render(parsed)
        m = self.load_module(rendered)

        response = m.GetStarship.from_json(
            """
            {
                "data": {
                    "ship": {
                        "id": "Enterprise",
                        "name": "Enterprise",
                        "length": 100
                    }
                }
            }
        """
        )

        assert response

        ship = response.data.ship
        assert ship
        assert ship.id == "Enterprise"
        assert ship.name == "Enterprise"
        assert ship.length == 100
Ejemplo n.º 6
0
    def test_simple_query_with_enums_default_value(self):
        """
            enum LengthUnit {
              METER
              KM
            }

            type Starship {
              id: ID!
              name: String!
              length(unit: LengthUnit = METER): Float
            }

            type Query {
                ship(id: String!): Starship
            }
        """

        length_unit_enum = GraphQLEnumType(
            'LengthUnit',
            {
                'METER': GraphQLEnumValue('METER'),
                'KM': GraphQLEnumValue('KM'),
            },
            description='One of the films in the Star Wars Trilogy',
        )

        starship_type = GraphQLObjectType(
            'Starship', lambda: {
                'id':
                GraphQLField(GraphQLNonNull(GraphQLString),
                             description='The id of the ship.'),
                'name':
                GraphQLField(GraphQLString,
                             description='The name of the ship.'),
                'length':
                GraphQLField(GraphQLInt,
                             args={
                                 'unit':
                                 GraphQLArgument(GraphQLNonNull(
                                     length_unit_enum),
                                                 default_value='METER',
                                                 description='id of the droid')
                             })
            })

        query_type = GraphQLObjectType(
            'Query', lambda: {
                'ship':
                GraphQLField(
                    starship_type,
                    args={
                        'id':
                        GraphQLArgument(GraphQLNonNull(GraphQLString),
                                        description='id of the ship')
                    },
                )
            })

        schema = GraphQLSchema(query_type,
                               types=[length_unit_enum, starship_type])

        query = """
            query GetStarship {
                ship(id: "Enterprise") {
                    id
                    name
                    length(unit: METER)
                }
            }
        """
        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)
        parsed = query_parser.parse(query)
        rendered = query_renderer.render(parsed)

        m = self.load_module(rendered)
        response = m.GetStarship.from_json("""
            {
                "data": {
                    "ship": {
                        "id": "Enterprise",
                        "name": "Enterprise",
                        "length": 100
                    }
                }
            }
        """)

        assert response

        ship = response.data.ship
        assert ship
        assert ship.id == 'Enterprise'
        assert ship.name == 'Enterprise'
        assert ship.length == 100