Skip to content

Commit

Permalink
Add documentation of @Date that adds a field argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 15, 2018
1 parent d5577e4 commit ad76736
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
64 changes: 64 additions & 0 deletions docs/source/schema-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,70 @@ const schema = makeExecutableSchema({
});
```

Of course, it would be even better if the schema author did not have decide on a specific `Date` format, but could instead leave that decision to the client. To make this work, the directive just needs to add an additional argument to the field:

```js
import formatDate from "dateformat";
import {
defaultFieldResolver,
graphql,
GraphQLString,
} from "graphql";

const typeDefs = `
directive @date(
defaultFormat: String = "mmmm d, yyyy"
) on FIELD_DEFINITION
scalar Date
type Query {
today: Date @date
}`;

class FormattableDateDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
const { defaultFormat } = this.args;

field.args.push({
name: 'format',
type: GraphQLString
});

field.resolve = async function (
source,
{ format, ...otherArgs },
context,
info,
) {
// If a format argument was not provided, default to the optional
// defaultFormat argument taken by the @date directive.
format = format || defaultFormat;
const date = await resolve.call(this, source, otherArgs, context, info);
return formatDate(date, format);
};
}
}

const schema = makeExecutableSchema({
typeDefs,
schemaDirectives: {
date: FormattableDateDirective
}
});
```

Now the client can specify a desired `format` argument when requesting the `today` field:

```js
graphql(schema, `query {
today(format: "d mmm yyyy")
}`).then(result => {
console.log(result.data.today);
});
```

### Marking strings for internationalization

Suppose you have a function called `translate` that takes a string, a path identifying that string's role in your application, and a target locale for the translation.
Expand Down
37 changes: 26 additions & 11 deletions src/test/testDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,19 +618,20 @@ describe('@directives', () => {
});
});

it('can be used to implement the @formattableDate example', async () => {
it('can be used to implement the @date by adding an argument', async () => {
class FormattableDateDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
const { defaultFormat } = this.args;

field.args.push({
name: 'format',
type: GraphQLString,
defaultValue: 'mmmm d, yyyy',
type: GraphQLString
});

field.type = GraphQLString;
field.resolve = async function (source, { format, ...args }, context, info) {
format = format || defaultFormat;
const date = await resolve.call(this, source, args, context, info);
return formatDate(date, format);
};
Expand All @@ -639,16 +640,18 @@ describe('@directives', () => {

const schema = makeExecutableSchema({
typeDefs: `
directive @formattableDate on FIELD_DEFINITION
directive @date(
defaultFormat: String = "mmmm d, yyyy"
) on FIELD_DEFINITION
scalar Date
type Query {
today: Date @formattableDate
today: Date @date
}`,

schemaDirectives: {
formattableDate: FormattableDateDirective
date: FormattableDateDirective
},

resolvers: {
Expand All @@ -660,16 +663,28 @@ describe('@directives', () => {
}
});

const resultNoArg = await graphql(schema, `query { today }`);

if (resultNoArg.errors) {
assert.deepEqual(resultNoArg.errors, []);
}

assert.deepEqual(
(await graphql(schema, `query { today }`)).data,
resultNoArg.data,
{ today: 'March 15, 2018' }
);

const resultWithArg = await graphql(schema, `
query {
today(format: "dd mmm yyyy")
}`);

if (resultWithArg.errors) {
assert.deepEqual(resultWithArg.errors, []);
}

assert.deepEqual(
(await graphql(schema, `
query {
today(format: "dd mmm yyyy")
}`)).data,
resultWithArg.data,
{ today: '15 Mar 2018' }
);
});
Expand Down

0 comments on commit ad76736

Please sign in to comment.