Skip to content

Commit

Permalink
Extract common helper functions. Fix docstrings to reflect operator b…
Browse files Browse the repository at this point in the history
…ehavior.
  • Loading branch information
obi1kenobi committed Oct 23, 2017
1 parent c1e0ce6 commit d2d98d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
11 changes: 3 additions & 8 deletions graphql_compiler/compiler/compiler_frontend.py
Expand Up @@ -71,8 +71,8 @@
validate_vertex_field_directive_interactions)
from .filters import process_filter_directive
from .helpers import (FoldScopeLocation, Location, get_ast_field_name, get_field_type_from_schema,
get_uniquely_named_objects_by_name, strip_non_null_from_type,
validate_safe_string)
get_uniquely_named_objects_by_name, is_vertex_field_name,
strip_non_null_from_type, validate_safe_string)


# The OutputMetadata will have the following types for its members:
Expand All @@ -91,11 +91,6 @@ def __ne__(self, other):
return not self.__eq__(other)


def _is_vertex_field_name(field_name):
"""Return True if the field name denotes a vertex field, or False if it's a property field."""
return field_name.startswith('out_') or field_name.startswith('in_')


def _get_fields(ast):
"""Return a list of property fields, and a list of vertex fields, for the given AST node.
Expand Down Expand Up @@ -132,7 +127,7 @@ def _get_fields(ast):
seen_field_names.add(name)

# Vertex fields start with 'out_' or 'in_', denoting the edge direction to that vertex.
if _is_vertex_field_name(name):
if is_vertex_field_name(name):
switched_to_vertices = True
vertex_fields.append(field_ast)
else:
Expand Down
24 changes: 17 additions & 7 deletions graphql_compiler/compiler/filters.py
Expand Up @@ -81,6 +81,16 @@ def wrapper(schema, current_schema_type, ast, context,
return decorator


def _is_variable_argument(argument_name):
"""Return True if the argument is a runtime variable, and False otherwise."""
return argument_name.startswith('$')


def _is_tag_argument(argument_name):
"""Return True if the argument is a tagged value, and False otherwise."""
return argument_name.startswith('%')


def _represent_argument(schema, ast, context, argument, inferred_type):
"""Return a two-element tuple that represents the argument to the directive being processed.
Expand All @@ -106,7 +116,7 @@ def _represent_argument(schema, ast, context, argument, inferred_type):
argument_name = argument[1:]
validate_safe_string(argument_name)

if argument.startswith('$'):
if _is_variable_argument(argument):
existing_type = context['inputs'].get(argument_name, inferred_type)
if not inferred_type.is_same_type(existing_type):
raise GraphQLCompilationError(u'Incompatible types inferred for argument {}. '
Expand All @@ -116,7 +126,7 @@ def _represent_argument(schema, ast, context, argument, inferred_type):
context['inputs'][argument_name] = inferred_type

return (expressions.Variable(argument, inferred_type), None)
elif argument.startswith('%'):
elif _is_tag_argument(argument):
argument_context = context['tags'].get(argument_name, None)
if argument_context is None:
raise GraphQLCompilationError(u'Undeclared argument used: {}'.format(argument))
Expand Down Expand Up @@ -164,7 +174,7 @@ def _process_comparison_filter_directive(schema, current_schema_type, ast,
is optional, etc.). May be mutated in-place in this function!
directive: GraphQL directive object, obtained from the AST node
parameters: list of 1 element, containing the value to perform the comparison against;
if the parameter is optional and missing, the check is performed against 'null'
if the parameter is optional and missing, the check will return True
operator: unicode, a comparison operator, like '=', '!=', '>=' etc.
This is a kwarg only to preserve the same positional arguments in the
function signature, to ease validation.
Expand Down Expand Up @@ -211,7 +221,7 @@ def _process_name_or_alias_filter_directive(schema, current_schema_type, ast,
is optional, etc.). May be mutated in-place in this function!
directive: GraphQL directive object, obtained from the AST node
parameters: list of 1 element, containing the value to check the name or alias against;
if the parameter is optional and missing, the check is performed against 'null'
if the parameter is optional and missing, the check will return True
Returns:
a Filter basic block that performs the check against the name or alias
Expand Down Expand Up @@ -326,7 +336,7 @@ def _process_in_collection_filter_directive(schema, current_schema_type, ast,
is optional, etc.). May be mutated in-place in this function!
directive: GraphQL directive object, obtained from the AST node
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check is assumed to be False
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the collection existence check
Expand Down Expand Up @@ -362,7 +372,7 @@ def _process_has_substring_filter_directive(schema, current_schema_type, ast,
is optional, etc.). May be mutated in-place in this function!
directive: GraphQL directive object, obtained from the AST node
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check is assumed to be False
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the substring check
Expand Down Expand Up @@ -401,7 +411,7 @@ def _process_contains_filter_directive(schema, current_schema_type, ast,
is optional, etc.). May be mutated in-place in this function!
directive: GraphQL directive object, obtained from the AST node
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check is assumed to be False
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the substring check
Expand Down
11 changes: 11 additions & 0 deletions graphql_compiler/compiler/helpers.py
Expand Up @@ -48,6 +48,17 @@ def strip_non_null_from_type(graphql_type):
return graphql_type


def is_vertex_field_name(field_name):
"""Return True if the field's name indicates it is a non-root vertex field."""
return field_name.startswith('out_') or field_name.startswith('in_')


def is_non_root_vertex_field(ast, graphql_type):
"""Return True if the AST belongs to a vertex field that is not the root vertex."""
field_name = get_ast_field_name(ast)
return is_vertex_field_name(field_name) and is_vertex_field_type(graphql_type)


def is_vertex_field_type(graphql_type):
"""Return True if the argument is a vertex field type, and False otherwise."""
# This will need to change if we ever support complex embedded types or edge field types.
Expand Down

0 comments on commit d2d98d0

Please sign in to comment.