Skip to content

Commit f621804

Browse files
author
Tom Coleman
authored
Simplify authorized loader.
Thoughts here: 1. We only need one authorized loader (for the `readOne` case). 2. We can determine the `authQuery` at initialization time, assuming that a single user will be used for all queries (this is the case). 3. We can have the user at initialization time, although this will require refactoring the way the context is initialized here: https://github.com/tobkle/create-graphql-server/blob/master/skel/server/index.js i. We need to add the models to the context *after* the authentication, i.e. here: https://github.com/tobkle/create-graphql-server/blob/master/skel/server/index.js#L44 ii. This will mean that we need a different reference to the mongo collection in the `passport.authenticate` call. But we can make that work I think, it doesn't really need the proper user collection.
1 parent 884a8c5 commit f621804

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

test/output-app/README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,12 @@ import { queryForRoles, findByIds } from '../server/authorize';
9898
export default class <Type> {
9999
constructor(context){
100100
...
101-
this.loaders = (_user = {}, resolver = '') => ({
102-
readOne: new DataLoader(ids => new Promise( async (resolve, reject) => {
103-
try {
104-
const authQuery = queryForRoles(_user, ['admin', 'world'], ['authorId', 'coauthorsIds'], 'readOne', { User: this.context.User }, resolver);
105-
const result = await findByIds(this.collection, ids, authQuery);
106-
resolve(result);
107-
} catch (err) { reject(err); }
108-
})),
109-
});
110-
...
101+
102+
this.unauthorizedLoader = new DataLoader(ids => findByIds(this.collection, ids));
103+
104+
const { user: me, User } = context;
105+
const authQuery = queryForRoles(me, ['admin', 'world'], ['authorId', 'coauthorsIds'], 'readOne', { User }, 'findOneLoader');
106+
this.authorizedLoader = new DataLoader(ids => findByIds(this.collection, ids, authQuery));
111107
}
112108
...
113109
async getOneById(id, _user = {}, resolver = 'tweet getOneById') {

0 commit comments

Comments
 (0)