feat: Implement getConnectedApps graphQL query
This commit is contained in:
43
packages/backend/src/graphql/queries/get-connected-apps.ts
Normal file
43
packages/backend/src/graphql/queries/get-connected-apps.ts
Normal 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;
|
@@ -1,12 +1,14 @@
|
|||||||
import { GraphQLObjectType } from 'graphql';
|
import { GraphQLObjectType } from 'graphql';
|
||||||
import getApps from './queries/get-apps';
|
import getApps from './queries/get-apps';
|
||||||
import getApp from './queries/get-app';
|
import getApp from './queries/get-app';
|
||||||
|
import getConnectedApps from './queries/get-connected-apps';
|
||||||
|
|
||||||
const rootQuery = new GraphQLObjectType({
|
const rootQuery = new GraphQLObjectType({
|
||||||
name: 'Query',
|
name: 'Query',
|
||||||
fields: {
|
fields: {
|
||||||
getApps: getApps,
|
getApps,
|
||||||
getApp: getApp
|
getApp,
|
||||||
|
getConnectedApps
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql';
|
import { GraphQLObjectType, GraphQLString, GraphQLList, GraphQLInt } from 'graphql';
|
||||||
import fieldType from './field';
|
import fieldType from './field';
|
||||||
import authenticationStepType from './authentication-step';
|
import authenticationStepType from './authentication-step';
|
||||||
|
|
||||||
@@ -7,6 +7,7 @@ const appType = new GraphQLObjectType({
|
|||||||
fields: {
|
fields: {
|
||||||
name: { type: GraphQLString },
|
name: { type: GraphQLString },
|
||||||
key: { type: GraphQLString },
|
key: { type: GraphQLString },
|
||||||
|
connectionCount: { type: GraphQLInt },
|
||||||
slug: { type: GraphQLString },
|
slug: { type: GraphQLString },
|
||||||
iconUrl: { type: GraphQLString },
|
iconUrl: { type: GraphQLString },
|
||||||
docUrl: { type: GraphQLString },
|
docUrl: { type: GraphQLString },
|
||||||
|
@@ -7,6 +7,7 @@ class Connection extends Base {
|
|||||||
data!: any
|
data!: any
|
||||||
userId!: number
|
userId!: number
|
||||||
verified: boolean
|
verified: boolean
|
||||||
|
count: number
|
||||||
|
|
||||||
static tableName = 'connections';
|
static tableName = 'connections';
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user