Skip to content

Commit

Permalink
move semantic nullability tests to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
twof committed Nov 8, 2024
1 parent af58560 commit 668f3dc
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 199 deletions.
199 changes: 0 additions & 199 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLSemanticNonNull,
GraphQLSemanticNullable,
GraphQLUnionType,
} from '../../type/definition';
import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';

import { execute, executeSync } from '../execute';
import { GraphQLError } from '../../error';
import { ExecutableDefinitionNode, FieldNode, SelectionSetNode } from '../../language';

describe('Execute: Handles basic execution tasks', () => {
it('throws if no document is provided', () => {
Expand Down Expand Up @@ -1328,198 +1324,3 @@ describe('Execute: Handles basic execution tasks', () => {
});
});

describe('Execute: Handles Semantic Nullability', () => {
const DeepDataType = new GraphQLObjectType({
name: 'DeepDataType',
fields: {
f: { type: new GraphQLNonNull(GraphQLString) }
},
});

const DataType: GraphQLObjectType = new GraphQLObjectType({
name: 'DataType',
fields: () => ({
a: { type: new GraphQLSemanticNullable(GraphQLString) },
b: { type: new GraphQLSemanticNonNull(GraphQLString) },
c: { type: new GraphQLNonNull(GraphQLString) },
d: { type: new GraphQLSemanticNonNull(DeepDataType) }
}),
});

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

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

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

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

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

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
}
`);

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

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

expect(result).to.deep.equal({
data: {
b: null
},
errors: [
new GraphQLError(
'Something went wrong',
{
nodes: selectionSet,
path: ['b']
}
)
]
});
});

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

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


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

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

let executable = document.definitions?.values().next().value as ExecutableDefinitionNode;
let dSelectionSet = executable.selectionSet.selections.values().next().value as FieldNode;
let fSelectionSet = dSelectionSet.selectionSet?.selections.values().next().value;

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

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

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

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

expect(result).to.deep.equal({
data: {
a: null
}
});
});

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

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

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

expect(result).to.deep.equal({
data: {
a: 'Apple'
}
});
});
});
Loading

0 comments on commit 668f3dc

Please sign in to comment.