Skip to content

Commit

Permalink
Add a test of undeclared @directive argument values.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 14, 2018
1 parent 0128a10 commit 72d1cd5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ function valueFromASTUntyped(
obj[field.name.value] = valueFromASTUntyped(field.value);
});
return obj;
/* istanbul ignore next */
default:
throw new Error('Unexpected value kind: ' + valueNode.kind);
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/testDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,82 @@ describe('@directives', () => {
}
});
});

it('can handle all kinds of undeclared arguments', () => {
const schemaText = `
enum SpineEnum {
VERTEBRATE @directive(spineless: false)
INVERTEBRATE @directive(spineless: true)
}
type Query @directive(c: null, d: 1, e: { oyez: 3.1415926 }) {
animal(
name: String @directive(f: ["n", "a", "m", "e"])
): Animal @directive(g: INVERTEBRATE)
}
type Animal {
name: String @directive(default: "horse")
spine: SpineEnum @directive(default: VERTEBRATE)
}
`;

const schema = makeExecutableSchema({
typeDefs: schemaText,
});

let enumValueCount = 0;
let objectCount = 0;
let argumentCount = 0;
let fieldCount = 0;

SchemaDirectiveVisitor.visitSchema(schema, {
directive: class extends SchemaDirectiveVisitor {
public visitEnumValue(value: GraphQLEnumValue) {
++enumValueCount;
assert.strictEqual(
this.args.spineless,
value.name === 'INVERTEBRATE'
);
}

public visitObject(object: GraphQLObjectType) {
++objectCount;
assert.strictEqual(this.args.c, null);
assert.strictEqual(this.args.d, 1);
assert.strictEqual(Math.round(this.args.e.oyez), 3);
}

public visitArgumentDefinition(arg: GraphQLArgument) {
++argumentCount;
assert.strictEqual(this.args.f.join(''), 'name');
}

public visitFieldDefinition(field: GraphQLField<any, any>, details: {
objectType: GraphQLObjectType,
}) {
++fieldCount;
switch (details.objectType.name) {
case 'Query':
assert.strictEqual(this.args.g, 'INVERTEBRATE');
break;
case 'Animal':
if (field.name === 'name') {
assert.strictEqual(this.args.default, 'horse');
} else if (field.name === 'spine') {
assert.strictEqual(this.args.default, 'VERTEBRATE');
}
break;
default:
throw new Error('unexpected field parent object type');
}
}
}
});

assert.strictEqual(enumValueCount, 2);
assert.strictEqual(objectCount, 1);
assert.strictEqual(argumentCount, 1);
assert.strictEqual(fieldCount, 3);
});
});

0 comments on commit 72d1cd5

Please sign in to comment.