This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4015 from withspectrum/2.4.42
2.4.42
- Loading branch information
Showing
82 changed files
with
1,981 additions
and
1,134 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 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,119 @@ | ||
// @flow | ||
import { ApolloServer } from 'apollo-server-express'; | ||
import depthLimit from 'graphql-depth-limit'; | ||
import costAnalysis from 'graphql-cost-analysis'; | ||
import createLoaders from './loaders'; | ||
import createErrorFormatter from './utils/create-graphql-error-formatter'; | ||
import schema from './schema'; | ||
import { getUser, setUserOnline } from './models/user'; | ||
import { getUserIdFromReq } from './utils/session-store'; | ||
import UserError from './utils/UserError'; | ||
import type { DBUser } from 'shared/types'; | ||
|
||
// NOTE(@mxstbr): Evil hack to make graphql-cost-analysis work with Apollo Server v2 | ||
// @see pa-bru/graphql-cost-analysis#12 | ||
// @author @arianon | ||
class ProtectedApolloServer extends ApolloServer { | ||
async createGraphQLServerOptions( | ||
req: express$Request, | ||
res: express$Response | ||
): Promise<*> { | ||
const options = await super.createGraphQLServerOptions(req, res); | ||
|
||
return { | ||
...options, | ||
validationRules: [ | ||
...options.validationRules, | ||
costAnalysis({ | ||
maximumCost: 750, | ||
defaultCost: 1, | ||
variables: req.body.variables, | ||
createError: (max, actual) => { | ||
const err = new UserError( | ||
`GraphQL query exceeds maximum complexity, please remove some nesting or fields and try again. (max: ${max}, actual: ${actual})` | ||
); | ||
return err; | ||
}, | ||
}), | ||
], | ||
}; | ||
} | ||
} | ||
|
||
const server = new ProtectedApolloServer({ | ||
schema, | ||
formatError: createErrorFormatter(), | ||
// For subscriptions, this gets passed "connection", for everything else "req" and "res" | ||
context: ({ req, res, connection, ...rest }, ...other) => { | ||
if (connection) { | ||
return { | ||
...(connection.context || {}), | ||
}; | ||
} | ||
|
||
const loaders = createLoaders(); | ||
let currentUser = req.user && !req.user.bannedAt ? req.user : null; | ||
|
||
return { | ||
loaders, | ||
updateCookieUserData: (data: DBUser) => | ||
new Promise((res, rej) => | ||
req.login(data, err => (err ? rej(err) : res())) | ||
), | ||
user: currentUser, | ||
}; | ||
}, | ||
subscriptions: { | ||
path: '/websocket', | ||
onOperation: (_: any, params: Object) => { | ||
const errorFormatter = createErrorFormatter(); | ||
params.formatError = errorFormatter; | ||
return params; | ||
}, | ||
onDisconnect: rawSocket => { | ||
return getUserIdFromReq(rawSocket.upgradeReq) | ||
.then(id => id && setUserOnline(id, false)) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
}, | ||
onConnect: (connectionParams, rawSocket) => | ||
getUserIdFromReq(rawSocket.upgradeReq) | ||
.then(id => (id ? setUserOnline(id, true) : null)) | ||
.then(user => { | ||
return { | ||
user: user || null, | ||
loaders: createLoaders({ cache: false }), | ||
}; | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
return { | ||
loaders: createLoaders({ cache: false }), | ||
}; | ||
}), | ||
}, | ||
playground: { | ||
settings: { | ||
'editor.theme': 'light', | ||
}, | ||
tabs: [ | ||
{ | ||
endpoint: 'http://localhost:3001/api', | ||
query: `{ | ||
user(username: "mxstbr") { | ||
id | ||
username | ||
} | ||
}`, | ||
}, | ||
], | ||
}, | ||
maxFileSize: 25 * 1024 * 1024, // 25MB | ||
engine: false, | ||
tracing: true, | ||
cacheControl: true, | ||
validationRules: [depthLimit(10)], | ||
}); | ||
|
||
export default server; |
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
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
52 changes: 52 additions & 0 deletions
52
api/migrations/20181001073107-denormalize-channel-community-member-counts.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,52 @@ | ||
exports.up = function(r, conn) { | ||
return Promise.all([ | ||
r | ||
.table('communities') | ||
.update( | ||
{ | ||
memberCount: r | ||
.table('usersCommunities') | ||
.getAll(r.row('id'), { index: 'communityId' }) | ||
.filter(row => row('isMember').eq(true)) | ||
.count() | ||
.default(1), | ||
}, | ||
{ | ||
nonAtomic: true, | ||
} | ||
) | ||
.run(conn), | ||
r | ||
.table('channels') | ||
.update( | ||
{ | ||
memberCount: r | ||
.table('usersChannels') | ||
.getAll(r.row('id'), { index: 'channelId' }) | ||
.filter(row => row('isMember').eq(true)) | ||
.count() | ||
.default(1), | ||
}, | ||
{ | ||
nonAtomic: true, | ||
} | ||
) | ||
.run(conn), | ||
]).catch(err => console.error(err)); | ||
}; | ||
exports.down = function(r, conn) { | ||
return Promise.all([ | ||
r | ||
.table('communities') | ||
.update({ | ||
memberCount: r.literal(), | ||
}) | ||
.run(conn), | ||
r | ||
.table('channels') | ||
.update({ | ||
memberCount: r.literal(), | ||
}) | ||
.run(conn), | ||
]); | ||
}; |
Oops, something went wrong.