-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL Middleware
- Loading branch information
Showing
19 changed files
with
6,992 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
docker: | ||
- image: 'circleci/node:latest' | ||
steps: | ||
- checkout | ||
- run: | ||
name: install | ||
command: | | ||
sudo npm install -g yarn | ||
yarn install | ||
- run: yarn test | ||
- run: yarn semantic-release |
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 |
---|---|---|
|
@@ -2,3 +2,5 @@ node_modules | |
dist | ||
.idea | ||
.DS_Store | ||
*.log* | ||
package-lock.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"trailingComma": "all", | ||
"singleQuote": true | ||
} |
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,13 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "AVA tests debug", | ||
"program": "${workspaceFolder}/node_modules/ava/profile.js", | ||
"args": ["--serial", "${file}"], | ||
"skipFiles": ["<node_internals>/**/*.js"] | ||
} | ||
] | ||
} |
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,3 @@ | ||
{ | ||
"eslint.enable": false | ||
} |
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,57 @@ | ||
const { GraphQLServer } = require('graphql-yoga') | ||
const { makeExecutableSchema } = require('graphql-tools') | ||
const { applyFieldMiddleware } = require('graphql-middleware') | ||
|
||
// Agents | ||
|
||
const agents = [ | ||
{ id: '1', name: 'Matt', code: 'mysecret' }, | ||
{ id: '2', name: 'Joh', code: 'cool' }, | ||
{ id: '3', name: 'Em', code: 'donttell' }, | ||
] | ||
|
||
// Schema | ||
|
||
const typeDefs = ` | ||
type Query { | ||
hello(name: String): String! | ||
agent(mission: String!): String! | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: (_, { name }) => `Hello ${name || 'World'}`, | ||
agent: (_, { mission }) => | ||
`Top secret information about your mission (${mission})`, | ||
}, | ||
} | ||
|
||
// Middleware | ||
|
||
const authMiddleware = { | ||
Query: { | ||
agent: (resolve, parent, args, ctx, info) => { | ||
// Include your agent code as Authorization: <token> header. | ||
const code = ctx.request.get('Authorization') | ||
const permit = agents.some(agent => agent.code === code) | ||
|
||
if (!permit) { | ||
throw new Error(`Not authorised!`) | ||
} | ||
return resolve() | ||
}, | ||
}, | ||
} | ||
|
||
// Server | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
const securedSchema = applyFieldMiddleware(schema, authMiddleware) | ||
|
||
const server = new GraphQLServer({ | ||
schema: securedSchema, | ||
context: req => ({ ...req }), | ||
}) | ||
|
||
server.start(() => console.log('Server is running on localhost:4000')) |
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,11 @@ | ||
{ | ||
"name": "authorization", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"graphql-middleware": "latest", | ||
"graphql-tools": "^2.23.1", | ||
"graphql-yoga": "latest" | ||
} | ||
} |
Oops, something went wrong.