Skip to content

Commit ca0f463

Browse files
authored
Merge pull request #1 from Minminzei/feature/ver1.0.0
Feature/ver1.0.0
2 parents 034f3d9 + da55878 commit ca0f463

File tree

76 files changed

+4208
-1259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+4208
-1259
lines changed

App.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
33
import useCachedResources from "./hooks/useCachedResources";
44
import useColorScheme from "./hooks/useColorScheme";
55
import Navigation from "@navigation/index";
6+
import { RecoilRoot } from "recoil";
67
import { RelayEnvironmentProvider } from "react-relay/hooks";
78
import RelayEnvironment from "./RelayEnvironment";
89

@@ -15,11 +16,13 @@ export default function App() {
1516
} else {
1617
return (
1718
<RelayEnvironmentProvider environment={RelayEnvironment}>
18-
<SafeAreaProvider>
19-
<SafeAreaView style={styles.container}>
20-
<Navigation colorScheme={colorScheme} />
21-
</SafeAreaView>
22-
</SafeAreaProvider>
19+
<RecoilRoot>
20+
<SafeAreaProvider>
21+
<SafeAreaView style={styles.container}>
22+
<Navigation colorScheme={colorScheme} />
23+
</SafeAreaView>
24+
</SafeAreaProvider>
25+
</RecoilRoot>
2326
</RelayEnvironmentProvider>
2427
);
2528
}

RelayEnvironment.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RequestParameters,
99
Variables,
1010
} from "relay-runtime";
11-
import axios, { AxiosResponse } from "axios";
11+
import axios from "axios";
1212

1313
const axiosInstance = axios.create({
1414
headers: {
@@ -50,6 +50,8 @@ async function fetchRelay(params: RequestParameters, variables: any) {
5050
return fetchGraphQL(<string>params.text, variables);
5151
}
5252

53+
// const storeObject = new Store(new RecordSource());
54+
5355
// Export a singleton instance of Relay Environment configured with our network function:
5456
export default new Environment({
5557
network: Network.create(fetchRelay),

api/convertId.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

api/fromGlobalId.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { fromGlobalId } from "graphql-relay";
2+
3+
export default function decodeId(globalId: string): number {
4+
const { id } = fromGlobalId(globalId);
5+
const typeId = Number(id);
6+
if (isNaN(typeId)) {
7+
throw new Error("invalid id");
8+
}
9+
return typeId;
10+
}

api/resolver/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { GraphQLObjectType } from "graphql";
2+
import node from "@api/resolver/query/node";
23
import user from "@api/resolver/query/user";
34
import chat from "@api/resolver/query/chat";
45
import chats from "@api/resolver/query/chats";
56
import viewer from "@api/resolver/query/viewer";
6-
import myChats from "@api/resolver/query/myChats";
7+
import viewerChats from "@api/resolver/query/viewerChats";
78
import updateProfile from "@api/resolver/mutation/updateProfile";
89
import createChat from "@api/resolver/mutation/createChat";
910
import removeChat from "@api/resolver/mutation/removeChat";
@@ -13,11 +14,12 @@ import removePost from "@api/resolver/mutation/removePost";
1314
const query = new GraphQLObjectType({
1415
name: "Query",
1516
fields: {
17+
node,
1618
user,
1719
chat,
1820
chats,
1921
viewer,
20-
myChats,
22+
viewerChats,
2123
},
2224
});
2325

api/resolver/mutation/createChat.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
GraphQLInputObjectType,
77
GraphQLUnionType,
88
} from "graphql";
9-
import { decode } from "@api/convertId";
10-
import { ChatType, ChatModel } from "@api/types/chat";
9+
import fromGlobalId from "@api/fromGlobalId";
10+
import { ChatModel, ChatEdgeModel, ChatEdgeType } from "@api/types/chat";
1111
import prisma from "@database/lib";
1212
import ErrorType from "@api/types/error";
1313

@@ -16,7 +16,11 @@ interface CreateChatInput {
1616
title: string;
1717
}
1818

19-
type Result = ChatModel | ErrorType;
19+
type Result =
20+
| {
21+
chatEdges: ChatEdgeModel;
22+
}
23+
| ErrorType;
2024

2125
const createChatInput = new GraphQLInputObjectType({
2226
name: "CreateChatInput",
@@ -31,55 +35,72 @@ async function create(data: CreateChatInput): Promise<Result> {
3135
const { id } = await prisma.chat.create({
3236
data: {
3337
title: data.title,
34-
user_id: decode(data.user_id),
38+
user_id: fromGlobalId(data.user_id),
3539
},
3640
});
3741
const chat = await prisma.chat.findUnique({
3842
where: {
3943
id,
4044
},
41-
include: { user: true },
45+
include: {
46+
user: true,
47+
},
4248
});
4349
if (!chat) {
4450
throw new Error("チャットを作成できませんでした");
4551
}
46-
return new ChatModel(chat);
52+
53+
return {
54+
chatEdges: new ChatEdgeModel({
55+
cursor: id,
56+
node: new ChatModel(chat),
57+
}),
58+
};
4759
} catch (e: any) {
4860
return {
4961
message: e.message,
5062
};
5163
}
5264
}
5365

54-
const ChatCreatedErrorType = new GraphQLObjectType({
55-
name: "ChatCreatedError",
56-
fields: {
57-
message: { type: new GraphQLNonNull(GraphQLString) },
58-
},
59-
});
60-
61-
const ChatCreatedResultType = new GraphQLUnionType({
62-
name: "ChatCreatedResult",
63-
types: [ChatType, ChatCreatedErrorType],
64-
resolveType: (value) => {
65-
if (value instanceof ChatModel) {
66-
return "Chat";
67-
}
68-
return "ChatCreatedError";
69-
},
70-
});
71-
72-
const createChat = {
73-
type: new GraphQLNonNull(ChatCreatedResultType),
66+
export default {
67+
type: new GraphQLNonNull(
68+
new GraphQLUnionType({
69+
name: "ChatCreatedResult",
70+
types: [
71+
new GraphQLObjectType({
72+
name: "ChatEdges",
73+
fields: {
74+
chatEdges: {
75+
type: ChatEdgeType,
76+
},
77+
},
78+
}),
79+
new GraphQLObjectType({
80+
name: "ChatCreatedError",
81+
fields: {
82+
message: { type: new GraphQLNonNull(GraphQLString) },
83+
},
84+
}),
85+
],
86+
resolveType: (value) => {
87+
if (value.chatEdges instanceof ChatEdgeModel) {
88+
return "ChatEdges";
89+
}
90+
return "ChatCreatedError";
91+
},
92+
})
93+
),
7494
args: {
7595
input: { type: new GraphQLNonNull(createChatInput) },
7696
},
77-
resolve(obj: any, { input }: { input: CreateChatInput }): Promise<Result> {
97+
resolve(
98+
obj: undefined,
99+
{ input }: { input: CreateChatInput }
100+
): Promise<Result> {
78101
return new Promise(async (resolve) => {
79102
const result = await create(input);
80103
resolve(result);
81104
});
82105
},
83106
};
84-
85-
export default createChat;

api/resolver/mutation/createPost.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
GraphQLObjectType,
77
GraphQLUnionType,
88
} from "graphql";
9-
import { decode } from "@api/convertId";
10-
import { PostType, PostModel } from "@api/types/post";
9+
import fromGlobalId from "@api/fromGlobalId";
10+
import { PostModel, PostEdgeType, PostEdgeModel } from "@api/types/post";
1111
import prisma from "@database/lib";
1212
import ErrorType from "@api/types/error";
1313

@@ -17,14 +17,18 @@ interface CreatePostInput {
1717
content: string;
1818
}
1919

20-
type Result = PostModel | ErrorType;
20+
type Result =
21+
| {
22+
postEdges: PostEdgeModel;
23+
}
24+
| ErrorType;
2125

2226
const createPostInput = new GraphQLInputObjectType({
2327
name: "CreatePostInput",
2428
fields: {
2529
chat_id: { type: new GraphQLNonNull(GraphQLID) },
2630
user_id: { type: new GraphQLNonNull(GraphQLID) },
27-
title: { type: new GraphQLNonNull(GraphQLString) },
31+
content: { type: new GraphQLNonNull(GraphQLString) },
2832
},
2933
});
3034

@@ -33,8 +37,8 @@ async function create(data: CreatePostInput): Promise<Result> {
3337
const { id } = await prisma.post.create({
3438
data: {
3539
content: data.content,
36-
user_id: decode(data.user_id),
37-
chat_id: decode(data.chat_id),
40+
user_id: fromGlobalId(data.user_id),
41+
chat_id: fromGlobalId(data.chat_id),
3842
},
3943
});
4044
const post = await prisma.post.findUnique({
@@ -46,34 +50,48 @@ async function create(data: CreatePostInput): Promise<Result> {
4650
if (!post) {
4751
throw new Error("メッセージを投稿できませんでした");
4852
}
49-
return new PostModel(post);
53+
54+
return {
55+
postEdges: new PostEdgeModel({
56+
node: new PostModel(post),
57+
cursor: id,
58+
}),
59+
};
5060
} catch (e: any) {
5161
return {
5262
message: e.message,
5363
};
5464
}
5565
}
5666

57-
const PostCreatedErrorType = new GraphQLObjectType({
58-
name: "PostCreatedError",
59-
fields: {
60-
message: { type: new GraphQLNonNull(GraphQLString) },
61-
},
62-
});
63-
64-
const PostCreatedResultType = new GraphQLUnionType({
65-
name: "PostCreatedResult",
66-
types: [PostType, PostCreatedErrorType],
67-
resolveType: (value) => {
68-
if (value instanceof PostModel) {
69-
return "Post";
70-
}
71-
return "PostCreatedError";
72-
},
73-
});
74-
75-
const createPost = {
76-
type: new GraphQLNonNull(PostCreatedResultType),
67+
export default {
68+
type: new GraphQLNonNull(
69+
new GraphQLUnionType({
70+
name: "CreatePostResult",
71+
types: [
72+
new GraphQLObjectType({
73+
name: "PostEdges",
74+
fields: {
75+
postEdges: {
76+
type: PostEdgeType,
77+
},
78+
},
79+
}),
80+
new GraphQLObjectType({
81+
name: "CreatePostError",
82+
fields: {
83+
message: { type: new GraphQLNonNull(GraphQLString) },
84+
},
85+
}),
86+
],
87+
resolveType: (value) => {
88+
if (value.postEdges instanceof PostEdgeModel) {
89+
return "PostEdges";
90+
}
91+
return "CreatePostError";
92+
},
93+
})
94+
),
7795
args: {
7896
input: { type: new GraphQLNonNull(createPostInput) },
7997
},
@@ -84,5 +102,3 @@ const createPost = {
84102
});
85103
},
86104
};
87-
88-
export default createPost;

0 commit comments

Comments
 (0)