Skip to content

Commit

Permalink
Tokenをserver-presetに移植
Browse files Browse the repository at this point in the history
  • Loading branch information
kgtkr committed Jan 17, 2024
1 parent b6d08ac commit c0ae3d3
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 139 deletions.
2 changes: 2 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const config: CodegenConfig = {
"src/schema": defineConfig({
typesPluginsConfig: {
contextType: "../server#AppContext",
optionalResolveType: false,
resolversNonOptionalTypename: false,
},
}),
},
Expand Down
69 changes: 0 additions & 69 deletions packages/server/src/resolvers/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions packages/server/src/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
17 changes: 0 additions & 17 deletions packages/server/src/resolvers/res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ const resBase: Pick<G.ResResolvers, "topic"> = {
},
};

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) => {
Expand Down
12 changes: 0 additions & 12 deletions packages/server/src/resolvers/token.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/server/src/schema/history/resolvers/History.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const History: HistoryResolvers = {
throw new Error();
}
return topic;
},
}
};
30 changes: 30 additions & 0 deletions packages/server/src/schema/resolveTypes.ts
Original file line number Diff line number Diff line change
@@ -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";
}
},
},
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CreateTokenGeneralResponseResolvers } from "./../../types.generated";
export const CreateTokenGeneralResponse: CreateTokenGeneralResponseResolvers =
{};
{
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import type { MutationResolvers } from './../../../types.generated';
export const authTokenReq: NonNullable<MutationResolvers['authTokenReq']> = async (_parent, _arg, _ctx) => { /* Implement Mutation.authTokenReq resolver logic here */ };
import { AtNotFoundError } from "../../../../at-error";
import type { MutationResolvers } from "./../../../types.generated";

export const authTokenReq: NonNullable<MutationResolvers['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();
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
import type { MutationResolvers } from './../../../types.generated';
export const createTokenGeneral: NonNullable<MutationResolvers['createTokenGeneral']> = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenGeneral resolver logic here */ };
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,
};
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
import type { MutationResolvers } from './../../../types.generated';
export const createTokenMaster: NonNullable<MutationResolvers['createTokenMaster']> = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenMaster resolver logic here */ };
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();
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
import type { MutationResolvers } from './../../../types.generated';
export const createTokenReq: NonNullable<MutationResolvers['createTokenReq']> = async (_parent, _arg, _ctx) => { /* Implement Mutation.createTokenReq resolver logic here */ };
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;
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import type { MutationResolvers } from './../../../types.generated';
export const delTokenClient: NonNullable<MutationResolvers['delTokenClient']> = async (_parent, _arg, _ctx) => { /* Implement Mutation.delTokenClient resolver logic here */ };
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;
};
14 changes: 9 additions & 5 deletions packages/server/src/schema/token/resolvers/Query/token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { QueryResolvers } from "./../../../types.generated";
export const token: NonNullable<QueryResolvers["token"]> = async (
_parent,
_arg,
_ctx
export const token: NonNullable<QueryResolvers['token']> = 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();
};
14 changes: 12 additions & 2 deletions packages/server/src/schema/token/resolvers/Query/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import type { QueryResolvers } from './../../../types.generated';
export const tokens: NonNullable<QueryResolvers['tokens']> = async (_parent, _arg, _ctx) => { /* Implement Query.tokens resolver logic here */ };
import type { QueryResolvers } from "./../../../types.generated";
export const tokens: NonNullable<QueryResolvers['tokens']> = async (
_obj,
_args,
context,
_info
) => {
const tokens = await context.ports.tokenRepo.findAll(
context.ports.authContainer.getTokenMaster()
);
return tokens.map((t) => t.toAPI());
};
3 changes: 3 additions & 0 deletions packages/server/src/schema/token/resolvers/TokenGeneral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ }
};
3 changes: 2 additions & 1 deletion packages/server/src/schema/token/resolvers/TokenMaster.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import type { TokenMasterResolvers } from "./../../types.generated";
export const TokenMaster: TokenMasterResolvers = {};
export const TokenMaster: TokenMasterResolvers = {
};
3 changes: 2 additions & 1 deletion packages/server/src/schema/token/resolvers/TokenReq.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import type { TokenReqResolvers } from "./../../types.generated";
export const TokenReq: TokenReqResolvers = {};
export const TokenReq: TokenReqResolvers = {
};
15 changes: 7 additions & 8 deletions packages/server/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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: "/",
Expand Down

0 comments on commit c0ae3d3

Please sign in to comment.