Skip to content

Commit

Permalink
Merge pull request #17 from 4Catalyzer/createContext
Browse files Browse the repository at this point in the history
feat: Create new context for each event
  • Loading branch information
taion authored May 24, 2018
2 parents 23857e6 + e24b1a3 commit e41c3b0
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 333 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
/flow-typed/**
lib/
es/
examples/
examples/*/token.json
package.json
CHANGELOG.md
12 changes: 11 additions & 1 deletion examples/todo/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module.exports = {
extends: '../../.babelrc.js',
presets: [
[
'@4c/4catalyzer',
{
target: 'node',
useBuiltIns: false,
exclude: ['proposal-async-generator-functions'], // https://github.com/babel/babel/pull/8003
},
],
'@babel/flow',
],
};
5 changes: 5 additions & 0 deletions examples/todo/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"extends": ["4catalyzer-flow-react", "prettier", "prettier/flowtype"],
"rules": {
"flowtype/require-valid-file-annotation": "off",

// Don't fail linting if example dependencies aren't installed.
"import/extensions": "off",
"import/no-unresolved": "off",

"jsx-a11y/anchor-is-valid": [
"error",
{
Expand Down
1 change: 1 addition & 0 deletions examples/todo/js/NetworkLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class NetworkLayer extends Relay.DefaultNetworkLayer {
this.token = null;
this.client = new SubscriptionClient();
}

setToken(token) {
if (token) {
this._init.headers = {
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/js/SubscriptionClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class SubscriptionClient {
this.requests = new Map();

this.socket.on('app_error', err => {
console.log(err);
console.log(err); // eslint-disable-line no-console
});

this.socket.on('connect', () => {
Expand Down
14 changes: 7 additions & 7 deletions examples/todo/js/components/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ class Todo extends React.Component {
})}
>
<div className="view">
<input
checked={todo.complete}
className="toggle"
id={`todo-input-${todo.id}`}
onChange={this._handleCompleteChange}
type="checkbox"
/>
<label
htmlFor={`todo-input-${todo.id}`}
onDoubleClick={this._handleLabelDoubleClick}
>
<input
checked={todo.complete}
className="toggle"
id={`todo-input-${todo.id}`}
onChange={this._handleCompleteChange}
type="checkbox"
/>
{todo.text}
</label>
<button className="destroy" onClick={this._handleDestroyClick} />
Expand Down
41 changes: 21 additions & 20 deletions examples/todo/package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
{
"private": true,
"scripts": {
"start": "npm run create-token && babel-node server.js",
"check": "flow check",
"create-token": "node tools/create-token.js",
"start": "npm run create-token && babel-node server.js",
"update-schema": "babel-node tools/updateSchema.js"
},
"dependencies": {
"@4c/babel-preset-4catalyzer": "^1.2.1",
"@4c/graphql-subscription-server": "link:../..",
"@babel/core": "^7.0.0-beta.41",
"@babel/node": "^7.0.0-beta.41",
"@babel/polyfill": "^7.0.0-beta.41",
"@babel/preset-flow": "^7.0.0-beta.41",
"babel-loader": "^7.1.4",
"babel-plugin-relay": "^1.4.1",
"@4c/babel-preset-4catalyzer": "^1.4.0",
"@4c/graphql-subscription-server": "../..",
"@babel/core": "^7.0.0-beta.47",
"@babel/node": "^7.0.0-beta.47",
"@babel/polyfill": "^7.0.0-beta.47",
"@babel/preset-flow": "^7.0.0-beta.47",
"babel-loader": "^8.0.0-beta.3",
"babel-plugin-relay": "~1.5.0",
"classnames": "^2.2.5",
"express": "^4.16.3",
"express-graphql": "^0.6.11",
"graphql": "^0.13.1",
"graphql-relay": "^0.5.3",
"express-graphql": "^0.6.12",
"flow-bin": "^0.72.0",
"graphql": "^0.13.2",
"graphql-relay": "^0.5.5",
"graphql-relay-subscription": "^0.2.0",
"history": "^2.1.2",
"jsonwebtoken": "^8.2.0",
"jsonwebtoken": "^8.2.1",
"jwks-rsa": "^1.2.1",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react": "^15.6.2",
"react-dom": "^15.6.2",
"react-relay": "~1.5.0",
"react-router": "^2.8.1",
"react-router-relay": "^0.14.0",
"relay-subscriptions": "^2.0.2",
"socket.io-client": "^2.0.4",
"todomvc-app-css": "^2.1.0",
"todomvc-common": "^1.0.4",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.4"
"socket.io-client": "^2.1.1",
"todomvc-app-css": "^2.1.2",
"todomvc-common": "^1.0.5",
"webpack": "^3.12.0",
"webpack-dev-server": "^2.11.2"
}
}
18 changes: 7 additions & 11 deletions examples/todo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import WebpackDevServer from 'webpack-dev-server';

import SubscriptionServer from '@4c/graphql-subscription-server/lib/SubscriptionServer';
import EventSubscriber from '@4c/graphql-subscription-server/lib/EventSubscriber';
import JwtCredentialManager, {
import JwtCredentialsManager, {
type JwtCredentials,
} from '@4c/graphql-subscription-server/lib/JwtCredentialsManager';
import database from './data/database';
Expand Down Expand Up @@ -58,13 +58,13 @@ type Credentials = JwtCredentials & {
user: ?string,
};

class CredentialManager extends JwtCredentialManager<Credentials> {
class CredentialsManager extends JwtCredentialsManager<Credentials> {
getCredentialsFromAuthorization(token) {
const { header } = jwt.decode(token, { complete: true });
const jwkSet = require('./jwk-set.json'); // eslint-disable-line global-require

const jwk = jwkSet.keys.find(k => k.kid === header.kid);
if (!jwk) return this.config.initialCredentials;
if (!jwk) return null;

return new Promise((resolve, reject) => {
jwt.verify(
Expand All @@ -85,17 +85,13 @@ class CredentialManager extends JwtCredentialManager<Credentials> {
}

const subscriptionServer = new SubscriptionServer({
schema,
createContext,
path: '/subscriptions',
schema,
subscriber: new EventSubscriber(database),
hasPermission: () => true,
createCredentialsManager: () =>
new CredentialManager({
gracePeriod: 10,
enableTokenRenewal: false,
initialCredentials: { user: null, exp: 0 },
}),
new CredentialsManager({ tokenExpirationMarginSeconds: null }),
hasPermission: () => true,
createContext,
createLogger: (group = '') => (level, message, meta) => {
console.log(
`${level}:${group && ` [${group}]`} ${message} ${
Expand Down
Loading

0 comments on commit e41c3b0

Please sign in to comment.