Skip to content

Commit 2d14275

Browse files
authored
add directive test (#9)
* add directive test * add failing test
1 parent 2d354bc commit 2d14275

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/execution/__tests__/variables-test.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ import {
2222
GraphQLObjectType,
2323
GraphQLScalarType,
2424
} from '../../type/definition.js';
25-
import { GraphQLString } from '../../type/scalars.js';
25+
import { GraphQLBoolean, GraphQLString } from '../../type/scalars.js';
2626
import { GraphQLSchema } from '../../type/schema.js';
2727

2828
import { executeSync } from '../execute.js';
2929
import { getVariableValues } from '../values.js';
30+
import { GraphQLSkipDirective } from '../../type/directives.js';
31+
import { GraphQLIncludeDirective } from '../../type/directives.js';
32+
import { GraphQLDirective } from '../../type/directives.js';
33+
import { DirectiveLocation } from '../../language/directiveLocation.js';
3034

3135
const TestFaultyScalarGraphQLError = new GraphQLError(
3236
'FaultyScalarErrorMessage',
@@ -154,7 +158,30 @@ const TestType = new GraphQLObjectType({
154158
},
155159
});
156160

157-
const schema = new GraphQLSchema({ query: TestType });
161+
const schema = new GraphQLSchema({
162+
query: TestType,
163+
directives: [
164+
new GraphQLDirective({
165+
name: 'skip',
166+
description:
167+
'Directs the executor to skip this field or fragment when the `if` argument is true.',
168+
locations: [
169+
DirectiveLocation.FIELD,
170+
DirectiveLocation.FRAGMENT_SPREAD,
171+
DirectiveLocation.INLINE_FRAGMENT,
172+
],
173+
args: {
174+
if: {
175+
type: new GraphQLNonNull(GraphQLBoolean),
176+
description: 'Skipped when true.',
177+
// default values will override operation variables in the setting of defined fragment variables that are not provided
178+
defaultValue: true,
179+
},
180+
},
181+
}),
182+
GraphQLIncludeDirective,
183+
],
184+
});
158185

159186
function executeQuery(
160187
query: string,
@@ -1434,5 +1461,33 @@ describe('Execute: Handles inputs', () => {
14341461
},
14351462
});
14361463
});
1464+
1465+
it('when argument passed to a directive', () => {
1466+
const result = executeQueryWithFragmentArguments(`
1467+
query {
1468+
...a(value: true)
1469+
}
1470+
fragment a($value: Boolean!) on TestType {
1471+
fieldWithNonNullableStringInput @skip(if: $value)
1472+
}
1473+
`);
1474+
expect(result).to.deep.equal({
1475+
data: {},
1476+
});
1477+
});
1478+
1479+
it('when a nullable argument to a directive with a field default is not provided and shadowed by an operation variable', () => {
1480+
const result = executeQueryWithFragmentArguments(`
1481+
query($value: Boolean) {
1482+
...a
1483+
}
1484+
fragment a($value: Boolean) on TestType {
1485+
fieldWithNonNullableStringInput @skip(if: $value)
1486+
}
1487+
`);
1488+
expect(result).to.deep.equal({
1489+
data: {},
1490+
});
1491+
});
14371492
});
14381493
});

0 commit comments

Comments
 (0)