-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GraphQL API server backend for the experimental-explorer goal (#1…
…5697) * Add explorer backend api server. Start it with `./pants experimental-explorer` then go to http://localhost:8000/graphql for the GraphQL UI, where queries may be executed interactively as well as browsing the Graph API documentation.
- Loading branch information
Showing
25 changed files
with
2,052 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() | ||
|
||
python_distribution( | ||
name="dist", | ||
provides=python_artifact( | ||
name="pantsbuild.pants.explorer-server", | ||
description="Backend API server implementation for the Pants Explorer UI.", | ||
classifiers=["Topic :: Software Development"], | ||
), | ||
entry_points={ | ||
"pantsbuild.plugin": { | ||
"rules": "pants.backend.explorer.register:rules", | ||
} | ||
}, | ||
) | ||
|
||
python_tests( | ||
name="tests", | ||
dependencies=[":explorer"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from pants.core.util_rules.system_binaries import OpenBinary | ||
from pants.engine.explorer import RequestState | ||
from pants.engine.process import Process, ProcessCacheScope, ProcessResult | ||
from pants.engine.rules import QueryRule, collect_rules, rule | ||
from pants.util.logging import LogLevel | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Browser: | ||
open_binary: OpenBinary | ||
protocol: str | ||
server: str | ||
|
||
def open(self, request_state: RequestState, uri: str = "/") -> ProcessResult | None: | ||
if not self.open_binary: | ||
return None | ||
|
||
url = f"{self.protocol}://{self.server}{uri}" | ||
return request_state.product_request( | ||
ProcessResult, | ||
( | ||
Process( | ||
(self.open_binary.path, url), | ||
description=f"Open {url} with default web browser.", | ||
level=LogLevel.INFO, | ||
cache_scope=ProcessCacheScope.PER_SESSION, | ||
), | ||
), | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class BrowserRequest: | ||
protocol: str | ||
server: str | ||
|
||
|
||
@rule | ||
async def get_browser(request: BrowserRequest, open_binary: OpenBinary) -> Browser: | ||
return Browser(open_binary, request.protocol, request.server) | ||
|
||
|
||
def rules(): | ||
return ( | ||
*collect_rules(), | ||
QueryRule(ProcessResult, (Process,)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import cast | ||
|
||
from strawberry.types import Info | ||
|
||
from pants.backend.explorer.server.uvicorn import UvicornServer | ||
from pants.engine.explorer import RequestState | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GraphQLContext: | ||
uvicorn: UvicornServer | ||
|
||
def create_request_context(self) -> dict[str, RequestState]: | ||
return dict(pants_request_state=self.uvicorn.request_state) | ||
|
||
@staticmethod | ||
def request_state_from_info(info: Info) -> RequestState: | ||
return cast(RequestState, info.context["pants_request_state"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import json | ||
from typing import NewType | ||
|
||
import strawberry | ||
|
||
JSONScalar = strawberry.scalar( | ||
NewType("JSONScalar", object), | ||
serialize=lambda v: v, | ||
parse_value=lambda v: json.loads(v), | ||
description="The GenericScalar scalar type represents a generic GraphQL scalar value.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
import strawberry | ||
|
||
from pants.backend.explorer.graphql.query.rules import QueryRulesMixin | ||
from pants.backend.explorer.graphql.query.targets import QueryTargetsMixin | ||
|
||
|
||
@strawberry.type | ||
class Query(QueryRulesMixin, QueryTargetsMixin): | ||
"""Access to Pantsbuild data.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
from dataclasses import asdict | ||
from typing import Iterable, Iterator, List, Optional, cast | ||
|
||
import strawberry | ||
from strawberry.types import Info | ||
|
||
from pants.backend.explorer.graphql.context import GraphQLContext | ||
from pants.help import help_info_extracter | ||
|
||
|
||
@strawberry.type(description=cast(str, help_info_extracter.RuleInfo.__doc__)) | ||
class RuleInfo: | ||
name: str | ||
description: Optional[str] | ||
documentation: Optional[str] | ||
provider: str | ||
output_type: str | ||
input_types: List[str] | ||
input_gets: List[str] | ||
|
||
@classmethod | ||
def from_help(cls, info: help_info_extracter.RuleInfo) -> RuleInfo: | ||
data = asdict(info) | ||
return cls(**data) | ||
|
||
|
||
@strawberry.input( | ||
description="Filter rules based on name and/or limit the number of entries to return." | ||
) | ||
class RulesQuery: | ||
name_re: Optional[str] = strawberry.field( | ||
default=None, description="Select rules matching a regexp." | ||
) | ||
limit: Optional[int] = strawberry.field( | ||
default=None, description="Limit the number of entries returned." | ||
) | ||
|
||
def __bool__(self) -> bool: | ||
return not (self.name_re is None and self.limit is None) | ||
|
||
@staticmethod | ||
def filter(query: RulesQuery | None, rules: Iterable[RuleInfo]) -> Iterator[RuleInfo]: | ||
if not query: | ||
yield from rules | ||
return | ||
|
||
name_pattern = query.name_re and re.compile(query.name_re) | ||
count = 0 | ||
for info in rules: | ||
if name_pattern and not re.match(name_pattern, info.name): | ||
continue | ||
yield info | ||
count += 1 | ||
if query.limit and count >= query.limit: | ||
return | ||
|
||
|
||
@strawberry.type | ||
class QueryRulesMixin: | ||
"""Get rules related info.""" | ||
|
||
@strawberry.field | ||
def rules(self, info: Info, query: Optional[RulesQuery] = None) -> List[RuleInfo]: | ||
request_state = GraphQLContext.request_state_from_info(info) | ||
return list( | ||
RulesQuery.filter( | ||
query, | ||
( | ||
RuleInfo.from_help(info) | ||
for info in request_state.all_help_info.name_to_rule_info.values() | ||
), | ||
) | ||
) |
Oops, something went wrong.