def _replace_tag_names_in_tag_directive(name_change_map, tag_directive):
    """Apply tag parameter renaming to the given tag directive.

    Args:
        name_change_map: Dict[str, str] mapping tag names to new names
        tag_directive: GraphQL library tag directive whose name is in the name_change_map.
                       This ast is not mutated.

    Returns:
        GraphQL library directive object, equivalent to the input one, with its name changed
        according to the name_change_map. If no changes were made, this is the same object
        as the input tag directive.
    """
    # Schema validation has ensured this exists
    current_name = tag_directive.arguments[0].value.value
    new_name = name_change_map[current_name]

    if new_name == current_name:
        # No changes are necessary, return the original input object.
        return tag_directive

    renamed_tag_directive = copy(tag_directive)
    renamed_tag_directive.arguments = [
        Argument(Name('tag_name'), StringValue(new_name))
    ]
    return renamed_tag_directive
Exemple #2
0
def _get_minimal_query_ast_from_macro_ast(macro_ast):
    """Get a query that should successfully compile to IR if the macro is valid."""
    ast_without_macro_directives = remove_directives_from_ast(macro_ast, {
        directive.name
        for directive in DIRECTIVES_REQUIRED_IN_MACRO_EDGE_DEFINITION
    })

    # We will add this output directive to make the ast a valid query
    output_directive = Directive(Name('output'), arguments=[
        Argument(Name('out_name'), StringValue('dummy_output_name'))
    ])

    # Shallow copy everything on the path to the first level selection list
    query_ast = copy(ast_without_macro_directives)
    root_level_selection = copy(get_only_selection_from_ast(query_ast, GraphQLInvalidMacroError))
    first_level_selections = copy(root_level_selection.selection_set.selections)

    # Add an output to a new or existing __typename field
    existing_typename_field = None
    for idx, selection in enumerate(first_level_selections):
        if isinstance(selection, Field):
            if selection.name.value == '__typename':
                # We have a copy of the list, but the elements are references to objects
                # in macro_ast that we don't want to mutate. So the following copy is necessary.
                existing_typename_field = copy(selection)
                existing_typename_field.directives = copy(existing_typename_field.directives)
                existing_typename_field.directives.append(output_directive)
                first_level_selections[idx] = existing_typename_field
    if existing_typename_field is None:
        first_level_selections.insert(0, Field(Name('__typename'), directives=[output_directive]))

    # Propagate the changes back to the result_ast
    root_level_selection.selection_set = SelectionSet(first_level_selections)
    query_ast.selection_set = SelectionSet([root_level_selection])
    return Document([query_ast])
Exemple #3
0
def _get_in_collection_filter_directive(input_filter_name):
    """Create a @filter directive with in_collecion operation and the desired variable name."""
    return Directive(
        name=Name(value=FilterDirective.name),
        arguments=[
            Argument(
                name=Name(value='op_name'),
                value=StringValue(value='in_collection'),
            ),
            Argument(
                name=Name(value='value'),
                value=ListValue(values=[
                    StringValue(value=u'$' + input_filter_name),
                ], ),
            ),
        ],
    )
def _replace_tag_names_in_filter_directive(name_change_map, filter_directive):
    """Apply tag parameter renaming to the given filter directive.

    Args:
        name_change_map: Dict[str, str] mapping tag names to new names
        filter_directive: GraphQL library filter directive object that potentially uses
                          tagged parameters. All such tagged parameters should be in
                          the name_change_map. This directive object is not mutated,
                          and if no changes are necessary then it will be returned

    Returns:
        GraphQL library directive object, equivalent to the input one, with any tagged parameters it
        uses replaced according to the name_change_map. If no changes were made, this is the
        same object as the input filter directive.
    """
    made_changes = False

    new_arguments = []
    for argument in filter_directive.arguments:
        if argument.name.value == 'op_name':
            new_arguments.append(argument)
        elif argument.name.value == 'value':
            new_value_list = []
            for value in argument.value.values:
                parameter = value.value
                new_value = value

                # Rewrite tagged parameter names if necessary.
                if is_tagged_parameter(parameter):
                    current_name = get_parameter_name(parameter)
                    new_name = name_change_map[current_name]
                    if new_name != current_name:
                        made_changes = True
                        new_value = StringValue('%' + new_name)

                new_value_list.append(new_value)

            if made_changes:
                new_argument = Argument(Name('value'),
                                        value=ListValue(new_value_list))
            else:
                new_argument = argument
            new_arguments.append(new_argument)
        else:
            raise AssertionError(
                u'Unknown argument name {} in filter directive {}, this should '
                u'have been caught in an earlier validation step.'.format(
                    argument.name.value, filter_directive))

    if not made_changes:
        # No changes were made, return the original input object.
        return filter_directive

    filter_with_renamed_args = copy(filter_directive)
    filter_with_renamed_args.arguments = new_arguments
    return filter_with_renamed_args
Exemple #5
0
def test_json_string():
    test_json_string = '{"foo": "bar"}'

    test_object = JSONString().parse_value(test_json_string)
    assert test_object == {'foo': 'bar'}

    assert JSONString.serialize(test_object) == test_json_string

    node = StringValue(test_json_string)
    test_object = JSONString.parse_literal(node)
    assert test_object == {'foo': 'bar'}
def _get_output_directive(out_name):
    """Return a Directive representing an @output with the input out_name."""
    return Directive(
        name=Name(value=OutputDirective.name),
        arguments=[
            Argument(
                name=Name(value=u'out_name'),
                value=StringValue(value=out_name),
            ),
        ],
    )
def test_date_time():
    test_iso_string = "2016-04-29T18:34:12.502Z"

    def check_datetime(test_dt):
        assert test_dt.tzinfo == iso8601.UTC
        assert test_dt.year == 2016
        assert test_dt.month == 4
        assert test_dt.day == 29
        assert test_dt.hour == 18
        assert test_dt.minute == 34
        assert test_dt.second == 12

    test_dt = DateTime().parse_value(test_iso_string)
    check_datetime(test_dt)

    assert DateTime.serialize(test_dt) == "2016-04-29T18:34:12.502000+00:00"

    node = StringValue(test_iso_string)
    test_dt = DateTime.parse_literal(node)
    check_datetime(test_dt)