Skip to content

use semantic nullable wrapper only #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: SemanticNullabilityDocumentDirective
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 76 additions & 116 deletions src/execution/__tests__/semantic-nullability-test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { GraphQLError } from '../../error/GraphQLError';
import { expectJSON } from '../../__testUtils__/expectJSON';

import type { ObjMap } from '../../jsutils/ObjMap';

import type { ExecutableDefinitionNode, FieldNode } from '../../language/ast';
import { parse } from '../../language/parser';

import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLSemanticNonNull,
GraphQLSemanticNullable,
} from '../../type/definition';
import { GraphQLString } from '../../type/scalars';
Expand All @@ -29,163 +28,130 @@ describe('Execute: Handles Semantic Nullability', () => {
name: 'DataType',
fields: () => ({
a: { type: new GraphQLSemanticNullable(GraphQLString) },
b: { type: new GraphQLSemanticNonNull(GraphQLString) },
b: { type: GraphQLString },
c: { type: new GraphQLNonNull(GraphQLString) },
d: { type: new GraphQLSemanticNonNull(DeepDataType) },
d: { type: DeepDataType },
}),
});

const schema = new GraphQLSchema({
useSemanticNullability: true,
query: DataType,
});

function executeWithSemanticNullability(
query: string,
rootValue: ObjMap<unknown>,
) {
return execute({
schema,
document: parse(query),
rootValue,
});
}

it('SemanticNonNull throws error on null without error', async () => {
const data = {
a: () => 'Apple',
b: () => null,
c: () => 'Cookie',
};

const document = parse(`
query {
b
}
`);
const query = `
query {
b
}
`;

const result = await execute({
schema: new GraphQLSchema({ query: DataType }),
document,
rootValue: data,
});

const executable = document.definitions?.values().next()
.value as ExecutableDefinitionNode;
const selectionSet = executable.selectionSet.selections
.values()
.next().value;
const result = await executeWithSemanticNullability(query, data);

expect(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: {
b: null,
},
errors: [
new GraphQLError(
'Cannot return null for semantic-non-nullable field DataType.b.',
{
nodes: selectionSet,
path: ['b'],
},
),
{
message:
'Cannot return null for semantic-non-nullable field DataType.b.',
path: ['b'],
locations: [{ line: 3, column: 9 }],
},
],
});
});

it('SemanticNonNull succeeds on null with error', async () => {
const data = {
a: () => 'Apple',
b: () => {
throw new Error('Something went wrong');
},
c: () => 'Cookie',
};

const document = parse(`
query {
b
}
`);

const executable = document.definitions?.values().next()
.value as ExecutableDefinitionNode;
const selectionSet = executable.selectionSet.selections
.values()
.next().value;

const result = await execute({
schema: new GraphQLSchema({ query: DataType }),
document,
rootValue: data,
});
const query = `
query {
b
}
`;

expect(result).to.deep.equal({
const result = await executeWithSemanticNullability(query, data);

expectJSON(result).toDeepEqual({
data: {
b: null,
},
errors: [
new GraphQLError('Something went wrong', {
nodes: selectionSet,
{
message: 'Something went wrong',
path: ['b'],
}),
locations: [{ line: 3, column: 9 }],
},
],
});
});

it('SemanticNonNull halts null propagation', async () => {
const deepData = {
f: () => null,
};

const data = {
a: () => 'Apple',
b: () => null,
c: () => 'Cookie',
d: () => deepData,
d: () => ({
f: () => null,
}),
};

const document = parse(`
query {
d {
f
}
const query = `
query {
d {
f
}
`);

const result = await execute({
schema: new GraphQLSchema({ query: DataType }),
document,
rootValue: data,
});
}
`;

const executable = document.definitions?.values().next()
.value as ExecutableDefinitionNode;
const dSelectionSet = executable.selectionSet.selections.values().next()
.value as FieldNode;
const fSelectionSet = dSelectionSet.selectionSet?.selections
.values()
.next().value;
const result = await executeWithSemanticNullability(query, data);

expect(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: {
d: null,
},
errors: [
new GraphQLError(
'Cannot return null for non-nullable field DeepDataType.f.',
{
nodes: fSelectionSet,
path: ['d', 'f'],
},
),
{
message: 'Cannot return null for non-nullable field DeepDataType.f.',
path: ['d', 'f'],
locations: [{ line: 4, column: 11 }],
},
],
});
});

it('SemanticNullable allows null values', async () => {
const data = {
a: () => null,
b: () => null,
c: () => 'Cookie',
};

const document = parse(`
query {
a
}
`);
const query = `
query {
a
}
`;

const result = await execute({
schema: new GraphQLSchema({ query: DataType }),
document,
rootValue: data,
});
const result = await executeWithSemanticNullability(query, data);

expect(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: {
a: null,
},
Expand All @@ -195,23 +161,17 @@ describe('Execute: Handles Semantic Nullability', () => {
it('SemanticNullable allows non-null values', async () => {
const data = {
a: () => 'Apple',
b: () => null,
c: () => 'Cookie',
};

const document = parse(`
query {
a
}
`);
const query = `
query {
a
}
`;

const result = await execute({
schema: new GraphQLSchema({ query: DataType }),
document,
rootValue: data,
});
const result = await executeWithSemanticNullability(query, data);

expect(result).to.deep.equal({
expectJSON(result).toDeepEqual({
data: {
a: 'Apple',
},
Expand Down
Loading