Beispiel #1
0
def test_gevent_executor_with_error():
    doc = 'query Example { a, b }'

    @run_in_greenlet
    def resolver(context, *_):
        gevent.sleep(0.001)
        return 'hey'

    @run_in_greenlet
    def resolver_2(context, *_):
        gevent.sleep(0.003)
        raise Exception('resolver_2 failed!')

    Type = GraphQLObjectType(
        'Type', {
            'a': GraphQLField(GraphQLString, resolver=resolver),
            'b': GraphQLField(GraphQLString, resolver=resolver_2)
        })

    executor = Executor([GeventExecutionMiddleware()])
    result = executor.execute(GraphQLSchema(Type), doc)
    formatted_errors = list(map(format_error, result.errors))
    assert formatted_errors == [{
        'locations': [{
            'line': 1,
            'column': 20
        }],
        'message': 'resolver_2 failed!'
    }]
    assert result.data == {'a': 'hey', 'b': None}
Beispiel #2
0
def test_gevent_executor():
    @run_in_greenlet
    def resolver(context, *_):
        gevent.sleep(0.001)
        return 'hey'

    @run_in_greenlet
    def resolver_2(context, *_):
        gevent.sleep(0.003)
        return 'hey2'

    def resolver_3(contest, *_):
        return 'hey3'

    Type = GraphQLObjectType(
        'Type', {
            'a': GraphQLField(GraphQLString, resolver=resolver),
            'b': GraphQLField(GraphQLString, resolver=resolver_2),
            'c': GraphQLField(GraphQLString, resolver=resolver_3)
        })

    doc = '{ a b c }'
    executor = Executor([GeventExecutionMiddleware()])
    result = executor.execute(GraphQLSchema(Type), doc)
    assert not result.errors
    assert result.data == {'a': 'hey', 'b': 'hey2', 'c': 'hey3'}
Beispiel #3
0
def test_gevent_executor():
    doc = 'query Example { a, b }'

    @run_in_greenlet
    def resolver(context, *_):
        gevent.sleep(0.001)
        return 'hey'

    @run_in_greenlet
    def resolver_2(context, *_):
        gevent.sleep(0.003)
        return 'hey2'

    Type = GraphQLObjectType(
        'Type', {
            'a': GraphQLField(GraphQLString, resolver=resolver),
            'b': GraphQLField(GraphQLString, resolver=resolver_2)
        })

    executor = Executor(GraphQLSchema(Type), [GeventExecutionMiddleware()])
    result = executor.execute(doc)
    assert not result.errors
    assert result.data == {'a': 'hey', 'b': 'hey2'}
Beispiel #4
0

class Patron(graphene.ObjectType):
    id = graphene.ID()
    name = graphene.String()
    age = graphene.ID()


class Query(graphene.ObjectType):

    patron = graphene.Field(Patron)

    @run_in_greenlet
    def resolve_patron(self, args, info):
        return Patron(id=1, name='Demo')


gevent_executor = Executor([GeventExecutionMiddleware()], map_type=OrderedDict)

schema = graphene.Schema(query=Query, executor=gevent_executor)
query = '''
    query something{
      patron {
        id
        name
      }
    }
'''
result = schema.execute(query)
print(result.data['patron'])