def undefined_var(var_name, l1, c1, op_name, l2, c2):
    return {
        "message":
        NoUndefinedVariables.undefined_var_message(var_name, op_name),
        "locations": [SourceLocation(l1, c1),
                      SourceLocation(l2, c2)],
    }
def duplicate_fragment(fragment_name, l1, c1, l2, c2):
    return {
        "message":
        UniqueFragmentNames.duplicate_fragment_name_message(fragment_name),
        "locations": [SourceLocation(l1, c1),
                      SourceLocation(l2, c2)],
    }
Ejemplo n.º 3
0
def undefined_var(var_name, l1, c1, op_name, l2, c2):
    return {
        'message':
        NoUndefinedVariables.undefined_var_message(var_name, op_name),
        'locations': [
            SourceLocation(l1, c1),
            SourceLocation(l2, c2),
        ]
    }
def test_string_non_null_boolean_in_directive():
    expect_fails_rule(
        VariablesInAllowedPosition, '''
      query Query($stringVar: String) {
        dog @include(if: $stringVar)
      }
    ''', [{
            'message':
            VariablesInAllowedPosition.bad_var_pos_message(
                'stringVar', 'String', 'Boolean!'),
            'locations': [SourceLocation(2, 19),
                          SourceLocation(3, 26)]
        }])
def test_string_string_fail():
    expect_fails_rule(
        VariablesInAllowedPosition, '''
      query Query($stringVar: String) {
        complicatedArgs {
          stringListArgField(stringListArg: $stringVar)
        }
      }
    ''', [{
            'message':
            VariablesInAllowedPosition.bad_var_pos_message(
                'stringVar', 'String', '[String]'),
            'locations': [SourceLocation(2, 19),
                          SourceLocation(4, 45)]
        }])
def test_string_over_boolean():
    expect_fails_rule(
        VariablesInAllowedPosition, '''
      query Query($stringVar: String) {
        complicatedArgs {
          booleanArgField(booleanArg: $stringVar)
        }
      }
    ''', [{
            'message':
            VariablesInAllowedPosition.bad_var_pos_message(
                'stringVar', 'String', 'Boolean'),
            'locations': [SourceLocation(2, 19),
                          SourceLocation(4, 39)]
        }])
def test_int_non_null_int():
    expect_fails_rule(
        VariablesInAllowedPosition, '''
      query Query($intArg: Int) {
        complicatedArgs {
          nonNullIntArgField(nonNullIntArg: $intArg)
        }
      }
    ''', [{
            'message':
            VariablesInAllowedPosition.bad_var_pos_message(
                'intArg', 'Int', 'Int!'),
            'locations': [SourceLocation(4, 45),
                          SourceLocation(2, 19)]
        }])
Ejemplo n.º 8
0
def test_parse_provides_useful_errors():
    # type: () -> None
    with raises(GraphQLSyntaxError) as excinfo:
        parse("""{""")
    assert (u"Syntax Error GraphQL (1:2) Expected Name, found EOF\n"
            u"\n"
            u"1: {\n"
            u"    ^\n"
            u"") == excinfo.value.message

    assert excinfo.value.positions == [1]
    assert excinfo.value.locations == [SourceLocation(line=1, column=2)]

    with raises(GraphQLSyntaxError) as excinfo:
        parse("""{ ...MissingOn }
fragment MissingOn Type
""")
    assert 'Syntax Error GraphQL (2:20) Expected "on", found Name "Type"' in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse("{ field: {} }")
    assert "Syntax Error GraphQL (1:10) Expected Name, found {" in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse("notanoperation Foo { field }")
    assert 'Syntax Error GraphQL (1:1) Unexpected Name "notanoperation"' in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse("...")
    assert "Syntax Error GraphQL (1:1) Unexpected ..." in str(excinfo.value)
def unknown_arg(arg_name, field_name, type_name, line, column):
    return {
        'message':
        KnownArgumentNames.unknown_arg_message(arg_name, field_name,
                                               type_name),
        'locations': [SourceLocation(line, column)]
    }
def unknown_directive_arg(arg_name, directive_name, line, column):
    return {
        'message':
        KnownArgumentNames.unknown_directive_arg_message(
            arg_name, directive_name),
        'locations': [SourceLocation(line, column)]
    }
Ejemplo n.º 11
0
def default_for_non_null_arg(var_name, type_name, guess_type_name, line, column):
    return {
        "message": DefaultValuesOfCorrectType.default_for_non_null_arg_message(
            var_name, type_name, guess_type_name
        ),
        "locations": [SourceLocation(line, column)],
    }
Ejemplo n.º 12
0
def test_parse_provides_useful_errors():
    with raises(GraphQLSyntaxError) as excinfo:
        parse("""{""")
    assert (u'Syntax Error GraphQL (1:2) Expected Name, found EOF\n'
            u'\n'
            u'1: {\n'
            u'    ^\n'
            u'') == excinfo.value.message

    assert excinfo.value.positions == [1]
    assert excinfo.value.locations == [SourceLocation(line=1, column=2)]

    with raises(GraphQLSyntaxError) as excinfo:
        parse("""{ ...MissingOn }
fragment MissingOn Type
""")
    assert 'Syntax Error GraphQL (2:20) Expected "on", found Name "Type"' in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse('{ field: {} }')
    assert 'Syntax Error GraphQL (1:10) Expected Name, found {' in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse('notanoperation Foo { field }')
    assert 'Syntax Error GraphQL (1:1) Unexpected Name "notanoperation"' in str(
        excinfo.value)

    with raises(GraphQLSyntaxError) as excinfo:
        parse('...')
    assert 'Syntax Error GraphQL (1:1) Unexpected ...' in str(excinfo.value)
Ejemplo n.º 13
0
def fragment_on_non_composite_error(frag_name, type_name, line, column):
    return {
        "message": FragmentsOnCompositeTypes.fragment_on_non_composite_error_message(
            frag_name, type_name
        ),
        "locations": [SourceLocation(line, column)],
    }
def test_string_non_null_boolean_in_directive():
    expect_fails_rule(
        VariablesInAllowedPosition,
        """
      query Query($stringVar: String) {
        dog @include(if: $stringVar)
      }
    """,
        [{
            "message":
            VariablesInAllowedPosition.bad_var_pos_message(
                "stringVar", "String", "Boolean!"),
            "locations": [SourceLocation(2, 19),
                          SourceLocation(3, 26)],
        }],
    )
def error(frag_name, parent_type, frag_type, line, column):
    return {
        "message":
        PossibleFragmentSpreads.type_incompatible_spread_message(
            frag_name, parent_type, frag_type),
        "locations": [SourceLocation(line, column)],
    }
Ejemplo n.º 16
0
def error_anon(parent_type, frag_type, line, column):
    return {
        'message':
        PossibleFragmentSpreads.type_incompatible_anon_spread_message(
            parent_type, frag_type),
        'locations': [SourceLocation(line, column)]
    }
Ejemplo n.º 17
0
def test_encode_execution_results_with_error():
    execution_results = [
        ExecutionResult(
            None,
            [
                GraphQLError("Some error",
                             locations=[SourceLocation(1, 2)],
                             path=["somePath"])
            ],
        ),
        ExecutionResult({"result": 42}, None),
    ]

    output = encode_execution_results(execution_results)
    assert isinstance(output, ServerResponse)
    assert isinstance(output.body, str)
    assert isinstance(output.status_code, int)
    assert json.loads(output.body) == {
        "data":
        None,
        "errors": [{
            "message": "Some error",
            "locations": [{
                "line": 1,
                "column": 2
            }],
            "path": ["somePath"],
        }],
    }
    assert output.status_code == 200
Ejemplo n.º 18
0
def non_input_type_on_variable(variable_name, type_name, line, col):
    return {
        "message":
        VariablesAreInputTypes.non_input_type_on_variable_message(
            variable_name, type_name),
        "locations": [SourceLocation(line, col)],
    }
def missing_field_arg(field_name, arg_name, type_name, line, column):
    return {
        "message":
        ProvidedNonNullArguments.missing_field_arg_message(
            field_name, arg_name, type_name),
        "locations": [SourceLocation(line, column)],
    }
Ejemplo n.º 20
0
def unknown_directive_arg(arg_name, directive_name, suggested_args, line,
                          column):
    return {
        "message":
        _unknown_directive_arg_message(arg_name, directive_name,
                                       suggested_args),
        "locations": [SourceLocation(line, column)],
    }
def undefined_field(field, gql_type, suggested_types, suggested_fields, line,
                    column):
    return {
        "message":
        _undefined_field_message(field, gql_type, suggested_types,
                                 suggested_fields),
        "locations": [SourceLocation(line, column)],
    }
Ejemplo n.º 22
0
def undefined_field(field, gql_type, suggested_types, suggested_fields, line,
                    column):
    return {
        'message':
        _undefined_field_message(field, gql_type, suggested_types,
                                 suggested_fields),
        'locations': [SourceLocation(line, column)]
    }
Ejemplo n.º 23
0
def unknown_directive_arg(arg_name, directive_name, suggested_args, line,
                          column):
    return {
        'message':
        _unknown_directive_arg_message(arg_name, directive_name,
                                       suggested_args),
        'locations': [SourceLocation(line, column)]
    }
def test_int_non_null_int_within_fragment():
    expect_fails_rule(
        VariablesInAllowedPosition, '''
      fragment nonNullIntArgFieldFrag on ComplicatedArgs {
        nonNullIntArgField(nonNullIntArg: $intArg)
      }
      query Query($intArg: Int) {
        complicatedArgs {
          ...nonNullIntArgFieldFrag
        }
      }
    ''', [{
            'message':
            VariablesInAllowedPosition.bad_var_pos_message(
                'intArg', 'Int', 'Int!'),
            'locations': [SourceLocation(5, 19),
                          SourceLocation(3, 43)]
        }])
def test_int_non_null_int():
    expect_fails_rule(
        VariablesInAllowedPosition,
        """
      query Query($intArg: Int) {
        complicatedArgs {
          nonNullIntArgField(nonNullIntArg: $intArg)
        }
      }
    """,
        [{
            "message":
            VariablesInAllowedPosition.bad_var_pos_message(
                "intArg", "Int", "Int!"),
            "locations": [SourceLocation(4, 45),
                          SourceLocation(2, 19)],
        }],
    )
def test_string_over_boolean():
    expect_fails_rule(
        VariablesInAllowedPosition,
        """
      query Query($stringVar: String) {
        complicatedArgs {
          booleanArgField(booleanArg: $stringVar)
        }
      }
    """,
        [{
            "message":
            VariablesInAllowedPosition.bad_var_pos_message(
                "stringVar", "String", "Boolean"),
            "locations": [SourceLocation(2, 19),
                          SourceLocation(4, 39)],
        }],
    )
def test_string_string_fail():
    expect_fails_rule(
        VariablesInAllowedPosition,
        """
      query Query($stringVar: String) {
        complicatedArgs {
          stringListArgField(stringListArg: $stringVar)
        }
      }
    """,
        [{
            "message":
            VariablesInAllowedPosition.bad_var_pos_message(
                "stringVar", "String", "[String]"),
            "locations": [SourceLocation(2, 19),
                          SourceLocation(4, 45)],
        }],
    )
def bad_value(var_name, type_name, value, line, column, errors=None):
    if not errors:
        errors = ['Expected type "{}", found {}.'.format(type_name, value)]

    return {
        'message':
        DefaultValuesOfCorrectType.bad_value_for_default_arg_message(
            var_name, type_name, value, errors),
        'locations': [SourceLocation(line, column)]
    }
def bad_value(arg_name, type_name, value, line, column, errors=None):
    if not errors:
        errors = [u'Expected type "{}", found {}.'.format(type_name, value)]

    return {
        "message":
        ArgumentsOfCorrectType.bad_value_message(arg_name, type_name, value,
                                                 errors),
        "locations": [SourceLocation(line, column)],
    }
def test_int_non_null_int_within_fragment():
    expect_fails_rule(
        VariablesInAllowedPosition,
        """
      fragment nonNullIntArgFieldFrag on ComplicatedArgs {
        nonNullIntArgField(nonNullIntArg: $intArg)
      }
      query Query($intArg: Int) {
        complicatedArgs {
          ...nonNullIntArgFieldFrag
        }
      }
    """,
        [{
            "message":
            VariablesInAllowedPosition.bad_var_pos_message(
                "intArg", "Int", "Int!"),
            "locations": [SourceLocation(5, 19),
                          SourceLocation(3, 43)],
        }],
    )