Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: WIP introspection from schema using typeddict #108

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ packages = [

[tool.poetry.dependencies]
python = "^3.6"
typing-extensions = { version = "3.7.*", python = "~3.6" }

[tool.poetry.dev-dependencies]
pytest = "^5.4"
Expand Down
141 changes: 138 additions & 3 deletions src/graphql/utilities/introspection_from_schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,149 @@
from typing import Any, Dict
import sys

from ..pyutils import FrozenList

if sys.version_info < (3, 7):
from typing_extensions import TypedDict
else:
from typing import TypedDict, Optional, Generic, TypeVar

from typing import Any

from ..error import GraphQLError
from ..language import parse
from ..language import parse, DirectiveLocation
from ..type import GraphQLSchema
from .get_introspection_query import get_introspection_query

__all__ = ["introspection_from_schema"]

# Based on the GraphQL-JS implementation
# https://github.com/graphql/graphql-js/blob/master/src/utilities/getIntrospectionQuery.js

class IntrospectionType:
__slots__ = "name", "description"

name: str
description: Optional[str]

class IntrospectionScalarType(IntrospectionType):
description: Optional[str]
specified_by_url: Optional[str]

class IntrospectionObjectType(IntrospectionType):
kind: str = "OBJECT"
fields: FrozenList["IntrospectionField"]
interfaces: FrozenList["IntrospectionInterfaceType"]

class IntrospectionInterfaceType(IntrospectionType):
kind: str = "INTERFACE"
fields: FrozenList["IntrospectionField"]
interfaces: FrozenList["IntrospectionInterfaceType"]
possible_types: FrozenList["IntrospectionObjectType"]

class IntrospectionUnionType (IntrospectionType):
kind: str = "UNION"
possible_types: FrozenList["IntrospectionNamedTypeRef"]

class IntrospectionEnumType(IntrospectionType):
kind: str = "ENUM"
enum_values: FrozenList["IntrospectionEnumValue"]

class IntrospectionInputObjectType(IntrospectionType):
kind: str = "INPUT_OBJECT"
input_fields: FrozenList["IntrospectionInputValue"]

IntrospectionOutputType = (
IntrospectionScalarType,
IntrospectionObjectType,
IntrospectionInterfaceType,
IntrospectionUnionType,
IntrospectionEnumType,
)

IntrospectionInputType = (
IntrospectionScalarType,
IntrospectionEnumType,
IntrospectionInputObjectType,
)

# GraphQL Introspection Type
GIT = TypeVar("GIT", bound="IntrospectionType")

class IntrospectionNamedTypeRef(IntrospectionType, Generic[GIT]):
kind: GIT["kind"]
name: str

class IntrospectionListTypeRef(IntrospectionType, Generic[GIT]):
kind: str = 'LIST'
of_type: GIT

IntrospectionTypeRef = (
IntrospectionNamedTypeRef,
IntrospectionListTypeRef
)

IntrospectionOutputTypeRef = (
IntrospectionNamedTypeRef[IntrospectionType, IntrospectionOutputType]
)

IntrospectionInputTypeRef = IntrospectionInputType

class IntrospectionField:
name: str
description: Optional[str]
args: FrozenList["IntrospectionInputValue"]
type: IntrospectionOutputTypeRef
isDeprecated: bool
deprecationReason: Optional[str]

class IntrospectionInputValue:
name: str
description: Optional[str]
type: IntrospectionInputTypeRef
defaultValue: Optional[str]

class IntrospectionEnumValue:
name: str
description: Optional[str]
isDeprecated: bool
deprecationReason: Optional[str]

#
# | IntrospectionScalarType
# | IntrospectionObjectType
# | IntrospectionInterfaceType
# | IntrospectionUnionType
# | IntrospectionEnumType
# | IntrospectionInputObjectType
#
# IntrospectionOutputType =
# | IntrospectionScalarType
# | IntrospectionObjectType
# | IntrospectionInterfaceType
# | IntrospectionUnionType
# | IntrospectionEnumType
#
# IntrospectionInputType =
# | IntrospectionScalarType
# | IntrospectionEnumType
# | IntrospectionInputObjectType


class IntrospectionDirective(TypedDict):
name: str
description: Optional[str]
isRepeatable: Optional[bool]
locations: FrozenList[DirectiveLocation]
args: FrozenList[IntrospectionInputValue]


IntrospectionSchema = Dict[str, Any]
class IntrospectionSchema(TypedDict):
description: Optional[str]
queryType: IntrospectionNamedTypeRef[IntrospectionObjectType]
mutationType: Optional[IntrospectionNamedTypeRef[IntrospectionObjectType]]
subscriptionType: Optional[IntrospectionNamedTypeRef[IntrospectionObjectType]]
types: FrozenList[IntrospectionType]
directives: FrozenList[IntrospectionDirective]


def introspection_from_schema(
Expand Down