get groups from login server, first running api call to login server

This commit is contained in:
einhornimmond 2021-06-24 17:32:54 +02:00 committed by Ulf Gebhardt
parent 0224cac092
commit 0063996512
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
4 changed files with 36 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import axios from 'axios'
import CONFIG from '../config'
// eslint-disable-next-line no-unused-vars
import regeneratorRuntime from 'regenerator-runtime'
// import regeneratorRuntime from 'regenerator-runtime'
// control email-text sended with email verification code
const EMAIL_TYPE = {
@ -24,14 +24,14 @@ const apiGet = async (url: string) => {
}
}
const apiPost = async (url: string, payload: string) => {
const apiPost = async (url: string, payload: any): Promise<any> => {
try {
const result = await axios.post(url, payload)
if (result.status !== 200) {
throw new Error('HTTP Status Error ' + result.status)
}
if (result.data.state === 'warning') {
return { success: true, result: result.error }
return { success: true, result: result.data.errors }
}
if (result.data.state !== 'success') {
throw new Error(result.data.msg)
@ -42,6 +42,15 @@ const apiPost = async (url: string, payload: string) => {
}
}
interface NetworkInfosResult {
state: string
msg?: string
errors: string[]
data: {
groups?: string[]
}
}
const loginAPI = {
login: async (email: string, password: string): Promise<any> => {
const payload: any = {
@ -158,6 +167,9 @@ const loginAPI = {
checkUsername: async (username: string, groupId = 1): Promise<any> => {
return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`)
},
getNetworkInfos: async (ask: string[]): Promise<NetworkInfosResult> => {
return (await apiPost(CONFIG.LOGIN_API_URL + `networkInfos`, { ask: ask })).result.data
},
}
export default loginAPI
export { loginAPI, NetworkInfosResult }

View File

@ -1,5 +1,8 @@
// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env)
import dotenv from 'dotenv'
dotenv.config()
const server = {
LOGIN_API_URL: process.env.LOGIN_API_URL || 'http://localhost/login_api/',
COMMUNITY_API_URL: process.env.COMMUNITY_API_URL || 'http://localhost/api/',

View File

@ -1,17 +1,26 @@
import { Resolver, Query, Mutation, Arg } from 'type-graphql'
import { Group } from '../models/Group'
import { LoginServerAPI } from '../datasources/loginServer'
import { loginAPI, NetworkInfosResult } from '../../apis/loginAPI'
@Resolver()
export class UserResolver {
export class GroupResolver {
@Query(() => [Group])
groups(): Group[] {
const loginServer = new LoginServerAPI()
return loginServer.getAllGroupAliases()
async groups(): Promise<Group[]> {
// eslint-disable-next-line no-console
console.log('group resolver')
const result: NetworkInfosResult = await loginAPI.getNetworkInfos(['groups'])
const groups: Group[] = []
result.data.groups?.forEach((alias: string) => {
console.log("alias: ", alias)
const group = new Group()
group.alias = alias
groups.push(group)
})
return groups
}
@Query(() => Group)
group(@Arg('id') id: string): Promise<Group | undefined> {
return Group.findOne({ where: { id } })
}
}

View File

@ -5,12 +5,12 @@ import { graphqlHTTP } from 'express-graphql'
import { buildSchema } from 'type-graphql'
import { BookResolver } from './graphql/resolvers/BookResolver'
import { UserResolver } from './graphql/resolvers/UserResolver'
import LoginServerAPI = require('./graphql/datasources/loginServer')
import { GroupResolver } from './graphql/resolvers/GroupResolver'
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
async function main() {
// const connection = await createConnection()
const schema = await buildSchema({ resolvers: [BookResolver] })
const schema = await buildSchema({ resolvers: [BookResolver, GroupResolver] })
const server = express()
server.use(