Skip to content

Commit

Permalink
Rename makeExecutableSchema({ directives }) option to schemaDirectives.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 14, 2018
1 parent 2c8c50d commit 86666cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
30 changes: 15 additions & 15 deletions docs/source/schema-directives.md
Expand Up @@ -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";
Expand All @@ -42,7 +42,7 @@ type Person @rename(to: "Human") {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
rename: RenameDirective
}
});
Expand Down Expand Up @@ -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";
Expand All @@ -107,7 +107,7 @@ type ExampleType {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
deprecated: DeprecatedDirective
}
});
Expand All @@ -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

Expand Down Expand Up @@ -158,7 +158,7 @@ class UpperCaseDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
upper: UpperCaseDirective,
upperCase: UpperCaseDirective
}
Expand Down Expand Up @@ -188,7 +188,7 @@ class RestDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
rest: RestDirective
}
});
Expand Down Expand Up @@ -225,7 +225,7 @@ class DateFormatDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
date: DateFormatDirective
}
});
Expand Down Expand Up @@ -260,7 +260,7 @@ class IntlDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
intl: IntlDirective
}
});
Expand Down Expand Up @@ -342,7 +342,7 @@ class AuthDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
auth: AuthDirective,
authorized: AuthDirective,
authenticated: AuthDirective
Expand Down Expand Up @@ -428,7 +428,7 @@ class LimitedLengthType extends GraphQLScalarType {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
length: LengthDirective
}
});
Expand Down Expand Up @@ -488,7 +488,7 @@ class UniqueIdDirective extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directives: {
schemaDirectives: {
uniqueID: UniqueIdDirective
}
});
Expand Down Expand Up @@ -597,10 +597,10 @@ function attachDirectiveResolvers(
schema: GraphQLSchema,
directiveResolvers: IDirectiveResolvers<any, any>,
) {
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<any, any>) {
const resolver = directiveResolvers[directiveName];
const originalResolver = field.resolve || defaultFieldResolver;
Expand All @@ -621,7 +621,7 @@ function attachDirectiveResolvers(

SchemaDirectiveVisitor.visitSchemaDirectives(
schema,
directives,
schemaDirectives,
);
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces.ts
Expand Up @@ -82,7 +82,7 @@ export interface IExecutableSchemaDefinition<TContext = any> {
allowUndefinedInResolve?: boolean;
resolverValidationOptions?: IResolverValidationOptions;
directiveResolvers?: IDirectiveResolvers<any, TContext>;
directives?: { [name: string]: typeof SchemaDirectiveVisitor };
schemaDirectives?: { [name: string]: typeof SchemaDirectiveVisitor };
parseOptions?: GraphQLParseOptions;
}

Expand Down
12 changes: 6 additions & 6 deletions src/schemaGenerator.ts
Expand Up @@ -114,7 +114,7 @@ function makeExecutableSchema<TContext = any>({
allowUndefinedInResolve = true,
resolverValidationOptions = {},
directiveResolvers = null,
directives = null,
schemaDirectives = null,
parseOptions = {},
}: IExecutableSchemaDefinition<TContext>) {
const jsSchema = _generateSchema(
Expand Down Expand Up @@ -142,10 +142,10 @@ function makeExecutableSchema<TContext = any>({
attachDirectiveResolvers(jsSchema, directiveResolvers);
}

if (directives) {
if (schemaDirectives) {
SchemaDirectiveVisitor.visitSchemaDirectives(
jsSchema,
directives,
schemaDirectives,
);
}

Expand Down Expand Up @@ -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<any, any>) {
const resolver = directiveResolvers[directiveName];
const originalResolver = field.resolve || defaultFieldResolver;
Expand All @@ -716,7 +716,7 @@ function attachDirectiveResolvers(

SchemaDirectiveVisitor.visitSchemaDirectives(
schema,
directives,
schemaDirectives,
);
}

Expand Down
22 changes: 11 additions & 11 deletions src/test/testDirectives.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('@directives', () => {

makeExecutableSchema({
typeDefs: schemaText,
directives,
schemaDirectives,
});

assert.strictEqual(enumValueCount, 2);
Expand Down Expand Up @@ -537,7 +537,7 @@ describe('@directives', () => {
type Query {
hello: String @upper
}`,
directives: {
schemaDirectives: {
upper: class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
Expand Down Expand Up @@ -582,7 +582,7 @@ describe('@directives', () => {
today: Date @date(format: "mmmm d, yyyy")
}`,

directives: {
schemaDirectives: {
date: class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('@directives', () => {
greeting: String @intl
}`,

directives: {
schemaDirectives: {
intl: class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>, details: {
objectType: GraphQLObjectType,
Expand Down Expand Up @@ -769,7 +769,7 @@ describe('@directives', () => {
users: [User]
}`,

directives: {
schemaDirectives: {
auth: AuthDirective
},

Expand Down Expand Up @@ -878,7 +878,7 @@ describe('@directives', () => {
title: String! @length(max: 10)
}`,

directives: {
schemaDirectives: {
length: class extends SchemaDirectiveVisitor {
public visitInputFieldDefinition(field: GraphQLInputField) {
this.wrapType(field);
Expand Down Expand Up @@ -968,7 +968,7 @@ describe('@directives', () => {
address: String
}`,

directives: {
schemaDirectives: {
uniqueID: class extends SchemaDirectiveVisitor {
public visitObject(type: GraphQLObjectType) {
const { name, from } = this.args;
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1130,7 +1130,7 @@ describe('@directives', () => {
born: Date
}`,

directives: {
schemaDirectives: {
rename: class extends SchemaDirectiveVisitor {
public visitObject(object: GraphQLObjectType) {
object.name = this.args.to;
Expand Down

0 comments on commit 86666cd

Please sign in to comment.