From 86666cd9f2be29d80e3116c3f35d385630194ab3 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 14 Mar 2018 18:56:06 -0400 Subject: [PATCH] Rename makeExecutableSchema({ directives }) option to schemaDirectives. --- docs/source/schema-directives.md | 30 +++++++++++++++--------------- src/Interfaces.ts | 2 +- src/schemaGenerator.ts | 12 ++++++------ src/test/testDirectives.ts | 22 +++++++++++----------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/docs/source/schema-directives.md b/docs/source/schema-directives.md index c4d06899a9d..d9d42c511cf 100644 --- a/docs/source/schema-directives.md +++ b/docs/source/schema-directives.md @@ -28,7 +28,7 @@ This document focuses on directives that appear in GraphQL _schemas_ (as opposed Most of this document is concerned with _implementing_ schema directives, and some of the examples may seem quite complicated. No matter how many tools and best practices you have at your disposal, it can be difficult to implement a non-trivial schema directive in a reliable, reusable way. Exhaustive testing is essential, and using a typed language like TypeScript is recommended, because there are so many different schema types to worry about. -However, the API we provide for _using_ a schema directive is extremely simple. Just import the implementation of the directive, then pass it to `makeExecutableSchema` via the `directives` argument, which is an object that maps directive names to directive implementations: +However, the API we provide for _using_ a schema directive is extremely simple. Just import the implementation of the directive, then pass it to `makeExecutableSchema` via the `schemaDirectives` argument, which is an object that maps directive names to directive implementations: ```js import { makeExecutableSchema } from "graphql-tools"; @@ -42,7 +42,7 @@ type Person @rename(to: "Human") { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { rename: RenameDirective } }); @@ -94,7 +94,7 @@ class DeprecatedDirective extends SchemaDirectiveVisitor { } ``` -In order to apply this implementation to a schema that contains `@deprecated` directives, simply pass the `DeprecatedDirective` class to the `makeExecutableSchema` function via the `directives` option: +In order to apply this implementation to a schema that contains `@deprecated` directives, simply pass the `DeprecatedDirective` class to the `makeExecutableSchema` function via the `schemaDirectives` option: ```typescript import { makeExecutableSchema } from "graphql-tools"; @@ -107,7 +107,7 @@ type ExampleType { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { deprecated: DeprecatedDirective } }); @@ -123,7 +123,7 @@ SchemaDirectiveVisitor.visitSchemaDirectives(schema, { Note that a subclass of `SchemaDirectiveVisitor` may be instantiated multiple times to visit multiple different occurrences of the `@deprecated` directive. That's why you provide a class rather than an instance of that class. -If for some reason you have a schema that uses another name for the `@deprecated` directive, but you want to use the same implementation, you can! The same `DeprecatedDirective` class can be passed with a different name, simply by changing its key in the `directives` object passed to `makeExecutableSchema`. In other words, `SchemaDirectiveVisitor` implementations are effectively anonymous, so it's up to whoever uses them to assign names to them. +If for some reason you have a schema that uses another name for the `@deprecated` directive, but you want to use the same implementation, you can! The same `DeprecatedDirective` class can be passed with a different name, simply by changing its key in the `schemaDirectives` object passed to `makeExecutableSchema`. In other words, `SchemaDirectiveVisitor` implementations are effectively anonymous, so it's up to whoever uses them to assign names to them. ## Examples @@ -158,7 +158,7 @@ class UpperCaseDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { upper: UpperCaseDirective, upperCase: UpperCaseDirective } @@ -188,7 +188,7 @@ class RestDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { rest: RestDirective } }); @@ -225,7 +225,7 @@ class DateFormatDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { date: DateFormatDirective } }); @@ -260,7 +260,7 @@ class IntlDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { intl: IntlDirective } }); @@ -342,7 +342,7 @@ class AuthDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { auth: AuthDirective, authorized: AuthDirective, authenticated: AuthDirective @@ -428,7 +428,7 @@ class LimitedLengthType extends GraphQLScalarType { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { length: LengthDirective } }); @@ -488,7 +488,7 @@ class UniqueIdDirective extends SchemaDirectiveVisitor { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { uniqueID: UniqueIdDirective } }); @@ -597,10 +597,10 @@ function attachDirectiveResolvers( schema: GraphQLSchema, directiveResolvers: IDirectiveResolvers, ) { - const directives = Object.create(null); + const schemaDirectives = Object.create(null); Object.keys(directiveResolvers).forEach(directiveName => { - directives[directiveName] = class extends SchemaDirectiveVisitor { + schemaDirectives[directiveName] = class extends SchemaDirectiveVisitor { public visitFieldDefinition(field: GraphQLField) { const resolver = directiveResolvers[directiveName]; const originalResolver = field.resolve || defaultFieldResolver; @@ -621,7 +621,7 @@ function attachDirectiveResolvers( SchemaDirectiveVisitor.visitSchemaDirectives( schema, - directives, + schemaDirectives, ); } ``` diff --git a/src/Interfaces.ts b/src/Interfaces.ts index 6ab860ae069..bad0ead0b42 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -82,7 +82,7 @@ export interface IExecutableSchemaDefinition { allowUndefinedInResolve?: boolean; resolverValidationOptions?: IResolverValidationOptions; directiveResolvers?: IDirectiveResolvers; - directives?: { [name: string]: typeof SchemaDirectiveVisitor }; + schemaDirectives?: { [name: string]: typeof SchemaDirectiveVisitor }; parseOptions?: GraphQLParseOptions; } diff --git a/src/schemaGenerator.ts b/src/schemaGenerator.ts index 2a6dfe4d466..bd61c782426 100644 --- a/src/schemaGenerator.ts +++ b/src/schemaGenerator.ts @@ -114,7 +114,7 @@ function makeExecutableSchema({ allowUndefinedInResolve = true, resolverValidationOptions = {}, directiveResolvers = null, - directives = null, + schemaDirectives = null, parseOptions = {}, }: IExecutableSchemaDefinition) { const jsSchema = _generateSchema( @@ -142,10 +142,10 @@ function makeExecutableSchema({ attachDirectiveResolvers(jsSchema, directiveResolvers); } - if (directives) { + if (schemaDirectives) { SchemaDirectiveVisitor.visitSchemaDirectives( jsSchema, - directives, + schemaDirectives, ); } @@ -692,10 +692,10 @@ function attachDirectiveResolvers( ); } - const directives = Object.create(null); + const schemaDirectives = Object.create(null); Object.keys(directiveResolvers).forEach(directiveName => { - directives[directiveName] = class extends SchemaDirectiveVisitor { + schemaDirectives[directiveName] = class extends SchemaDirectiveVisitor { public visitFieldDefinition(field: GraphQLField) { const resolver = directiveResolvers[directiveName]; const originalResolver = field.resolve || defaultFieldResolver; @@ -716,7 +716,7 @@ function attachDirectiveResolvers( SchemaDirectiveVisitor.visitSchemaDirectives( schema, - directives, + schemaDirectives, ); } diff --git a/src/test/testDirectives.ts b/src/test/testDirectives.ts index 5cfa9ca8870..a40c31ca87d 100644 --- a/src/test/testDirectives.ts +++ b/src/test/testDirectives.ts @@ -398,7 +398,7 @@ describe('@directives', () => { let argumentCount = 0; let fieldCount = 0; - const directives = { + const schemaDirectives = { directive: class extends SchemaDirectiveVisitor { public visitEnumValue(value: GraphQLEnumValue) { ++enumValueCount; @@ -444,7 +444,7 @@ describe('@directives', () => { makeExecutableSchema({ typeDefs: schemaText, - directives, + schemaDirectives, }); assert.strictEqual(enumValueCount, 2); @@ -537,7 +537,7 @@ describe('@directives', () => { type Query { hello: String @upper }`, - directives: { + schemaDirectives: { upper: class extends SchemaDirectiveVisitor { public visitFieldDefinition(field: GraphQLField) { const { resolve = defaultFieldResolver } = field; @@ -582,7 +582,7 @@ describe('@directives', () => { today: Date @date(format: "mmmm d, yyyy") }`, - directives: { + schemaDirectives: { date: class extends SchemaDirectiveVisitor { public visitFieldDefinition(field: GraphQLField) { const { resolve = defaultFieldResolver } = field; @@ -640,7 +640,7 @@ describe('@directives', () => { greeting: String @intl }`, - directives: { + schemaDirectives: { intl: class extends SchemaDirectiveVisitor { public visitFieldDefinition(field: GraphQLField, details: { objectType: GraphQLObjectType, @@ -769,7 +769,7 @@ describe('@directives', () => { users: [User] }`, - directives: { + schemaDirectives: { auth: AuthDirective }, @@ -878,7 +878,7 @@ describe('@directives', () => { title: String! @length(max: 10) }`, - directives: { + schemaDirectives: { length: class extends SchemaDirectiveVisitor { public visitInputFieldDefinition(field: GraphQLInputField) { this.wrapType(field); @@ -968,7 +968,7 @@ describe('@directives', () => { address: String }`, - directives: { + schemaDirectives: { uniqueID: class extends SchemaDirectiveVisitor { public visitObject(type: GraphQLObjectType) { const { name, from } = this.args; @@ -1043,7 +1043,7 @@ describe('@directives', () => { const schema = makeExecutableSchema({ typeDefs, - directives: { + schemaDirectives: { objectTypeDirective: class extends SchemaDirectiveVisitor { public visitObject(object: GraphQLObjectType) { return HumanType = Object.create(object, { @@ -1095,7 +1095,7 @@ describe('@directives', () => { PERSON_YEARS @remove(if: false) }`, - directives: { + schemaDirectives: { remove: class extends SchemaDirectiveVisitor { public visitEnumValue(value: GraphQLEnumValue): null { if (this.args.if) { @@ -1130,7 +1130,7 @@ describe('@directives', () => { born: Date }`, - directives: { + schemaDirectives: { rename: class extends SchemaDirectiveVisitor { public visitObject(object: GraphQLObjectType) { object.name = this.args.to;