Skip to content

Instantly share code, notes, and snippets.

@rockyburt
Created September 12, 2021 13:22
Show Gist options
  • Save rockyburt/8f3c5f3d19c2852a42af81f039c23098 to your computer and use it in GitHub Desktop.
Save rockyburt/8f3c5f3d19c2852a42af81f039c23098 to your computer and use it in GitHub Desktop.
## While perhaps not the best way, this worked fine in strawberry v74.x
## module1, to keep strawberry stuff out
def parse_slice_size(val: int) -> types.PositiveInt:
assert isinstance(val, int), "Input must be of type int"
if val > config.settings.max_slice_size or val < 1:
raise ValueError(f"Must be between 1 and (including) {config.settings.max_slice_size}")
return types.PositiveInt(val)
PositiveInt = NewType("PositiveInt", int)
PositiveInt.__doc__ = "Zero or a positive-only integer"
## module2, all strawberry'isms on top off the base classes
strawberry.scalar(
types.PositiveInt,
serialize=lambda x: int(x),
parse_value=types.parse_slice_size,
description=types.PositiveInt.__doc__,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment