feat: Implement getConnectedApps graphQL query

This commit is contained in:
Faruk AYDIN
2021-10-17 15:10:38 +02:00
committed by Ali BARIN
parent 8fa0178e08
commit 084a831e7a
4 changed files with 50 additions and 3 deletions

View File

@@ -0,0 +1,43 @@
import { GraphQLList, GraphQLString } from 'graphql';
import Connection from '../../models/connection';
import App from '../../models/app';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import appType from '../types/app';
type Params = {
name: string
}
const getConnectedAppsResolver = async (params: Params, req: RequestWithCurrentUser) => {
let apps = App.findAll(params.name)
const connections = await Connection.query()
.select('connections.key')
.count('connections.id as count')
.where({ user_id: req.currentUser.id, verified: true })
.groupBy('connections.key')
const connectionKeys = connections.map(connection => connection.key)
apps = apps
.filter((app: any) => connectionKeys.includes(app.key))
.map((app: any) => {
const connection = connections
.find((connection: any) => connection.key === app.key)
app.connectionCount = connection.count;
return app;
})
return apps;
}
const getConnectedApps = {
type: GraphQLList(appType),
args: {
name: { type: GraphQLString }
},
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getConnectedAppsResolver(params, req)
}
export default getConnectedApps;

View File

@@ -1,12 +1,14 @@
import { GraphQLObjectType } from 'graphql';
import getApps from './queries/get-apps';
import getApp from './queries/get-app';
import getConnectedApps from './queries/get-connected-apps';
const rootQuery = new GraphQLObjectType({
name: 'Query',
fields: {
getApps: getApps,
getApp: getApp
getApps,
getApp,
getConnectedApps
}
});

View File

@@ -1,4 +1,4 @@
import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql';
import { GraphQLObjectType, GraphQLString, GraphQLList, GraphQLInt } from 'graphql';
import fieldType from './field';
import authenticationStepType from './authentication-step';
@@ -7,6 +7,7 @@ const appType = new GraphQLObjectType({
fields: {
name: { type: GraphQLString },
key: { type: GraphQLString },
connectionCount: { type: GraphQLInt },
slug: { type: GraphQLString },
iconUrl: { type: GraphQLString },
docUrl: { type: GraphQLString },

View File

@@ -7,6 +7,7 @@ class Connection extends Base {
data!: any
userId!: number
verified: boolean
count: number
static tableName = 'connections';