Skip to content

Commit

Permalink
Rename directiveResolvers and some subclasses in docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 14, 2018
1 parent 6776473 commit d3bdc29
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions docs/source/schema-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,30 @@ type Query {

const schema = makeExecutableSchema({ typeDefs });

SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
rest: class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { url } = this.args;
field.resolve = () => fetch(url);
}
class RestDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { url } = this.args;
field.resolve = () => fetch(url);
}
}

SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
rest: RestDirective
});
```

The subclass in this example is defined as an anonymous `class` expression, for brevity. A truly reusable `SchemaDirectiveVisitor` would most likely be defined in a library using a named class declaration, and then exported for consumption by other modules and packages.

It's also possible to pass directive implementations to `makeExecutableSchema` via the `directiveVisitors` parameter, if you prefer:
For convenience, you can also pass directive implementations to `makeExecutableSchema` via the `directives` parameter:

```typescript
const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
rest: class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { url } = this.args;
field.resolve = () => fetch(url);
}
}
directives: {
rest: RestDirective
}
});
```

Note that a subclass of `SchemaDirectiveVisitor` may be instantiated multiple times to visit multiple different `@directive` occurrences, or even `@directive`s of different names. In other words, `SchemaDirectiveVisitor` implementations are effectively anonymous, and it's up to the caller of `SchemaDirectiveVisitor.visitSchemaDirectives` to assign names to them.
Note that a subclass of `SchemaDirectiveVisitor` may be instantiated multiple times to visit multiple different `@directive` occurrences, or even `@directive`s of different names. In other words, `SchemaDirectiveVisitor` implementations are effectively anonymous, so it's up to whoever uses them to assign names to them.

## Declaring schema directives

Expand Down Expand Up @@ -122,7 +117,7 @@ import {
GraphQLEnumType,
} from "graphql";

class AuthDirectiveVisitor extends SchemaDirectiveVisitor {
class AuthDirective extends SchemaDirectiveVisitor {
public visitObject(object: GraphQLObjectType) {...}
public visitFieldDefinition(field: GraphQLField<any, any>) {...}

Expand Down Expand Up @@ -198,7 +193,7 @@ type Query {
hello: String @upper
}`;

class UpperCaseVisitor extends SchemaDirectiveVisitor {
class UpperCaseDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
Expand All @@ -213,8 +208,8 @@ class UpperCaseVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
upper: UpperCaseVisitor
directives: {
upper: UpperCaseDirective
}
});
```
Expand All @@ -235,7 +230,7 @@ type Post {
published: Date @date(format: "mmmm d, yyyy")
}`;

class DateFormatVisitor extends SchemaDirectiveVisitor {
class DateFormatDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
const { format } = this.args;
Expand All @@ -249,8 +244,8 @@ class DateFormatVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
date: DateFormatVisitor
directives: {
date: DateFormatDirective
}
});
```
Expand All @@ -267,7 +262,7 @@ type Query {
greeting: String @intl
}`;

class IntlVisitor extends SchemaDirectiveVisitor {
class IntlDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field, details) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
Expand All @@ -282,8 +277,8 @@ class IntlVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
intl: IntlVisitor
directives: {
intl: IntlDirective
}
});
```
Expand Down Expand Up @@ -316,7 +311,7 @@ type User @auth(requires: USER) {
const authReqSymbol = Symbol.for("@auth required role");
const authWrapSymbol = Symbol.for("@auth wrapped");

class AuthVisitor extends SchemaDirectiveVisitor {
class AuthDirective extends SchemaDirectiveVisitor {
visitObject(type) {
this.ensureFieldsWrapped(type);
type[authReqSymbol] = this.args.requires;
Expand Down Expand Up @@ -359,8 +354,8 @@ class AuthVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
auth: AuthVisitor
directives: {
auth: AuthDirective
}
});
```
Expand Down Expand Up @@ -415,7 +410,7 @@ class LimitedLengthType extends GraphQLScalarType {
}
}

class LengthVisitor extends SchemaDirectiveVisitor {
class LengthDirective extends SchemaDirectiveVisitor {
visitInputFieldDefinition(field) {
this.wrapType(field);
}
Expand All @@ -435,8 +430,9 @@ class LengthVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
length: LengthVisitor
directives: {
length: LengthDirective
}
});
```

Expand All @@ -456,7 +452,7 @@ type Location @uniqueID(name: "uid", from: ["locationID"]) {
address: String
}`;

class UniqueIDVisitor extends SchemaDirectiveVisitor {
class UniqueIdDirective extends SchemaDirectiveVisitor {
visitObject(type) {
const { name, from } = this.args;
type.getFields()[name] = {
Expand All @@ -478,8 +474,8 @@ class UniqueIDVisitor extends SchemaDirectiveVisitor {

const schema = makeExecutableSchema({
typeDefs,
directiveVisitors: {
uniqueID: UniqueIDVisitor
directives: {
uniqueID: UniqueIdDirective
}
});
```
Expand All @@ -495,10 +491,10 @@ function attachDirectiveResolvers(
schema: GraphQLSchema,
directiveResolvers: IDirectiveResolvers<any, any>,
) {
const directiveVisitors = Object.create(null);
const directives = Object.create(null);

Object.keys(directiveResolvers).forEach(directiveName => {
directiveVisitors[directiveName] = class extends SchemaDirectiveVisitor {
directives[directiveName] = class extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const resolver = directiveResolvers[directiveName];
const originalResolver = field.resolve || defaultFieldResolver;
Expand All @@ -519,7 +515,7 @@ function attachDirectiveResolvers(

SchemaDirectiveVisitor.visitSchemaDirectives(
schema,
directiveVisitors,
directives,
);
}
```
Expand Down

0 comments on commit d3bdc29

Please sign in to comment.