Skip to content

Commit

Permalink
add additional specific test for transformation of new null bubbling …
Browse files Browse the repository at this point in the history
…from latest format
  • Loading branch information
yaacovCR committed Jan 14, 2025
1 parent 2b4dac9 commit 462afcf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
90 changes: 89 additions & 1 deletion src/transform/__tests__/legacyExecuteIncrementally-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON.js';

import { invariant } from '../../jsutils/invariant.js';
import { isPromise } from '../../jsutils/isPromise.js';
import type { ObjMap } from '../../jsutils/ObjMap.js';

import type { DocumentNode } from '../../language/ast.js';
import { Kind } from '../../language/kinds.js';
import { parse } from '../../language/parser.js';

Expand All @@ -10,14 +15,52 @@ import { GraphQLString } from '../../type/scalars.js';
import { GraphQLSchema } from '../../type/schema.js';

import { legacyExecuteIncrementally } from '../legacyExecuteIncrementally.js';
import type {
LegacyInitialIncrementalExecutionResult,
LegacySubsequentIncrementalExecutionResult,
} from '../transformResult.js';

const someObjectType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
someField: { type: GraphQLString },
anotherField: { type: GraphQLString },
nonNullableField: { type: new GraphQLNonNull(GraphQLString) },
},
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: { someField: { type: new GraphQLNonNull(GraphQLString) } },
fields: {
someField: { type: new GraphQLNonNull(GraphQLString) },
someObjectField: { type: someObjectType },
},
}),
});

async function complete(document: DocumentNode, rootValue: ObjMap<unknown>) {
const result = legacyExecuteIncrementally({
schema,
document,
rootValue,
});

invariant(!isPromise(result));

if ('initialResult' in result) {
const results: Array<
| LegacyInitialIncrementalExecutionResult
| LegacySubsequentIncrementalExecutionResult
> = [result.initialResult];
for await (const patch of result.subsequentResults) {
results.push(patch);
}
return results;
}
return result;
}

describe('legacyExecuteIncrementally', () => {
it('handles invalid document', () => {
const result = legacyExecuteIncrementally({
Expand Down Expand Up @@ -52,4 +95,49 @@ describe('legacyExecuteIncrementally', () => {
],
});
});

it('handles null-bubbling from latest format', async () => {
const document = parse(`
query {
someObjectField {
... @defer { someField anotherField }
... @defer { someField nonNullableField }
}
}
`);
const result = await complete(document, {
someObjectField: {
someField: 'someField',
anotherField: 'anotherField',
nonNullableField: null,
},
});
expectJSON(result).toDeepEqual([
{
data: { someObjectField: {} },
hasNext: true,
},
{
incremental: [
{
data: { someField: 'someField', anotherField: 'anotherField' },
path: ['someObjectField'],
},
{
data: null,
errors: [
{
message:
'Cannot return null for non-nullable field SomeObject.nonNullableField.',
locations: [{ line: 5, column: 11 }],
path: ['someObjectField', 'nonNullableField'],
},
],
path: ['someObjectField'],
},
],
hasNext: false,
},
]);
});
});
4 changes: 2 additions & 2 deletions src/transform/completeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export function completeSubValue(
}

invariant(isObjectLike(result));
return completeObjectType(context, errors, fieldDetailsList, result, path);
return completeObjectValue(context, errors, fieldDetailsList, result, path);
}

function completeObjectType(
function completeObjectValue(
context: TransformationContext,
errors: Array<GraphQLError>,
fieldDetailsList: ReadonlyArray<FieldDetails>,
Expand Down

0 comments on commit 462afcf

Please sign in to comment.