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

Fix-293-default-enum-values #533

Merged
merged 8 commits into from
Oct 15, 2021
Merged

Fix-293-default-enum-values #533

merged 8 commits into from
Oct 15, 2021

Conversation

kuchichan
Copy link

@kuchichan kuchichan commented Apr 9, 2021

fixes #293 #547

@codecov
Copy link

codecov bot commented Apr 9, 2021

Codecov Report

Merging #533 (87caafb) into master (4f32907) will increase coverage by 0.04%.
The diff coverage is 99.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #533      +/-   ##
==========================================
+ Coverage   98.21%   98.25%   +0.04%     
==========================================
  Files         104      104              
  Lines        5332     5513     +181     
==========================================
+ Hits         5237     5417     +180     
- Misses         95       96       +1     
Impacted Files Coverage Δ
ariadne/enums.py 98.36% <98.90%> (+1.48%) ⬆️
ariadne/executable_schema.py 100.00% <100.00%> (ø)
tests/test_enums.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f32907...87caafb. Read the comment docs.

@@ -37,8 +54,103 @@ def make_executable_schema(

assert_valid_schema(schema)

for type_name, field_name, arg, _ in find_enum_values_in_schema(schema):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block should live in separate validate_schema_enum_values function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return schema


def join_type_defs(type_defs: List[str]) -> str:
return "\n\n".join(t.strip() for t in type_defs)


def find_enum_values_in_schema(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets move this util somewhere else so executable_schema.py stays at current small and sweet size.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@kuchichan kuchichan force-pushed the fix-293-default-values-schema branch from c9478bc to b90e13c Compare April 13, 2021 13:53
ariadne/enums.py Outdated
return routes


def is_invalid_enum_value(field: Union[GraphQLInputField, GraphQLArgument]) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this function under validate_schema_enum_values

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ariadne/enums.py Outdated
return field.default_value is Undefined and field.ast_node.default_value is not None


def validate_schema_enum_values(schema: GraphQLSchema) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this function before find_enum_values_in_schema.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -22,23 +26,42 @@ def make_executable_schema(

ast_document = parse(type_defs)
schema = build_ast_schema(ast_document)
cleaned_bindables: List[SchemaBindable] = clean_bindables(*bindables)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be inlined in for bindable in clean_bindables(*bindables):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree, but please notice, that, clean_binables (now flat_binables) are also passed in repair_default_enum_values. That is the reason of extraction of that variable.


return schema


def join_type_defs(type_defs: List[str]) -> str:
return "\n\n".join(t.strip() for t in type_defs)


def clean_bindables(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def clean_bindables(
def flatten_bindables(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""
query = QueryType()

def resolv(*_, value):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def resolv(*_, value):
def resolve_test_enum(*_, value):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (for all lines, where it occurs).

assert result.data["complex"]


def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_flat_input():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_flat_input():
def test_input_exc_schema_raises_exception_for_undefined_enum_value_in_flat_input():

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

make_executable_schema([enum_definition, input_schema])


def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_in_nested_input():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_in_nested_input():
def test_input_exc_schema_raises_exception_for_undefined_enum_value_in_nested_object():

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

make_executable_schema([enum_definition, input_schema])


def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_in_query():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_input_exc_schema_should_raise_an_exception_if_undefined_enum_in_query():
def test_input_exc_schema_raises_exception_for_undefined_enum_value_in_nested_field_arg():

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

assert len(undefined) == number_of_undefined_default_enum_values


def test_issue_293():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name should shortly reflect what behaviour is tested, with docstring (or plain comment) mentioning that its a regression test for issue, eg.:

def test_something():
    # regression test for https://github.com/mirumee/issues/31321

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ariadne/enums.py Outdated
def find_enum_values_in_schema(
schema: GraphQLSchema,
) -> Generator[Union[ArgumentWithKeys, InputFieldWithKeys], None, None]:
object_types = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a biggest fan of iterating schema twice to create two lists that are then iterated over again.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored it a little bit, now I rely on functools's singledispatch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now we iterating only once ofc ;d

@kuchichan kuchichan marked this pull request as ready for review April 15, 2021 08:26
@kuchichan kuchichan changed the title Create initial functions for validating enums in inputs Fix-293-default-enum-values Apr 15, 2021
cast,
)
from functools import reduce, singledispatch
import operator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this import under import enum



def get_value_from_mapping_value(mapping: Mapping[T, Any], key_list: List[T]) -> Any:
return reduce(operator.getitem, key_list, mapping)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not run this in for loop?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm no particular reason, I found reduce more appropriate to apply collection of arguments to dict

@rafalp rafalp merged commit 20f2b90 into master Oct 15, 2021
@rafalp rafalp deleted the fix-293-default-values-schema branch October 15, 2021 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make_executable_schema's omits default enum values in args and inputs
2 participants