|
6 | 6 | GraphQLInputObjectField, GraphQLInputObjectType,
|
7 | 7 | GraphQLInt, GraphQLList, GraphQLNonNull,
|
8 | 8 | GraphQLObjectType, GraphQLSchema, GraphQLString)
|
| 9 | +from promise import Promise |
| 10 | + |
| 11 | +class CustomPromise(object): |
| 12 | + def __init__(self, fn=None, promise=None): |
| 13 | + self._promise = promise or Promise(fn) |
| 14 | + |
| 15 | + def get(self, _=None): |
| 16 | + raise NotImplementedError("Blocking for results not allowed. Use 'then' if you want to " |
| 17 | + "work with the result.") |
| 18 | + |
| 19 | + def then(self, success=None, failure=None): |
| 20 | + return self.__class__(promise=self._promise.then(success, failure)) |
| 21 | + |
| 22 | + def __getattr__(self, item): |
| 23 | + return getattr(self._promise, item) |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def fulfilled(cls, x): |
| 27 | + p = cls() |
| 28 | + p.fulfill(x) |
| 29 | + return p |
| 30 | + |
| 31 | + resolve = fulfilled |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def rejected(cls, reason): |
| 35 | + p = cls() |
| 36 | + p.reject(reason) |
| 37 | + return p |
9 | 38 |
|
10 | 39 |
|
11 | 40 | def _test_schema(test_field):
|
@@ -73,6 +102,34 @@ def resolver(source, args, *_):
|
73 | 102 | ]
|
74 | 103 |
|
75 | 104 |
|
| 105 | +def test_handles_resolved_promises(): |
| 106 | + def resolver(source, args, *_): |
| 107 | + return Promise.resolve('foo') |
| 108 | + |
| 109 | + schema = _test_schema(GraphQLField( |
| 110 | + GraphQLString, |
| 111 | + resolver=resolver |
| 112 | + )) |
| 113 | + |
| 114 | + result = graphql(schema, '{ test }', None) |
| 115 | + assert not result.errors |
| 116 | + assert result.data == {'test': 'foo'} |
| 117 | + |
| 118 | + |
| 119 | +def test_handles_resolved_custom_promises(): |
| 120 | + def resolver(source, args, *_): |
| 121 | + return CustomPromise.resolve('custom_foo') |
| 122 | + |
| 123 | + schema = _test_schema(GraphQLField( |
| 124 | + GraphQLString, |
| 125 | + resolver=resolver |
| 126 | + )) |
| 127 | + |
| 128 | + result = graphql(schema, '{ test }', None) |
| 129 | + assert not result.errors |
| 130 | + assert result.data == {'test': 'custom_foo'} |
| 131 | + |
| 132 | + |
76 | 133 | def test_maps_argument_out_names_well():
|
77 | 134 | def resolver(source, args, *_):
|
78 | 135 | return json.dumps([source, args], separators=(',', ':'))
|
|
0 commit comments