refactor: Use related query from objectionjs for mutations
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
e7c537f217
commit
86d5cceec7
@@ -4,20 +4,25 @@ import authLinkType from '../types/auth-link';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
id: number,
|
||||
}
|
||||
id: number;
|
||||
};
|
||||
|
||||
const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const connection = await Connection.query().findOne({
|
||||
user_id: req.currentUser.id,
|
||||
id: params.id
|
||||
}).throwIfNotFound();
|
||||
const createAuthDataResolver = async (
|
||||
params: Params,
|
||||
req: RequestWithCurrentUser
|
||||
) => {
|
||||
const connection = await req.currentUser
|
||||
.$relatedQuery('connections')
|
||||
.findOne({
|
||||
id: params.id,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
||||
|
||||
const appInstance = new appClass({
|
||||
consumerKey: connection.data.consumerKey,
|
||||
consumerSecret: connection.data.consumerSecret
|
||||
consumerSecret: connection.data.consumerSecret,
|
||||
});
|
||||
|
||||
const authLink = await appInstance.authenticationClient.createAuthData();
|
||||
@@ -25,19 +30,20 @@ const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUse
|
||||
await connection.$query().patch({
|
||||
data: {
|
||||
...connection.data,
|
||||
...authLink
|
||||
}
|
||||
})
|
||||
...authLink,
|
||||
},
|
||||
});
|
||||
|
||||
return authLink;
|
||||
}
|
||||
};
|
||||
|
||||
const createAuthData = {
|
||||
type: authLinkType,
|
||||
args: {
|
||||
id: { type: GraphQLNonNull(GraphQLInt) },
|
||||
},
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createAuthDataResolver(params, req)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
|
||||
createAuthDataResolver(params, req),
|
||||
};
|
||||
|
||||
export default createAuthData;
|
||||
|
Reference in New Issue
Block a user