From c0ae3d3cb2a327c5910b0a3ba01f214b45ebc3ad Mon Sep 17 00:00:00 2001 From: kgtkr Date: Wed, 17 Jan 2024 20:03:12 +0900 Subject: [PATCH] =?UTF-8?q?Token=E3=82=92server-preset=E3=81=AB=E7=A7=BB?= =?UTF-8?q?=E6=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codegen.ts | 2 + packages/server/src/resolvers/mutation.ts | 69 ------------------- packages/server/src/resolvers/query.ts | 12 ---- packages/server/src/resolvers/res.ts | 17 ----- packages/server/src/resolvers/token.ts | 12 ---- .../src/schema/history/resolvers/History.ts | 2 +- packages/server/src/schema/resolveTypes.ts | 30 ++++++++ .../resolvers/CreateTokenGeneralResponse.ts | 3 +- .../token/resolvers/Mutation/authTokenReq.ts | 13 +++- .../resolvers/Mutation/createTokenGeneral.ts | 29 +++++++- .../resolvers/Mutation/createTokenMaster.ts | 23 ++++++- .../resolvers/Mutation/createTokenReq.ts | 22 +++++- .../resolvers/Mutation/delTokenClient.ts | 13 +++- .../src/schema/token/resolvers/Query/token.ts | 14 ++-- .../schema/token/resolvers/Query/tokens.ts | 14 +++- .../schema/token/resolvers/TokenGeneral.ts | 3 + .../src/schema/token/resolvers/TokenMaster.ts | 3 +- .../src/schema/token/resolvers/TokenReq.ts | 3 +- packages/server/src/server/server.ts | 15 ++-- 19 files changed, 160 insertions(+), 139 deletions(-) delete mode 100644 packages/server/src/resolvers/token.ts create mode 100644 packages/server/src/schema/resolveTypes.ts diff --git a/codegen.ts b/codegen.ts index c3557f5..c838aa2 100644 --- a/codegen.ts +++ b/codegen.ts @@ -7,6 +7,8 @@ const config: CodegenConfig = { "src/schema": defineConfig({ typesPluginsConfig: { contextType: "../server#AppContext", + optionalResolveType: false, + resolversNonOptionalTypename: false, }, }), }, diff --git a/packages/server/src/resolvers/mutation.ts b/packages/server/src/resolvers/mutation.ts index 0f0f7c1..cec826e 100644 --- a/packages/server/src/resolvers/mutation.ts +++ b/packages/server/src/resolvers/mutation.ts @@ -308,75 +308,6 @@ export const mutation: G.MutationResolvers = { await context.ports.storageRepo.del(storage); return null; }, - delTokenClient: async (_obj, args, context, _info) => { - const client = await context.ports.clientRepo.findOne(args.client); - await context.ports.tokenRepo.delClientToken( - context.ports.authContainer.getTokenMaster(), - client.id - ); - return null; - }, - createTokenGeneral: async (_obj, args, context, _info) => { - const client = await context.ports.clientRepo.findOne(args.client); - const token = TokenGeneral.create( - context.ports.objectIdGenerator, - context.ports.authContainer.getTokenMaster(), - client, - context.ports.clock.now(), - context.ports.safeIdGenerator - ); - - const { req, token: newToken } = token.createReq( - context.ports.clock.now(), - context.ports.safeIdGenerator - ); - - await context.ports.tokenRepo.insert(newToken); - - return { - token: token.toAPI(), - req, - }; - }, - createTokenMaster: async (_obj, args, context, _info) => { - const authUser = await authFromApiParam.authUserRequestToUser( - context.ports.userRepo, - args.auth - ); - const token = TokenMaster.create( - context.ports.objectIdGenerator, - authUser, - context.ports.clock.now(), - context.ports.safeIdGenerator - ); - await context.ports.tokenRepo.insert(token); - - return token.toAPI(); - }, - authTokenReq: async (_obj, args, context, _info) => { - const token = await context.ports.tokenRepo.findOne(args.id); - if (token.type !== "general") { - throw new AtNotFoundError("トークンが見つかりません"); - } - token.authReq(args.key, context.ports.clock.now()); - return token.toAPI(); - }, - createTokenReq: async (_obj, _args, context, _info) => { - const token = await context.ports.tokenRepo.findOne( - context.ports.authContainer.getToken().id - ); - if (token.type !== "general") { - throw new AtNotFoundError("トークンが見つかりません"); - } - const { req, token: newToken } = token.createReq( - context.ports.clock.now(), - context.ports.safeIdGenerator - ); - - await context.ports.tokenRepo.update(newToken); - - return req; - }, createTopicNormal: async (_obj, args, context, _info) => { const user = await context.ports.userRepo.findOne( context.ports.authContainer.getToken().user diff --git a/packages/server/src/resolvers/query.ts b/packages/server/src/resolvers/query.ts index 80dd812..1f0ed3f 100644 --- a/packages/server/src/resolvers/query.ts +++ b/packages/server/src/resolvers/query.ts @@ -95,18 +95,6 @@ export const query: G.QueryResolvers = { ); return storages.map((x) => x.toAPI(context.ports.authContainer.getToken())); }, - token: async (_obj, _args, context, _info) => { - const token = await context.ports.tokenRepo.findOne( - context.ports.authContainer.getToken().id - ); - return token.toAPI(); - }, - tokens: async (_obj, _args, context, _info: any) => { - const tokens = await context.ports.tokenRepo.findAll( - context.ports.authContainer.getTokenMaster() - ); - return tokens.map((t) => t.toAPI()); - }, topics: async (_obj, args, context, _info) => { const topic = await context.ports.topicRepo.find( { diff --git a/packages/server/src/resolvers/res.ts b/packages/server/src/resolvers/res.ts index a4346be..19ac541 100644 --- a/packages/server/src/resolvers/res.ts +++ b/packages/server/src/resolvers/res.ts @@ -8,23 +8,6 @@ const resBase: Pick = { }, }; -export const res: G.ResResolvers = { - __resolveType(obj) { - switch (obj.type) { - case "normal": - return "ResNormal"; - case "history": - return "ResHistory"; - case "topic": - return "ResTopic"; - case "fork": - return "ResFork"; - case "delete": - return "ResDelete"; - } - }, -}; - export const resNormal: G.ResNormalResolvers = { ...resBase, reply: async (res, _args, context, _info) => { diff --git a/packages/server/src/resolvers/token.ts b/packages/server/src/resolvers/token.ts deleted file mode 100644 index 7a8440b..0000000 --- a/packages/server/src/resolvers/token.ts +++ /dev/null @@ -1,12 +0,0 @@ -import * as G from "../generated/graphql"; - -export const token: G.TokenResolvers = { - __resolveType(obj) { - switch (obj.type) { - case "general": - return "TokenGeneral"; - case "master": - return "TokenMaster"; - } - }, -}; diff --git a/packages/server/src/schema/history/resolvers/History.ts b/packages/server/src/schema/history/resolvers/History.ts index 0d07089..bd43a2c 100644 --- a/packages/server/src/schema/history/resolvers/History.ts +++ b/packages/server/src/schema/history/resolvers/History.ts @@ -8,5 +8,5 @@ export const History: HistoryResolvers = { throw new Error(); } return topic; - }, + } }; diff --git a/packages/server/src/schema/resolveTypes.ts b/packages/server/src/schema/resolveTypes.ts new file mode 100644 index 0000000..ca68854 --- /dev/null +++ b/packages/server/src/schema/resolveTypes.ts @@ -0,0 +1,30 @@ +import type { Resolvers } from "./types.generated"; + +export const resolveTypes: Resolvers = { + Token: { + __resolveType(obj) { + switch (obj.type) { + case "general": + return "TokenGeneral"; + case "master": + return "TokenMaster"; + } + }, + }, + Res: { + __resolveType(obj) { + switch (obj.type) { + case "normal": + return "ResNormal"; + case "history": + return "ResHistory"; + case "topic": + return "ResTopic"; + case "fork": + return "ResFork"; + case "delete": + return "ResDelete"; + } + }, + }, +}; diff --git a/packages/server/src/schema/token/resolvers/CreateTokenGeneralResponse.ts b/packages/server/src/schema/token/resolvers/CreateTokenGeneralResponse.ts index 553bb07..fcab606 100644 --- a/packages/server/src/schema/token/resolvers/CreateTokenGeneralResponse.ts +++ b/packages/server/src/schema/token/resolvers/CreateTokenGeneralResponse.ts @@ -1,3 +1,4 @@ import type { CreateTokenGeneralResponseResolvers } from "./../../types.generated"; export const CreateTokenGeneralResponse: CreateTokenGeneralResponseResolvers = - {}; + { +}; diff --git a/packages/server/src/schema/token/resolvers/Mutation/authTokenReq.ts b/packages/server/src/schema/token/resolvers/Mutation/authTokenReq.ts index 525806f..d1b8c0b 100644 --- a/packages/server/src/schema/token/resolvers/Mutation/authTokenReq.ts +++ b/packages/server/src/schema/token/resolvers/Mutation/authTokenReq.ts @@ -1,2 +1,11 @@ -import type { MutationResolvers } from './../../../types.generated'; - export const authTokenReq: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Mutation.authTokenReq resolver logic here */ }; \ No newline at end of file +import { AtNotFoundError } from "../../../../at-error"; +import type { MutationResolvers } from "./../../../types.generated"; + +export const authTokenReq: NonNullable = async (_obj, args, context, _info) => { + const token = await context.ports.tokenRepo.findOne(args.id); + if (token.type !== "general") { + throw new AtNotFoundError("トークンが見つかりません"); + } + token.authReq(args.key, context.ports.clock.now()); + return token.toAPI(); +}; diff --git a/packages/server/src/schema/token/resolvers/Mutation/createTokenGeneral.ts b/packages/server/src/schema/token/resolvers/Mutation/createTokenGeneral.ts index d93038b..2bc736b 100644 --- a/packages/server/src/schema/token/resolvers/Mutation/createTokenGeneral.ts +++ b/packages/server/src/schema/token/resolvers/Mutation/createTokenGeneral.ts @@ -1,2 +1,27 @@ -import type { MutationResolvers } from './../../../types.generated'; - export const createTokenGeneral: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenGeneral resolver logic here */ }; \ No newline at end of file +import { TokenGeneral } from "../../../../entities"; +import type { MutationResolvers } from "./../../../types.generated"; + +export const createTokenGeneral: NonNullable< + MutationResolvers["createTokenGeneral"] +> = async (_obj, args, context, _info) => { + const client = await context.ports.clientRepo.findOne(args.client); + const token = TokenGeneral.create( + context.ports.objectIdGenerator, + context.ports.authContainer.getTokenMaster(), + client, + context.ports.clock.now(), + context.ports.safeIdGenerator + ); + + const { req, token: newToken } = token.createReq( + context.ports.clock.now(), + context.ports.safeIdGenerator + ); + + await context.ports.tokenRepo.insert(newToken); + + return { + token: token.toAPI(), + req, + }; +}; diff --git a/packages/server/src/schema/token/resolvers/Mutation/createTokenMaster.ts b/packages/server/src/schema/token/resolvers/Mutation/createTokenMaster.ts index 28e31f8..6aac1ae 100644 --- a/packages/server/src/schema/token/resolvers/Mutation/createTokenMaster.ts +++ b/packages/server/src/schema/token/resolvers/Mutation/createTokenMaster.ts @@ -1,2 +1,21 @@ -import type { MutationResolvers } from './../../../types.generated'; - export const createTokenMaster: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenMaster resolver logic here */ }; \ No newline at end of file +import { TokenMaster } from "../../../../entities"; +import type { MutationResolvers } from "./../../../types.generated"; +import * as authFromApiParam from "../../../../server/auth-from-api-param"; + +export const createTokenMaster: NonNullable< + MutationResolvers["createTokenMaster"] +> = async (_obj, args, context, _info) => { + const authUser = await authFromApiParam.authUserRequestToUser( + context.ports.userRepo, + args.auth + ); + const token = TokenMaster.create( + context.ports.objectIdGenerator, + authUser, + context.ports.clock.now(), + context.ports.safeIdGenerator + ); + await context.ports.tokenRepo.insert(token); + + return token.toAPI(); +}; diff --git a/packages/server/src/schema/token/resolvers/Mutation/createTokenReq.ts b/packages/server/src/schema/token/resolvers/Mutation/createTokenReq.ts index 61b5480..895ae77 100644 --- a/packages/server/src/schema/token/resolvers/Mutation/createTokenReq.ts +++ b/packages/server/src/schema/token/resolvers/Mutation/createTokenReq.ts @@ -1,2 +1,20 @@ -import type { MutationResolvers } from './../../../types.generated'; - export const createTokenReq: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenReq resolver logic here */ }; \ No newline at end of file +import { AtNotFoundError } from "../../../../at-error"; +import type { MutationResolvers } from "./../../../types.generated"; +export const createTokenReq: NonNullable< + MutationResolvers["createTokenReq"] +> = async (_obj, _args, context, _info) => { + const token = await context.ports.tokenRepo.findOne( + context.ports.authContainer.getToken().id + ); + if (token.type !== "general") { + throw new AtNotFoundError("トークンが見つかりません"); + } + const { req, token: newToken } = token.createReq( + context.ports.clock.now(), + context.ports.safeIdGenerator + ); + + await context.ports.tokenRepo.update(newToken); + + return req; +}; diff --git a/packages/server/src/schema/token/resolvers/Mutation/delTokenClient.ts b/packages/server/src/schema/token/resolvers/Mutation/delTokenClient.ts index a74ee41..d22479d 100644 --- a/packages/server/src/schema/token/resolvers/Mutation/delTokenClient.ts +++ b/packages/server/src/schema/token/resolvers/Mutation/delTokenClient.ts @@ -1,2 +1,11 @@ -import type { MutationResolvers } from './../../../types.generated'; - export const delTokenClient: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Mutation.delTokenClient resolver logic here */ }; \ No newline at end of file +import type { MutationResolvers } from "./../../../types.generated"; +export const delTokenClient: NonNullable< + MutationResolvers["delTokenClient"] +> = async (_obj, args, context, _info) => { + const client = await context.ports.clientRepo.findOne(args.client); + await context.ports.tokenRepo.delClientToken( + context.ports.authContainer.getTokenMaster(), + client.id + ); + return null; +}; diff --git a/packages/server/src/schema/token/resolvers/Query/token.ts b/packages/server/src/schema/token/resolvers/Query/token.ts index effbeb8..c30ce90 100644 --- a/packages/server/src/schema/token/resolvers/Query/token.ts +++ b/packages/server/src/schema/token/resolvers/Query/token.ts @@ -1,8 +1,12 @@ import type { QueryResolvers } from "./../../../types.generated"; -export const token: NonNullable = async ( - _parent, - _arg, - _ctx +export const token: NonNullable = async ( + _obj, + _args, + context, + _info ) => { - /* Implement Query.token resolver logic here */ + const token = await context.ports.tokenRepo.findOne( + context.ports.authContainer.getToken().id + ); + return token.toAPI(); }; diff --git a/packages/server/src/schema/token/resolvers/Query/tokens.ts b/packages/server/src/schema/token/resolvers/Query/tokens.ts index a908ef9..a9571cf 100644 --- a/packages/server/src/schema/token/resolvers/Query/tokens.ts +++ b/packages/server/src/schema/token/resolvers/Query/tokens.ts @@ -1,2 +1,12 @@ -import type { QueryResolvers } from './../../../types.generated'; - export const tokens: NonNullable = async (_parent, _arg, _ctx) => { /* Implement Query.tokens resolver logic here */ }; \ No newline at end of file +import type { QueryResolvers } from "./../../../types.generated"; +export const tokens: NonNullable = async ( + _obj, + _args, + context, + _info +) => { + const tokens = await context.ports.tokenRepo.findAll( + context.ports.authContainer.getTokenMaster() + ); + return tokens.map((t) => t.toAPI()); +}; diff --git a/packages/server/src/schema/token/resolvers/TokenGeneral.ts b/packages/server/src/schema/token/resolvers/TokenGeneral.ts index 1c27775..5b49861 100644 --- a/packages/server/src/schema/token/resolvers/TokenGeneral.ts +++ b/packages/server/src/schema/token/resolvers/TokenGeneral.ts @@ -4,4 +4,7 @@ export const TokenGeneral: TokenGeneralResolvers = { const client = await context.ports.clientLoader.load(token.clientID); return client.toAPI(context.ports.authContainer.getTokenMasterOrNull()); }, + date: () => { /* TokenGeneral.date resolver is required because TokenGeneral.date exists but TokenGeneralMapper.date does not */ }, + id: () => { /* TokenGeneral.id resolver is required because TokenGeneral.id exists but TokenGeneralMapper.id does not */ }, + key: () => { /* TokenGeneral.key resolver is required because TokenGeneral.key exists but TokenGeneralMapper.key does not */ } }; diff --git a/packages/server/src/schema/token/resolvers/TokenMaster.ts b/packages/server/src/schema/token/resolvers/TokenMaster.ts index c4991d6..bd32e8f 100644 --- a/packages/server/src/schema/token/resolvers/TokenMaster.ts +++ b/packages/server/src/schema/token/resolvers/TokenMaster.ts @@ -1,2 +1,3 @@ import type { TokenMasterResolvers } from "./../../types.generated"; -export const TokenMaster: TokenMasterResolvers = {}; +export const TokenMaster: TokenMasterResolvers = { +}; diff --git a/packages/server/src/schema/token/resolvers/TokenReq.ts b/packages/server/src/schema/token/resolvers/TokenReq.ts index 3c12da7..2370c55 100644 --- a/packages/server/src/schema/token/resolvers/TokenReq.ts +++ b/packages/server/src/schema/token/resolvers/TokenReq.ts @@ -1,2 +1,3 @@ import type { TokenReqResolvers } from "./../../types.generated"; -export const TokenReq: TokenReqResolvers = {}; +export const TokenReq: TokenReqResolvers = { +}; diff --git a/packages/server/src/server/server.ts b/packages/server/src/server/server.ts index ed76402..af97b09 100644 --- a/packages/server/src/server/server.ts +++ b/packages/server/src/server/server.ts @@ -1,10 +1,11 @@ import cors from "@koa/cors"; import Koa from "koa"; -import * as fs from "fs/promises"; import { createServer } from "http"; import { AtError } from "../at-error"; import { Config } from "../config"; -import { resolvers as appResolvers } from "../resolvers"; +import { resolvers } from "../schema/resolvers.generated"; +import { resolveTypes } from "../schema/resolveTypes"; +import { typeDefs } from "../schema/typeDefs.generated"; import { runWorker } from "../worker"; import { AppContext, createContext } from "./context"; import Router from "@koa/router"; @@ -22,18 +23,16 @@ import bodyParser from "koa-bodyparser"; import { GraphQLError } from "graphql"; export async function serverRun() { - const typeDefs = await fs.readFile( - require.resolve("../../schema.gql"), - "utf8" - ); - const app = new Koa(); app.use(cors()); app.use(bodyParser()); const router = new Router(); const httpServer = createServer(app.callback()); - const schema = makeExecutableSchema({ typeDefs, resolvers: appResolvers }); + const schema = makeExecutableSchema({ + typeDefs, + resolvers: [resolvers, resolveTypes], + }); const wsServer = new WebSocketServer({ server: httpServer, path: "/",