def test_executes_interface_types_with_inline_fragment():
    # This is the valid version of the query in the above test.
    ast = parse('''
      {
        __typename
        name
        friends {
          __typename
          name
          ... on Dog {
            barks
          }
          ... on Cat {
            meows
          }
        }
      }
    ''')
    result = execute(schema, john, ast)
    assert not result.errors
    assert result.data == {
        '__typename': 'Person',
        'name': 'John',
        'friends': [
            {'__typename': 'Person', 'name': 'Liz'},
            {'__typename': 'Dog', 'name': 'Odie', 'barks': True}
        ]
    }
示例#2
0
def test_evaluates_mutations_serially():
    doc = '''mutation M {
      first: immediatelyChangeTheNumber(newNumber: 1) {
        theNumber
      },
      second: promiseToChangeTheNumber(newNumber: 2) {
        theNumber
      },
      third: immediatelyChangeTheNumber(newNumber: 3) {
        theNumber
      }
      fourth: promiseToChangeTheNumber(newNumber: 4) {
        theNumber
      },
      fifth: immediatelyChangeTheNumber(newNumber: 5) {
        theNumber
      }
    }'''
    ast = parse(doc)
    result = execute(schema, Root(6), ast, 'M')
    assert not result.errors
    assert result.data == \
        {
            'first': {'theNumber': 1},
            'second': {'theNumber': 2},
            'third': {'theNumber': 3},
            'fourth': {'theNumber': 4},
            'fifth': {'theNumber': 5},
        }
def test_executes_union_types_with_inline_fragment():
    # This is the valid version of the query in the above test.
    ast = parse('''
      {
        __typename
        name
        pets {
          __typename
          ... on Dog {
            name
            barks
          }
          ... on Cat {
            name
            meows
          }
        }
      }
    ''')
    result = execute(schema, john, ast)
    assert not result.errors
    assert result.data == {
        '__typename': 'Person',
        'name': 'John',
        'pets': [
            {'__typename': 'Cat', 'name': 'Garfield', 'meows': False},
            {'__typename': 'Dog', 'name': 'Odie', 'barks': True}
        ]
    }
示例#4
0
def test_supports_the_type_root_field():
    TestType = GraphQLObjectType("TestType", {"testField": GraphQLField(GraphQLString)})
    schema = GraphQLSchema(TestType)
    request = '{ __type(name: "TestType") { name } }'
    result = execute(schema, object(), parse(request))
    assert not result.errors
    assert result.data == {"__type": {"name": "TestType"}}
示例#5
0
def test_when_argument_provided_cannot_be_coerced():
    ast = parse('''{
        fieldWithDefaultArgumentValue(input: WRONG_TYPE)
    }''')
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == {'fieldWithDefaultArgumentValue': '"Hello World"'}
示例#6
0
def test_when_nullable_variable_provided():
    ast = parse('''query optionalVariable($optional: String) {
        fieldWithDefaultArgumentValue(input: $optional)
    }''')
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == {'fieldWithDefaultArgumentValue': '"Hello World"'}
示例#7
0
def test_correctly_threads_arguments():
    doc = '''
        query Example {
            b(numArg: 123, stringArg: "foo")
        }
    '''

    def resolver(_, args, *_args):
        assert args['numArg'] == 123
        assert args['stringArg'] == 'foo'
        resolver.got_here = True

    resolver.got_here = False

    doc_ast = parse(doc)

    Type = GraphQLObjectType('Type', {
        'b': GraphQLField(
            GraphQLString,
            args={
                'numArg': GraphQLArgument(GraphQLInt),
                'stringArg': GraphQLArgument(GraphQLString),
            },
            resolver=resolver),
    })

    result = execute(GraphQLSchema(Type), None, doc_ast, 'Example', {})
    assert not result.errors
    assert resolver.got_here
示例#8
0
def test_inline_does_not_use_incorrect_value():
    doc = '''
    {
        fieldWithObjectInput(input: ["foo", "bar", "baz"])
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert json.loads(result.data['fieldWithObjectInput']) is None
示例#9
0
def test_allows_lists_to_contain_values():
    doc = '''
        query q($input:[String]) {
          list(input: $input)
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast, None, {'input': ['A']})
    assert not result.errors
    assert result.data == {'list': '["A"]'}
示例#10
0
def test_does_not_allow_non_null_lists_to_be_null():
    doc = '''
        query q($input:[String]!) {
          nnList(input: $input)
        }
    '''
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, {'input': None})
    e = excinfo.value
示例#11
0
def test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null():
    doc = '''
        query q($input:[String!]!) {
          nnListNN(input: $input)
        }
    '''
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, {'input': ['A', None, 'B']})
    e = excinfo.value
示例#12
0
def test_does_not_allow_unknown_types_to_be_used_as_values():
    doc = '''
        query q($input: UnknownType!) {
          fieldWithObjectInput(input: $input)
        }
    '''
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast)
    e = excinfo.value
示例#13
0
def test_allows_lists_of_non_nulls_to_be_null():
    doc = '''
        query q($input:[String!]) {
          listNN(input: $input)
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast, None, {'input': None})
    assert not result.errors
    assert result.data == {'listNN': 'null'}
示例#14
0
def test_allows_non_null_lists_of_non_nulls_to_contain_values():
    doc = '''
        query q($input:[String!]!) {
          nnListNN(input: $input)
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast, None, {'input': ['A']})
    assert not result.errors
    assert result.data == {'nnListNN': '["A"]'}
示例#15
0
def test_does_not_allow_non_nullable_inputs_to_be_set_to_null_in_a_variable():
    doc = '''
        query SetsNonNullable($value: String!) {
          fieldWithNonNullableStringInput(input: $value)
        }
    '''
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, {'value': None})
    e = excinfo.value
示例#16
0
def test_allows_non_null_lists_to_contain_null():
    doc = '''
        query q($input:[String]!) {
          nnList(input: $input)
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast, None, {'input': ['A', None, 'B']})
    assert not result.errors
    assert result.data == {'nnList': '["A", null, "B"]'}
示例#17
0
def test_variable_uses_default_value_when_not_provided():
    with_defaults_ast = '''
    query q($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {
        fieldWithObjectInput(input: $input)
    }
    '''
    ast = parse(with_defaults_ast)
    result = execute(schema, None, ast)
    assert not result.errors
    assert json.loads(result.data['fieldWithObjectInput']) == \
           {"a": "foo", "b": ["bar"], "c": "baz"}
示例#18
0
def test_errors_on_addition_of_unknown_input_field():
    doc = '''
    query q($input:TestInputObject) {
        fieldWithObjectInput(input: $input)
    }
    '''
    params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz', 'd': 'dog'}}
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, params)
    e = excinfo.value
示例#19
0
def test_inline_properly_coerces_single_value_to_array():
    doc = '''
    {
        fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"})
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert json.loads(result.data['fieldWithObjectInput']) == \
           {"a": "foo", "b": ["bar"], "c": "baz"}
示例#20
0
def test_passes_along_null_for_non_nullable_inputs_if_explicitly_set_in_the_query():
    doc = '''
    {
        fieldWithNonNullableStringInput
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == \
           {"fieldWithNonNullableStringInput": 'null'}
示例#21
0
def test_allows_non_nullable_inputs_to_be_set_to_a_value_directly():
    doc = '''
        {
          fieldWithNonNullableStringInput(input: "a")
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == \
           {"fieldWithNonNullableStringInput": '"a"'}
示例#22
0
def test_uses_the_only_operation_if_no_operation_is_provided():
    doc = 'query Example { a }'
    class Data(object):
        a = 'b'
    ast = parse(doc)
    Type = GraphQLObjectType('Type', {
        'a': GraphQLField(GraphQLString)
    })
    result = execute(GraphQLSchema(Type), Data(), ast)
    assert not result.errors
    assert result.data == {'a': 'b'}
示例#23
0
def test_allows_nullable_inputs_to_be_omitted_in_an_unlisted_variable():
    doc = '''
    query SetsNullable {
        fieldWithNullableStringInput(input: $value)
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == \
           {"fieldWithNullableStringInput": "null"}
示例#24
0
def test_allows_nullable_inputs_to_be_omitted():
    doc = '''
    {
        fieldWithNullableStringInput
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert result.data == \
           {"fieldWithNullableStringInput": "null"}
示例#25
0
def test_raises_the_inline_operation_if_no_operation_is_provided():
    doc = 'query Example { a } query OtherExample { a }'
    class Data(object):
        a = 'b'
    ast = parse(doc)
    Type = GraphQLObjectType('Type', {
        'a': GraphQLField(GraphQLString)
    })
    with raises(GraphQLError) as excinfo:
        execute(GraphQLSchema(Type), Data(), ast)
    assert 'Must provide operation name if query contains multiple operations' in str(excinfo.value)
示例#26
0
def test_errors_on_incorrect_type():
    doc = '''
    query q($input:TestInputObject) {
        fieldWithObjectInput(input: $input)
    }
    '''
    params = {'input': 'foo bar'}
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, params)
    e = excinfo.value
示例#27
0
def test_inline_executes_with_complex_input():
    doc = '''
    {
      fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"})
    }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast)
    assert not result.errors
    assert json.loads(result.data['fieldWithObjectInput']) == \
        {"a": "foo", "b": ["bar"], "c": "baz"}
示例#28
0
def test_errors_on_omission_of_nested_non_null():
    doc = '''
    query q($input:TestInputObject) {
        fieldWithObjectInput(input: $input)
    }
    '''
    params = {'input': {'a': 'foo', 'b': 'bar'}}
    ast = parse(doc)
    with raises(GraphQLError) as excinfo:
        execute(schema, None, ast, None, params)
    e = excinfo.value
示例#29
0
def test_allows_non_nullable_inputs_to_be_set_to_a_value_in_a_variable():
    doc = '''
        query SetsNonNullable($value: String!) {
          fieldWithNonNullableStringInput(input: $value)
        }
    '''
    ast = parse(doc)
    result = execute(schema, None, ast, None, {'value': 'a'})
    assert not result.errors
    assert result.data == \
           {"fieldWithNonNullableStringInput": '"a"'}
示例#30
0
def test_variable_properly_coerces_single_value_to_array():
    doc = '''
    query q($input:TestInputObject) {
        fieldWithObjectInput(input: $input)
    }
    '''
    params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz'}}
    ast = parse(doc)
    result = execute(schema, None, ast, None, params)
    assert not result.errors
    assert json.loads(result.data['fieldWithObjectInput']) == \
           {"a": "foo", "b": ["bar"], "c": "baz"}