-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from mirumee/imgix_resolver
Imgix resolver
- Loading branch information
Showing
12 changed files
with
712 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .query_params_resolver import get_query_params_resolver | ||
|
||
__all__ = ["get_query_params_resolver"] |
62 changes: 62 additions & 0 deletions
62
ariadne_graphql_proxy/contrib/imgix/query_params_resolver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from functools import partial | ||
from typing import Any, Callable, Optional, Union, cast | ||
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse | ||
|
||
from graphql import GraphQLResolveInfo | ||
|
||
|
||
def get_attribute_value( | ||
obj: Any, info: GraphQLResolveInfo, attribute_str: str, **kwargs | ||
) -> Any: | ||
value = obj | ||
for attr in attribute_str.split("."): | ||
try: | ||
value = value.get(attr) | ||
except AttributeError: | ||
value = getattr(value, attr, None) | ||
return value | ||
|
||
|
||
def get_query_params_resolver( | ||
get_url: Union[str, Callable[..., str]], | ||
extra_params: Optional[dict[str, Any]] = None, | ||
get_params: Optional[Callable[..., dict[str, Any]]] = None, | ||
serialize_url: Optional[Callable[[str], Any]] = None, | ||
): | ||
get_source_url = cast( | ||
Callable[..., str], | ||
( | ||
get_url | ||
if callable(get_url) | ||
else partial(get_attribute_value, attribute_str=get_url) | ||
), | ||
) | ||
params = cast(dict[str, Any], extra_params if extra_params is not None else {}) | ||
get_params_from_kwargs = cast( | ||
Callable[..., dict[str, Any]], | ||
get_params if get_params is not None else lambda **kwargs: kwargs, | ||
) | ||
serialize = cast( | ||
Callable[[str], Any], | ||
serialize_url if serialize_url is not None else lambda url: url, | ||
) | ||
|
||
def resolver(obj: Any, info: GraphQLResolveInfo, **kwargs): | ||
source_url = get_source_url(obj, info, **kwargs) | ||
parse_result = urlparse(source_url) | ||
query_params = parse_qs(parse_result.query) | ||
query_params.update(params) | ||
query_params.update(get_params_from_kwargs(**kwargs)) | ||
result_url = urlunparse( | ||
( | ||
parse_result.scheme, | ||
parse_result.netloc, | ||
parse_result.path, | ||
parse_result.params, | ||
urlencode(query_params), | ||
parse_result.fragment, | ||
) | ||
) | ||
return serialize(result_url) | ||
|
||
return resolver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import cast | ||
|
||
from graphql import ( | ||
FieldDefinitionNode, | ||
GraphQLArgument, | ||
GraphQLField, | ||
GraphQLInputType, | ||
GraphQLOutputType, | ||
GraphQLSchema, | ||
assert_input_type, | ||
assert_output_type, | ||
parse, | ||
type_from_ast, | ||
value_from_ast, | ||
) | ||
|
||
|
||
def get_field_definition_from_str(field_str: str) -> FieldDefinitionNode: | ||
document = parse(f"type Placeholder{{ {field_str} }}") | ||
|
||
if len(document.definitions) != 1: | ||
raise ValueError("Field str has to define 1 type.") | ||
|
||
definition = document.definitions[0] | ||
|
||
fields = getattr(definition, "fields", []) | ||
if len(fields) != 1: | ||
raise ValueError("Field str has to provide only 1 field.") | ||
|
||
return fields[0] | ||
|
||
|
||
def get_graphql_field_from_field_definition( | ||
schema: GraphQLSchema, field_definition: FieldDefinitionNode | ||
) -> GraphQLField: | ||
field_type = cast(GraphQLOutputType, type_from_ast(schema, field_definition.type)) | ||
assert_output_type(field_type) | ||
|
||
field_args = {} | ||
for arg in field_definition.arguments: | ||
arg_type = cast(GraphQLInputType, type_from_ast(schema, arg.type)) | ||
assert_input_type(arg_type) | ||
arg_default_value = value_from_ast(value_node=arg.default_value, type_=arg_type) | ||
|
||
field_args[arg.name.value] = GraphQLArgument( | ||
type_=arg_type, | ||
default_value=arg_default_value, | ||
) | ||
|
||
description = ( | ||
None if not field_definition.description else field_definition.description.value | ||
) | ||
|
||
return GraphQLField( | ||
type_=field_type, | ||
args=field_args, | ||
description=description, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.