Skip to content

Commit

Permalink
Merge pull request #71 from jhnnsrs/skip_unreferenced_as_default
Browse files Browse the repository at this point in the history
Skip unreferenced as default
  • Loading branch information
jhnnsrs authored Jan 23, 2024
2 parents dee06da + 902c65b commit b956225
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 82 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
},

"files.exclude": {
"**/venv": true,
"**/__pycache__": true,
"**/.pytest_cache": true
},
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
{
"label": "Patch",
"type": "shell",
"command": "source venv/bin/activate && poetry version patch && poetry build "
"command": " poetry version patch && poetry build "
},
{
"label": "Publish",
"type": "shell",
"command": "source venv/bin/activate && poetry publish"
"command": " poetry publish"
},
{
"type": "shell",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "turms"
version = "0.4.2"
version = "0.5.0"
description = "graphql-codegen powered by pydantic"
authors = ["jhnnsrs <jhnnsrs@gmail.com>"]
license = "MIT"
Expand Down
8 changes: 2 additions & 6 deletions turms/plugins/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EnumsPluginConfig(PluginConfig):
type = "turms.plugins.enums.EnumsPlugin"
skip_underscore: bool = False
skip_double_underscore: bool = True
skip_unreferenced: bool = False
skip_unreferenced: bool = True
prepend: str = ""
append: str = ""

Expand All @@ -35,7 +35,6 @@ def generate_enums(
plugin_config: EnumsPluginConfig,
registry: ClassRegistry,
):

tree = []

enum_types = {
Expand All @@ -44,15 +43,14 @@ def generate_enums(
if isinstance(value, GraphQLEnumType)
}

if plugin_config.skip_unreferenced:
if plugin_config.skip_unreferenced and config.documents:
ref_registry = create_reference_registry_from_documents(
client_schema, parse_documents(client_schema, config.documents)
)
else:
ref_registry = None

for key, type in enum_types.items():

if ref_registry and key not in ref_registry.enums:
continue

Expand All @@ -71,7 +69,6 @@ def generate_enums(
)

for value_key, value in type.values.items():

if isinstance(value.value, str):
servalue = value.value
else:
Expand Down Expand Up @@ -129,7 +126,6 @@ def generate_ast(
config: GeneratorConfig,
registry: ClassRegistry,
) -> List[ast.AST]:

registry.register_import("enum.Enum")

return generate_enums(client_schema, config, self.config, registry)
40 changes: 28 additions & 12 deletions turms/plugins/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ def genereate_async_call(
)
)
else:
correct_attr = (
o.selection_set.selections[0].alias.value
if o.selection_set.selections[0].alias
else o.selection_set.selections[0].name.value
)

return ast.Return(
value=ast.Attribute(
value=ast.Await(
Expand All @@ -534,9 +540,7 @@ def genereate_async_call(
],
)
),
attr=registry.generate_node_name(
o.selection_set.selections[0].name.value
),
attr=registry.generate_node_name(correct_attr),
ctx=ast.Load(),
)
)
Expand Down Expand Up @@ -570,6 +574,12 @@ def genereate_sync_call(
)
)
else:
correct_attr = (
o.selection_set.selections[0].alias.value
if o.selection_set.selections[0].alias
else o.selection_set.selections[0].name.value
)

return ast.Return(
value=ast.Attribute(
value=ast.Call(
Expand All @@ -588,9 +598,7 @@ def genereate_sync_call(
generate_variable_dict(o, plugin_config, registry),
],
),
attr=registry.generate_node_name(
o.selection_set.selections[0].name.value
),
attr=registry.generate_node_name(correct_attr),
ctx=ast.Load(),
)
)
Expand Down Expand Up @@ -629,6 +637,12 @@ def genereate_async_iterator(
orelse=[],
)
else:
correct_attr = (
o.selection_set.selections[0].alias.value
if o.selection_set.selections[0].alias
else o.selection_set.selections[0].name.value
)

return ast.AsyncFor(
target=ast.Name(id="event", ctx=ast.Store()),
iter=ast.Call(
Expand All @@ -651,9 +665,7 @@ def genereate_async_iterator(
value=ast.Attribute(
value=ast.Name(id="event", ctx=ast.Load()),
ctx=ast.Load(),
attr=registry.generate_node_name(
o.selection_set.selections[0].name.value
),
attr=registry.generate_node_name(correct_attr),
)
)
),
Expand Down Expand Up @@ -695,6 +707,12 @@ def genereate_sync_iterator(
orelse=[],
)
else:
correct_attr = (
o.selection_set.selections[0].alias.value
if o.selection_set.selections[0].alias
else o.selection_set.selections[0].name.value
)

return ast.For(
target=ast.Name(id="event", ctx=ast.Store()),
iter=ast.Call(
Expand All @@ -717,9 +735,7 @@ def genereate_sync_iterator(
value=ast.Attribute(
value=ast.Name(id="event", ctx=ast.Load()),
ctx=ast.Load(),
attr=registry.generate_node_name(
o.selection_set.selections[0].name.value
),
attr=registry.generate_node_name(correct_attr),
)
)
),
Expand Down
9 changes: 2 additions & 7 deletions turms/plugins/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InputsPluginConfig(PluginConfig):
type = "turms.plugins.inputs.InputsPlugin"
inputtype_bases: List[str] = ["pydantic.BaseModel"]
skip_underscore: bool = True
skip_unreferenced: bool = False
skip_unreferenced: bool = True

class Config:
env_prefix = "TURMS_PLUGINS_INPUTS_"
Expand Down Expand Up @@ -106,7 +106,6 @@ def list_builder(x):
)

if is_optional:

registry.register_import("typing.Optional")
return ast.Subscript(
value=ast.Name("Optional", ctx=ast.Load()),
Expand Down Expand Up @@ -138,7 +137,6 @@ def generate_inputs(
plugin_config: InputsPluginConfig,
registry: ClassRegistry,
):

tree = []

inputobjects_type = {
Expand All @@ -147,7 +145,7 @@ def generate_inputs(
if isinstance(value, GraphQLInputObjectType)
}

if plugin_config.skip_unreferenced:
if plugin_config.skip_unreferenced and config.documents:
ref_registry = create_reference_registry_from_documents(
client_schema, parse_documents(client_schema, config.documents)
)
Expand All @@ -158,7 +156,6 @@ def generate_inputs(
registry.register_import(base)

for key, type in inputobjects_type.items():

if ref_registry and key not in ref_registry.inputs:
continue

Expand All @@ -174,7 +171,6 @@ def generate_inputs(
)

for value_key, value in type.fields.items():

field_name = registry.generate_node_name(value_key)

if field_name != value_key:
Expand Down Expand Up @@ -263,7 +259,6 @@ def generate_ast(
config: GeneratorConfig,
registry: ClassRegistry,
) -> List[ast.AST]:

for base in self.config.inputtype_bases:
registry.register_import(base)

Expand Down
30 changes: 11 additions & 19 deletions turms/plugins/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def default_generate_directives(
plugin_config: "StrawberryPluginConfig",
registry: ClassRegistry,
):

tree = []

directives = client_schema.directives
Expand All @@ -66,7 +65,6 @@ def default_generate_directives(
registry.register_import("strawberry.schema_directive.Location")

for directive in generatable_directives:

if plugin_config.skip_underscore and directive.name.startswith("_"):
continue

Expand Down Expand Up @@ -158,7 +156,6 @@ def default_generate_enums(
}

for key, type in enum_types.items():

directive_keywords = generate_directive_keywords(type.ast_node, plugin_config)
if directive_keywords:
decorator = ast.Call(
Expand All @@ -184,7 +181,6 @@ def default_generate_enums(
)

for value_key, value in type.values.items():

if isinstance(value.value, str):
servalue = value.value
else:
Expand Down Expand Up @@ -362,10 +358,12 @@ def generate_object_field_annotation(
)

if isinstance(graphql_type, GraphQLList):

registry.register_import("typing.List")

def list_builder(x):
return ast.Subscript(value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load())
return ast.Subscript(
value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load()
)

if is_optional:
registry.register_import("typing.Optional")
Expand Down Expand Up @@ -396,7 +394,9 @@ def list_builder(x):
)
)

raise NotImplementedError(f"Unknown object type {repr(graphql_type)}") # pragma: no cover
raise NotImplementedError(
f"Unknown object type {repr(graphql_type)}"
) # pragma: no cover


def recurse_argument_annotation(
Expand Down Expand Up @@ -501,10 +501,12 @@ def recurse_argument_annotation(
)

if isinstance(graphql_type, GraphQLList):

registry.register_import("typing.List")

def list_builder(x):
return ast.Subscript(value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load())
return ast.Subscript(
value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load()
)

if is_optional:
registry.register_import("typing.Optional")
Expand Down Expand Up @@ -582,7 +584,6 @@ def generate_inputs(
plugin_config: StrawberryPluginConfig,
registry: ClassRegistry,
):

tree = []

inputobjects_type = {
Expand All @@ -592,7 +593,6 @@ def generate_inputs(
}

for key, type in inputobjects_type.items():

if plugin_config.skip_underscore and key.startswith("_"): # pragma: no cover
continue

Expand Down Expand Up @@ -688,7 +688,6 @@ def generate_types(
plugin_config: StrawberryPluginConfig,
registry: ClassRegistry,
):

tree = []

objects = {
Expand Down Expand Up @@ -717,7 +716,6 @@ def generate_types(
] = {} # A list of interfaces with its respective base

for key, object_type in sorted_objects.items():

additional_bases = get_additional_bases_for_type(
object_type.name, config, registry
)
Expand Down Expand Up @@ -794,7 +792,6 @@ def generate_types(
additional_args = []
kwdefaults = []
if value.args:

sorted_args = {
key: value
for key, value in sorted(
Expand All @@ -805,7 +802,6 @@ def generate_types(
}

for argkey, arg in sorted_args.items():

additional_args.append(
ast.arg(
arg=registry.generate_parameter_name(argkey),
Expand Down Expand Up @@ -842,7 +838,6 @@ def generate_types(
keywords += generate_directive_keywords(value.ast_node, plugin_config)

if not additional_args and key not in ["Mutation", "Subscription", "Query"]:

if not keywords:
assign_value = None
else:
Expand Down Expand Up @@ -930,7 +925,6 @@ def generate_scalars(
plugin_config: StrawberryPluginConfig,
registry: ClassRegistry,
):

objects = {
key: value
for key, value in client_schema.type_map.items()
Expand All @@ -944,7 +938,6 @@ def generate_scalars(
registry.register_import("typing.NewType")

for key, scalar in objects.items():

keywords = []
keywords += generate_directive_keywords(scalar.ast_node, plugin_config)
if scalar.description:
Expand Down Expand Up @@ -1030,7 +1023,6 @@ def generate_ast(
config: GeneratorConfig,
registry: ClassRegistry,
) -> List[ast.AST]:

registry.register_import("strawberry")

directives = (
Expand Down
Loading

0 comments on commit b956225

Please sign in to comment.