-
-
Notifications
You must be signed in to change notification settings - Fork 821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstraction for reusable schema @directive implementations. #640
Merged
Merged
Changes from all commits
Commits
Show all changes
80 commits
Select commit
Hold shift + click to select a range
6344500
Simplify directiveResolvers wrapping with an async function.
benjamn a805fdd
Initial implementation of @directive visitor logic.
benjamn 38818fa
Add a test that @directives are preserved in the schema AST.
benjamn 25a5ab0
Stick to object union types to fix tests with GraphQL v0.11 (old).
benjamn 5b41872
Add a basic test of GraphQLSchemaDirective.visitSchema.
benjamn eb7dc73
Prevent re-visiting object types by not visiting union members.
benjamn 892eb73
Expose this.schema to GraphQLSchemaDirective visitor methods.
benjamn 0449de5
Make the static description string optional.
benjamn 18b3af0
Add an additional details parameter to some visitor methods.
benjamn 73c30b8
Use -Type suffix for GraphQLType details property names.
benjamn 866a7a5
Declare declaredDirectives values as GraphQLDirective, not any.
benjamn af91add
Rename GraphQLSchemaDirective to SchemaDirectiveVisitor.
benjamn a7aa136
Stop inheriting from GraphQLDirective.
benjamn 3d235b3
Add a top-level SchemaDirectiveVisitor comment.
benjamn b3bb685
Reorder constructor and improve comment about protected modifier.
benjamn f8a8dcc
Remove overzealous test requiring directives to be declared in schema.
benjamn 99ad072
Reimplement attachDirectiveResolvers using SchemaDirectiveVisitor.vis…
benjamn 0128a10
Fix an argument typo in @rest example.
benjamn 72d1cd5
Add a test of undeclared @directive argument values.
benjamn 00786cb
Test that SchemaDirectiveVisitor can be used as a no-op visitor.
benjamn 2b9401c
Test that SchemaDirectiveVisitor.getLocations works.
benjamn 37ebc31
Test that visitors can visit the schema object itself.
benjamn be78d68
Make makeExecutableSchema accept a directiveVisitors option.
benjamn 6c24668
Add a test of declared directive parameters with default values.
benjamn 7c2dc88
Final pass over SchemaDirectiveVisitor implementation comments.
benjamn f951c81
Expose this.visitedType on SchemaDirectiveVisitor instances.
benjamn 547f5d6
Return useful information from SchemaDirectiveVisitor.visitSchema.
benjamn f57036e
Skip visitors if visitorClass.shouldCall(method) returns false.
benjamn 5f385f6
Radically refactor SchemaVisitor, visitSchema, and SchemaDirectiveVis…
benjamn 3d32448
Rename VisitableType to VisitableSchemaType.
benjamn 9f22c87
Re-export schemaVisitor.ts exports from graphql-tools package.
benjamn 9838718
Move public schema member declaration to SchemaVisitor base class.
benjamn 0a9aa8b
Provide a shared context object to all visitor instances.
benjamn 2a44bb3
Improve coverage by testing implementsVisitorMethod more carefully.
benjamn 5870196
Add a test of a simple visitor that always selects itself.
benjamn a51f68b
Improve the no-op visitor test by actually verifying what happened.
benjamn 3ef5c35
Fix visitSchema visitorSelector comment to refer to VisitableSchemaType.
benjamn bfb837e
Add missing public access modifier to visitVieldDefinition in comment.
benjamn b5e347f
Introductory documentation for schema @directives.
benjamn 0d659c9
Fixing some typos.
benjamn d57c68d
Add section about implementing schema directives to docs.
benjamn 3ff3e25
Fix typo.
benjamn 67c625d
Add comment about omitted resolver argument.
benjamn 99fd3b7
Allow SchemaDirectiveVisitors to provide GraphQLDirective declarations.
benjamn 7a9139e
Throw if visitor does not implement methods for all declared locations.
benjamn 2a75280
Add documentation about directive declarations.
benjamn 43790f6
Add a number of examples to the SchemaDirectiveVisitor documentation.
benjamn 4c19400
Add tests for all directive examples, and convert examples to JS.
benjamn f8b4440
Tolerate GraphQL.js versions that do not return data from mutations.
benjamn 094b0e9
Flatten example classes a bit for readability.
benjamn 043ee0d
Conform LimitedLengthType name to older graphql-js requirements.
benjamn fc3670b
Pass GraphQLSchema instead of GraphQLDirective to getDirectiveDeclara…
benjamn cbfc21a
Remove directiveResolvers from directive docs, with note.
benjamn 69e1ede
Properly handle errors from faulty LimitedLengthType.
benjamn 6776473
Rename directiveVisitors option to just directives.
benjamn d3bdc29
Rename directiveResolvers and some subclasses in docs.
benjamn 83e23dc
Improve the @length example tests.
benjamn 8b4a2d0
Simplify intro documentation, plus many other improvements.
benjamn 552ad53
More uniform ```js Markdown language identifiers.
benjamn 9c9e7cf
Reorder LengthDirective and LimitedLengthType classes.
benjamn 193510f
Further simplify introduction, and add brief usage section.
benjamn fe33c44
Link to DirectiveLocation enum implementation.
benjamn 02d4f5a
Avoid editorial words like 'very'.
benjamn a01ce05
More tweaks to address review feedback from @stubailo.
benjamn 236ed46
Use .html in links rather than .md.
benjamn 5260e18
Tweak to title of schema directives documentation.
benjamn 34ca07a
Automatically update references to schema types that have changed.
benjamn 0439bf5
Allow each to be used for arrays as well as objects.
benjamn fb91771
Add a more powerful iteration utility called updateEachKey.
benjamn c90eb0d
Add missing GraphQLInputObjectType to VisitableSchemaType union.
benjamn a10d924
Allow visitor methods to return replacement objects, null, or undefined.
benjamn cdf2ccc
Annotate visitor method stubs with return type options.
benjamn a07ca3f
Prevent visitSchema from replacing the original GraphQLSchema object.
benjamn 01f10a7
Reconcile GraphQLObjectType .name properties with schema.getTypeMap().
benjamn 0dd82be
Add Query type to enum value removal test to make graphql@0.11 happy.
benjamn 31b8fb2
Fix bug in healSchema that sometimes removed '__'-prefixed types.
benjamn 63cec8e
Call healSchema in visitSchemaDirectives rather than in visitSchema.
benjamn 9b29251
Improve @auth example and corresponding tests.
benjamn 2c8c50d
Improve @uniqueID example.
benjamn 86666cd
Rename makeExecutableSchema({ directives }) option to schemaDirectives.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
|
||
## Directive example | ||
|
||
Let's take a look at how we can create `@upper` Directive to upper-case a string returned from resolve on Field | ||
[See a complete runnable example on Launchpad.](https://launchpad.graphql.com/p00rw37qx0) | ||
|
||
To start, let's grab the schema definition string from the `makeExecutableSchema` example [in the "Generating a schema" article](/tools/graphql-tools/generate-schema.html#example). | ||
|
||
```js | ||
import { makeExecutableSchema } from 'graphql-tools'; | ||
import { graphql } from 'graphql'; | ||
|
||
// Construct a schema, using GraphQL schema language | ||
const typeDefs = ` | ||
directive @upper on FIELD_DEFINITION | ||
|
||
type Query { | ||
hello: String @upper | ||
} | ||
`; | ||
|
||
// Implement resolvers for out custom Directive | ||
const directiveResolvers = { | ||
upper( | ||
next, | ||
src, | ||
args, | ||
context, | ||
) { | ||
return next().then((str) => { | ||
if (typeof(str) === 'string') { | ||
return str.toUpperCase(); | ||
} | ||
return str; | ||
}); | ||
}, | ||
} | ||
|
||
// Provide resolver functions for your schema fields | ||
const resolvers = { | ||
Query: { | ||
hello: (root, args, context) => { | ||
return 'Hello world!'; | ||
}, | ||
}, | ||
}; | ||
|
||
export const schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
directiveResolvers, | ||
}); | ||
|
||
const query = ` | ||
query UPPER_HELLO { | ||
hello | ||
} | ||
`; | ||
|
||
graphql(schema, query).then((result) => console.log('Got result', result)); | ||
``` | ||
|
||
> Note: next() always return a Promise for consistency, resolved with original resolver value or rejected with an error. | ||
|
||
## Multi-Directives example | ||
|
||
Multi-Directives on a field will be apply with LTR order. | ||
[See a complete runnable example on Launchpad.](https://launchpad.graphql.com/nx945rq1x7) | ||
|
||
```js | ||
// graphql-tools combines a schema string with resolvers. | ||
import { makeExecutableSchema } from 'graphql-tools'; | ||
|
||
// Construct a schema, using GraphQL schema language | ||
const typeDefs = ` | ||
directive @upper on FIELD_DEFINITION | ||
directive @concat(value: String!) on FIELD_DEFINITION | ||
|
||
type Query { | ||
foo: String @concat(value: "@gmail.com") @upper | ||
} | ||
`; | ||
|
||
// Customs directives, check https://github.com/apollographql/graphql-tools/pull/518 | ||
// for more examples | ||
const directiveResolvers = { | ||
upper( | ||
next, | ||
src, | ||
args, | ||
context, | ||
) { | ||
return next().then((str) => { | ||
if (typeof(str) === 'string') { | ||
return str.toUpperCase(); | ||
} | ||
return str; | ||
}); | ||
}, | ||
concat( | ||
next, | ||
src, | ||
args, | ||
context, | ||
) { | ||
return next().then((str) => { | ||
if (typeof(str) !== 'undefined') { | ||
return `${str}${args.value}`; | ||
} | ||
return str; | ||
}); | ||
}, | ||
} | ||
|
||
// Provide resolver functions for your schema fields | ||
const resolvers = { | ||
Query: { | ||
foo: (root, args, context) => { | ||
return 'foo'; | ||
}, | ||
}, | ||
}; | ||
|
||
// Required: Export the GraphQL.js schema object as "schema" | ||
export const schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
directiveResolvers, | ||
}); | ||
``` | ||
|
||
The result with query `{foo}` will be: | ||
```json | ||
{ | ||
"data": { | ||
"foo": "FOO@GMAIL.COM" | ||
} | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### directiveResolvers option | ||
|
||
```js | ||
import { makeExecutableSchema } from 'graphql-tools'; | ||
|
||
const directiveResolvers = { | ||
// directive resolvers implement | ||
}; | ||
|
||
const schema = makeExecutableSchema({ | ||
// ... other options | ||
directiveResolvers, | ||
}) | ||
``` | ||
|
||
`makeExecutableSchema` has new option field is `directiveResolvers`, a map object for custom Directive's resolvers. | ||
|
||
### attachDirectiveResolvers | ||
|
||
```js | ||
import { attachDirectiveResolvers } from 'graphql-tools'; | ||
|
||
const directiveResolvers = { | ||
// directive resolvers implement | ||
}; | ||
|
||
attachDirectiveResolvers({ | ||
schema, | ||
directiveResolvers, | ||
}); | ||
``` | ||
|
||
Given an instance of GraphQLSchema and a `directiveResolvers` map object, `attachDirectiveResolvers` wrap all field's resolver with directive resolvers. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the old documentation, just to be clear. The new docs are in
schema-directives.md
.