Skip to content

Commit

Permalink
Step 8.1: Add User type
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed May 20, 2020
1 parent 67b79a8 commit 2a734ae
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
1 change: 1 addition & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ generates:
mappers:
# import { Message } from '../db'
# The root types of Message resolvers
User: ../db#User
Message: ../db#Message
Chat: ../db#Chat
scalars:
Expand Down
64 changes: 54 additions & 10 deletions db.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
export type User = {
id: string;
name: string;
picture: string;
};

export type Message = {
id: string;
content: string;
createdAt: Date;
sender: string;
recipient: string;
};

export type Chat = {
id: string;
name: string;
picture: string;
messages: string[];
participants: string[];
};

export const users: User[] = [];
export const messages: Message[] = [];
export const chats: Chat[] = [];

export const resetDb = () => {
users.splice(
0,
Infinity,
...[
{
id: '1',
name: 'Ray Edwards',
picture: 'https://randomuser.me/api/portraits/thumb/lego/1.jpg',
},
{
id: '2',
name: 'Ethan Gonzalez',
picture: 'https://randomuser.me/api/portraits/thumb/men/1.jpg',
},
{
id: '3',
name: 'Bryan Wallace',
picture: 'https://randomuser.me/api/portraits/thumb/men/2.jpg',
},
{
id: '4',
name: 'Avery Stewart',
picture: 'https://randomuser.me/api/portraits/thumb/women/1.jpg',
},
{
id: '5',
name: 'Katie Peterson',
picture: 'https://randomuser.me/api/portraits/thumb/women/2.jpg',
},
]
);

messages.splice(
0,
Infinity,
Expand All @@ -23,27 +63,35 @@ export const resetDb = () => {
id: '1',
content: 'You on your way?',
createdAt: new Date(new Date('1-1-2019').getTime() - 60 * 1000 * 1000),
sender: '1',
recipient: '2',
},
{
id: '2',
content: "Hey, it's me",
createdAt: new Date(
new Date('1-1-2019').getTime() - 2 * 60 * 1000 * 1000
),
sender: '1',
recipient: '3',
},
{
id: '3',
content: 'I should buy a boat',
createdAt: new Date(
new Date('1-1-2019').getTime() - 24 * 60 * 1000 * 1000
),
sender: '1',
recipient: '4',
},
{
id: '4',
content: 'This is wicked good ice cream.',
createdAt: new Date(
new Date('1-1-2019').getTime() - 14 * 24 * 60 * 1000 * 1000
),
sender: '1',
recipient: '5',
},
]
);
Expand All @@ -54,26 +102,22 @@ export const resetDb = () => {
...[
{
id: '1',
name: 'Ethan Gonzalez',
picture: 'https://randomuser.me/api/portraits/thumb/men/1.jpg',
participants: ['1', '2'],
messages: ['1'],
},
{
id: '2',
name: 'Bryan Wallace',
picture: 'https://randomuser.me/api/portraits/thumb/men/2.jpg',
participants: ['1', '3'],
messages: ['2'],
},
{
id: '3',
name: 'Avery Stewart',
picture: 'https://randomuser.me/api/portraits/thumb/women/1.jpg',
participants: ['1', '4'],
messages: ['3'],
},
{
id: '4',
name: 'Katie Peterson',
picture: 'https://randomuser.me/api/portraits/thumb/women/2.jpg',
participants: ['1', '5'],
messages: ['4'],
},
]
Expand Down
28 changes: 27 additions & 1 deletion schema/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DateTimeResolver, URLResolver } from 'graphql-scalars';
import { Message, chats, messages } from '../db';
import { User, Message, chats, messages, users } from '../db';
import { Resolvers } from '../types/graphql';

const resolvers: Resolvers = {
Expand All @@ -10,9 +10,27 @@ const resolvers: Resolvers = {
chat(message) {
return chats.find(c => c.messages.some(m => m === message.id)) || null;
},

sender(message) {
return users.find(u => u.id === message.sender) || null;
},

recipient(message) {
return users.find(u => u.id === message.recipient) || null;
},
},

Chat: {
name() {
// TODO: Resolve in relation to current user
return null;
},

picture() {
// TODO: Resolve in relation to current user
return null;
},

messages(chat) {
return messages.filter((m) => chat.messages.includes(m.id));
},
Expand All @@ -22,6 +40,12 @@ const resolvers: Resolvers = {

return messages.find((m) => m.id === lastMessage) || null;
},

participants(chat) {
return chat.participants
.map(p => users.find(u => u.id === p))
.filter(Boolean) as User[];
},
},

Query: {
Expand Down Expand Up @@ -49,6 +73,8 @@ const resolvers: Resolvers = {
const message: Message = {
id: messageId,
createdAt: new Date(),
sender: '', // TODO: Fill-in
recipient: '', // TODO: Fill-in
content,
};

Expand Down
12 changes: 11 additions & 1 deletion schema/typeDefs.graphql
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
scalar Date
scalar URL

type User {
id: ID!
name: String!
picture: URL
}

type Message {
id: ID!
content: String!
createdAt: Date!
chat: Chat
sender: User
recipient: User
isMine: Boolean!
}

type Chat {
id: ID!
name: String!
name: String
picture: URL
lastMessage: Message
messages: [Message!]!
participants: [User!]!
}

type Query {
Expand Down

0 comments on commit 2a734ae

Please sign in to comment.