Skip to content

Commit d397712

Browse files
authored
Merge pull request #241 from kachkaev/schema-directives-example
Replace custom-directives with schema-directives example
2 parents 883af9c + d28be46 commit d397712

File tree

4 files changed

+75
-58
lines changed

4 files changed

+75
-58
lines changed

examples/custom-directives/index.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/custom-directives/README.md renamed to examples/schema-directives/README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# custom-directive
22

3-
This directory contains a simple GraphQL with custom directives example based on `graphql-yoga`.
3+
This directory contains a simple GraphQL with [schema directives](https://www.apollographql.com/docs/graphql-tools/schema-directives.html) example based on `graphql-yoga`.
44

55
## Get started
66

77
**Clone the repository:**
88

99
```sh
1010
git clone https://github.com/graphcool/graphql-yoga.git
11-
cd graphql-yoga/examples/custom-directives
11+
cd graphql-yoga/examples/schema-directives
1212
```
1313

1414
**Install dependencies and run the app:**
@@ -43,6 +43,7 @@ The server returns the following response:
4343
Note that the original `Hello Word` output from the resolver is now in upper case due to our custom `@upper` directive.
4444

4545
**Query `secret` (with role set as `admin`):**
46+
4647
```graphql
4748
query {
4849
secret
@@ -59,10 +60,9 @@ The server returns the following response:
5960
}
6061
```
6162

62-
6363
**Query `secret` (with role set as `user`):**
6464

65-
Go to `index.js:45`, change `admin` by `user` and reload the server.
65+
Go to `index.js:64`, change `admin` by `user` and reload the server.
6666

6767
```graphql
6868
query {
@@ -82,13 +82,11 @@ The server returns the following response:
8282
"message": "You are not authorized. Expected roles: admin",
8383
"locations": [
8484
{
85-
"line": 1,
86-
"column": 2
85+
"line": 2,
86+
"column": 3
8787
}
8888
],
89-
"path": [
90-
"secret"
91-
]
89+
"path": ["secret"]
9290
}
9391
]
9492
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { GraphQLServer } = require('graphql-yoga')
2+
const { SchemaDirectiveVisitor } = require('graphql-tools')
3+
const { defaultFieldResolver } = require('graphql')
4+
5+
const typeDefs = `
6+
directive @upper on FIELD_DEFINITION
7+
directive @auth(roles: [String]) on FIELD_DEFINITION
8+
9+
type Query {
10+
hello: String! @upper
11+
secret: String @auth(roles: ["admin"])
12+
}
13+
`
14+
15+
class UpperDirective extends SchemaDirectiveVisitor {
16+
visitFieldDefinition(field) {
17+
const { resolve = defaultFieldResolver } = field
18+
field.resolve = async function(...args) {
19+
const result = await resolve.apply(this, args)
20+
if (typeof result === 'string') {
21+
return result.toUpperCase()
22+
}
23+
return result
24+
}
25+
}
26+
}
27+
28+
class AuthDirective extends SchemaDirectiveVisitor {
29+
visitFieldDefinition(field) {
30+
const { resolve = defaultFieldResolver } = field
31+
const { roles: expectedRoles = [] } = this.args
32+
field.resolve = (...args) => {
33+
const [, , context] = args
34+
if (
35+
expectedRoles.length === 0 ||
36+
expectedRoles.some(r => context.roles.includes(r))
37+
) {
38+
// Call original resolver if role check has passed
39+
return resolve.apply(this, args)
40+
}
41+
42+
// We has two options here. throw an error or return null (if field is nullable).
43+
throw new Error(
44+
`You are not authorized. Expected roles: ${expectedRoles.join(', ')}`,
45+
)
46+
}
47+
}
48+
}
49+
50+
const resolvers = {
51+
Query: {
52+
hello: () => `Hello World`,
53+
secret: () => `This is very secret`,
54+
},
55+
}
56+
57+
const server = new GraphQLServer({
58+
typeDefs,
59+
resolvers,
60+
schemaDirectives: {
61+
upper: UpperDirective,
62+
auth: AuthDirective,
63+
},
64+
context: () => ({ roles: ['admin'] }),
65+
})
66+
67+
server.start(() => console.log('Server is running on localhost:4000'))

examples/custom-directives/package.json renamed to examples/schema-directives/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"start": "node ."
44
},
55
"dependencies": {
6-
"graphql-yoga": "1.6.0"
6+
"graphql-yoga": "^1.7.1"
77
}
88
}

0 commit comments

Comments
 (0)