Skip to content

Commit

Permalink
Expose this.visitedType on SchemaDirectiveVisitor instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 14, 2018
1 parent 7c2dc88 commit f951c81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export class SchemaDirectiveVisitor {
// object, in case a vistor method needs to refer to this.schema.
public schema: GraphQLSchema;

// A reference to the type object that this visitor was created to visit.
public visitedType: VisitableType;

// Call SchemaDirectiveVisitor.visitSchema(schema, directiveVisitors) to
// visit every @directive in the schema and instantiate an appropriate
// SchemaDirectiveVisitor subclass to visit/handle/transform the object
Expand Down Expand Up @@ -287,9 +290,12 @@ export class SchemaDirectiveVisitor {
// created and assigned names. Subclasses can override the constructor
// method, but since the constructor is marked as protected, these are
// the only arguments that will ever be passed.
directiveInstances.push(
new directiveClass({ name, args, schema })
);
directiveInstances.push(new directiveClass({
name,
args,
visitedType: type,
schema
}));
});

return directiveInstances;
Expand All @@ -306,10 +312,12 @@ export class SchemaDirectiveVisitor {
protected constructor(config: {
name: string,
args: { [name: string]: any },
visitedType: VisitableType,
schema: GraphQLSchema,
}) {
this.name = config.name;
this.args = config.args;
this.visitedType = config.visitedType;
this.schema = config.schema;
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/testDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ describe('@directives', () => {
mutationTypeDirective: class extends SchemaDirectiveVisitor {
public visitObject(object: GraphQLObjectType) {
mutationObjectType = object;
assert.strictEqual(this.visitedType, object);
assert.strictEqual(object.name, 'Mutation');
}
},
Expand All @@ -196,6 +197,7 @@ describe('@directives', () => {
public visitFieldDefinition(field: GraphQLField<any, any>, details: {
objectType: GraphQLObjectType,
}) {
assert.strictEqual(this.visitedType, field);
assert.strictEqual(field.name, 'addPerson');
assert.strictEqual(details.objectType, mutationObjectType);
assert.strictEqual(field.args.length, 1);
Expand All @@ -208,6 +210,7 @@ describe('@directives', () => {
field: GraphQLField<any, any>,
objectType: GraphQLObjectType,
}) {
assert.strictEqual(this.visitedType, arg);
assert.strictEqual(arg.name, 'input');
assert.strictEqual(details.field, mutationField);
assert.strictEqual(details.objectType, mutationObjectType);
Expand All @@ -217,6 +220,7 @@ describe('@directives', () => {

enumTypeDirective: class extends SchemaDirectiveVisitor {
public visitEnum(enumType: GraphQLEnumType) {
assert.strictEqual(this.visitedType, enumType);
assert.strictEqual(enumType.name, 'Gender');
enumObjectType = enumType;
}
Expand All @@ -226,6 +230,7 @@ describe('@directives', () => {
public visitEnumValue(value: GraphQLEnumValue, details: {
enumType: GraphQLEnumType,
}) {
assert.strictEqual(this.visitedType, value);
assert.strictEqual(value.name, 'NONBINARY');
assert.strictEqual(value.value, 'NONBINARY');
assert.strictEqual(details.enumType, enumObjectType);
Expand All @@ -235,6 +240,7 @@ describe('@directives', () => {
inputTypeDirective: class extends SchemaDirectiveVisitor {
public visitInputObject(object: GraphQLInputObjectType) {
inputObjectType = object;
assert.strictEqual(this.visitedType, object);
assert.strictEqual(object.name, 'PersonInput');
}
},
Expand All @@ -243,6 +249,7 @@ describe('@directives', () => {
public visitInputFieldDefinition(field: GraphQLInputField, details: {
objectType: GraphQLInputObjectType,
}) {
assert.strictEqual(this.visitedType, field);
assert.strictEqual(field.name, 'name');
assert.strictEqual(details.objectType, inputObjectType);
}
Expand Down

0 comments on commit f951c81

Please sign in to comment.