forked from maticzav/graphql-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): Fixed tests, updated logo
- Loading branch information
Showing
12 changed files
with
69 additions
and
83 deletions.
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
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
This file was deleted.
Oops, something went wrong.
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
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,50 @@ | ||
const { GraphQLServer } = require('graphql-yoga') | ||
const { makeExecutableSchema } = require('graphql-tools') | ||
const { applyMiddleware } = require('graphql-middleware') | ||
|
||
// Schema | ||
|
||
const code = 'supersecret' | ||
|
||
const typeDefs = ` | ||
type Query { | ||
open: String! | ||
secured: String! | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
open: () => `Open data, everyone's welcome!`, | ||
secured: () => `Personal diary - this is for my eyes only!`, | ||
}, | ||
} | ||
|
||
// Middleware | ||
|
||
const permissions = { | ||
Query: { | ||
secured: async (resolve, parent, args, ctx, info) => { | ||
// Include your agent code as Authorization: <token> header. | ||
const permit = ctx.request.get('Authorization') === code | ||
|
||
if (!permit) { | ||
throw new Error(`Not authorised!`) | ||
} | ||
|
||
return resolve() | ||
}, | ||
}, | ||
} | ||
|
||
// Server | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
const protectedSchema = applyMiddleware(schema, permissions) | ||
|
||
const server = new GraphQLServer({ | ||
schema: protectedSchema, | ||
context: req => ({ ...req }), | ||
}) | ||
|
||
server.start(() => console.log('Server is running on localhost:4000')) |
2 changes: 1 addition & 1 deletion
2
examples/authorization/package.json → examples/permissions/package.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "authorization", | ||
"name": "permissions", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
|
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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
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
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