|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { describe, it } from 'mocha';
|
3 | 3 |
|
| 4 | +import type { PromiseOrValue } from '../../jsutils/PromiseOrValue'; |
| 5 | + |
4 | 6 | import { parse } from '../../language/parser';
|
| 7 | +import type { GraphQLFieldResolver } from '../../type/definition'; |
| 8 | +import { GraphQLList, GraphQLObjectType } from '../../type/definition'; |
| 9 | +import { GraphQLString } from '../../type/scalars'; |
| 10 | +import { GraphQLSchema } from '../../type/schema'; |
5 | 11 |
|
6 | 12 | import { buildSchema } from '../../utilities/buildASTSchema';
|
7 | 13 |
|
| 14 | +import type { ExecutionResult } from '../execute'; |
8 | 15 | import { execute, executeSync } from '../execute';
|
9 | 16 |
|
10 | 17 | describe('Execute: Accepts any iterable as list value', () => {
|
@@ -64,6 +71,146 @@ describe('Execute: Accepts any iterable as list value', () => {
|
64 | 71 | });
|
65 | 72 | });
|
66 | 73 |
|
| 74 | +describe('Execute: Accepts async iterables as list value', () => { |
| 75 | + function complete(rootValue: unknown) { |
| 76 | + return execute({ |
| 77 | + schema: buildSchema('type Query { listField: [String] }'), |
| 78 | + document: parse('{ listField }'), |
| 79 | + rootValue, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + function completeObjectList( |
| 84 | + resolve: GraphQLFieldResolver<{ index: number }, unknown>, |
| 85 | + ): PromiseOrValue<ExecutionResult> { |
| 86 | + const schema = new GraphQLSchema({ |
| 87 | + query: new GraphQLObjectType({ |
| 88 | + name: 'Query', |
| 89 | + fields: { |
| 90 | + listField: { |
| 91 | + resolve: async function* listField() { |
| 92 | + yield await Promise.resolve({ index: 0 }); |
| 93 | + yield await Promise.resolve({ index: 1 }); |
| 94 | + yield await Promise.resolve({ index: 2 }); |
| 95 | + }, |
| 96 | + type: new GraphQLList( |
| 97 | + new GraphQLObjectType({ |
| 98 | + name: 'ObjectWrapper', |
| 99 | + fields: { |
| 100 | + index: { |
| 101 | + type: GraphQLString, |
| 102 | + resolve, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }), |
| 106 | + ), |
| 107 | + }, |
| 108 | + }, |
| 109 | + }), |
| 110 | + }); |
| 111 | + return execute({ |
| 112 | + schema, |
| 113 | + document: parse('{ listField { index } }'), |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + it('Accepts an AsyncGenerator function as a List value', async () => { |
| 118 | + async function* listField() { |
| 119 | + yield await Promise.resolve('two'); |
| 120 | + yield await Promise.resolve(4); |
| 121 | + yield await Promise.resolve(false); |
| 122 | + } |
| 123 | + |
| 124 | + expect(await complete({ listField })).to.deep.equal({ |
| 125 | + data: { listField: ['two', '4', 'false'] }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('Handles an AsyncGenerator function that throws', async () => { |
| 130 | + async function* listField() { |
| 131 | + yield await Promise.resolve('two'); |
| 132 | + yield await Promise.resolve(4); |
| 133 | + throw new Error('bad'); |
| 134 | + } |
| 135 | + |
| 136 | + expect(await complete({ listField })).to.deep.equal({ |
| 137 | + data: { listField: ['two', '4', null] }, |
| 138 | + errors: [ |
| 139 | + { |
| 140 | + message: 'bad', |
| 141 | + locations: [{ line: 1, column: 3 }], |
| 142 | + path: ['listField', 2], |
| 143 | + }, |
| 144 | + ], |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('Handles an AsyncGenerator function where an intermediate value triggers an error', async () => { |
| 149 | + async function* listField() { |
| 150 | + yield await Promise.resolve('two'); |
| 151 | + yield await Promise.resolve({}); |
| 152 | + yield await Promise.resolve(4); |
| 153 | + } |
| 154 | + |
| 155 | + expect(await complete({ listField })).to.deep.equal({ |
| 156 | + data: { listField: ['two', null, '4'] }, |
| 157 | + errors: [ |
| 158 | + { |
| 159 | + message: 'String cannot represent value: {}', |
| 160 | + locations: [{ line: 1, column: 3 }], |
| 161 | + path: ['listField', 1], |
| 162 | + }, |
| 163 | + ], |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('Handles errors from `completeValue` in AsyncIterables', async () => { |
| 168 | + async function* listField() { |
| 169 | + yield await Promise.resolve('two'); |
| 170 | + yield await Promise.resolve({}); |
| 171 | + } |
| 172 | + |
| 173 | + expect(await complete({ listField })).to.deep.equal({ |
| 174 | + data: { listField: ['two', null] }, |
| 175 | + errors: [ |
| 176 | + { |
| 177 | + message: 'String cannot represent value: {}', |
| 178 | + locations: [{ line: 1, column: 3 }], |
| 179 | + path: ['listField', 1], |
| 180 | + }, |
| 181 | + ], |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('Handles promises from `completeValue` in AsyncIterables', async () => { |
| 186 | + expect( |
| 187 | + await completeObjectList(({ index }) => Promise.resolve(index)), |
| 188 | + ).to.deep.equal({ |
| 189 | + data: { listField: [{ index: '0' }, { index: '1' }, { index: '2' }] }, |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('Handles rejected promises from `completeValue` in AsyncIterables', async () => { |
| 194 | + expect( |
| 195 | + await completeObjectList(({ index }) => { |
| 196 | + if (index === 2) { |
| 197 | + return Promise.reject(new Error('bad')); |
| 198 | + } |
| 199 | + return Promise.resolve(index); |
| 200 | + }), |
| 201 | + ).to.deep.equal({ |
| 202 | + data: { listField: [{ index: '0' }, { index: '1' }, { index: null }] }, |
| 203 | + errors: [ |
| 204 | + { |
| 205 | + message: 'bad', |
| 206 | + locations: [{ line: 1, column: 15 }], |
| 207 | + path: ['listField', 2, 'index'], |
| 208 | + }, |
| 209 | + ], |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
| 213 | + |
67 | 214 | describe('Execute: Handles list nullability', () => {
|
68 | 215 | async function complete(args: { listField: unknown; as: string }) {
|
69 | 216 | const { listField, as } = args;
|
|
0 commit comments