plural.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import Any, Callable, List, Optional
  2. from graphql import (
  3. GraphQLArgument,
  4. GraphQLField,
  5. GraphQLInputType,
  6. GraphQLOutputType,
  7. GraphQLList,
  8. GraphQLNonNull,
  9. GraphQLResolveInfo,
  10. get_nullable_type,
  11. )
  12. __all__ = ["plural_identifying_root_field"]
  13. def plural_identifying_root_field(
  14. arg_name: str,
  15. input_type: GraphQLInputType,
  16. output_type: GraphQLOutputType,
  17. resolve_single_input: Callable[[GraphQLResolveInfo, str], Any],
  18. description: Optional[str] = None,
  19. ) -> GraphQLField:
  20. def resolve(_obj: Any, info: GraphQLResolveInfo, **args: Any) -> List:
  21. inputs = args[arg_name]
  22. return [resolve_single_input(info, input_) for input_ in inputs]
  23. return GraphQLField(
  24. GraphQLList(output_type),
  25. description=description,
  26. args={
  27. arg_name: GraphQLArgument(
  28. GraphQLNonNull(
  29. GraphQLList(
  30. GraphQLNonNull(get_nullable_type(input_type)) # type: ignore
  31. )
  32. )
  33. )
  34. },
  35. resolve=resolve,
  36. )