Skip to content

Commit

Permalink
Step 8.2: Resolve queries in relation to current user
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed Jul 20, 2019
1 parent e898fbf commit 7da5e2e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
2 changes: 2 additions & 0 deletions context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PubSub } from 'apollo-server-express';
import { User } from './db';

export type MyContext = {
pubsub: PubSub;
currentUser: User;
};
6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import http from 'http';
import { users } from './db';
import schema from './schema';

const app = express();
Expand All @@ -17,7 +18,10 @@ app.get('/_ping', (req, res) => {
const pubsub = new PubSub();
const server = new ApolloServer({
schema,
context: () => ({ pubsub }),
context: () => ({
currentUser: users.find(u => u.id === '1'),
pubsub,
}),
});

server.applyMiddleware({
Expand Down
57 changes: 44 additions & 13 deletions schema/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,35 @@ const resolvers: Resolvers = {
recipient(message) {
return users.find(u => u.id === message.recipient) || null;
},

isMine(message, args, { currentUser }) {
return message.sender === currentUser.id;
},
},

Chat: {
name() {
// TODO: Resolve in relation to current user
return null;
name(chat, args, { currentUser }) {
if (!currentUser) return null;

const participantId = chat.participants.find(p => p !== currentUser.id);

if (!participantId) return null;

const participant = users.find(u => u.id === participantId);

return participant ? participant.name : null;
},

picture() {
// TODO: Resolve in relation to current user
return null;
picture(chat, args, { currentUser }) {
if (!currentUser) return null;

const participantId = chat.participants.find(p => p !== currentUser.id);

if (!participantId) return null;

const participant = users.find(u => u.id === participantId);

return participant ? participant.picture : null;
},

messages(chat) {
Expand All @@ -48,29 +66,42 @@ const resolvers: Resolvers = {
},

Query: {
chats() {
return chats;
chats(root, args, { currentUser }) {
if (!currentUser) return [];

return chats.filter(c => c.participants.includes(currentUser.id));
},

chat(root, { chatId }) {
return chats.find(c => c.id === chatId) || null;
chat(root, { chatId }, { currentUser }) {
if (!currentUser) return null;

const chat = chats.find(c => c.id === chatId);

if (!chat) return null;

return chat.participants.includes(currentUser.id) ? chat : null;
},
},

Mutation: {
addMessage(root, { chatId, content }, { pubsub }) {
addMessage(root, { chatId, content }, { currentUser, pubsub }) {
if (!currentUser) return null;

const chatIndex = chats.findIndex(c => c.id === chatId);

if (chatIndex === -1) return null;

const chat = chats[chatIndex];

if (!chat.participants.includes(currentUser.id)) return null;

const recentMessage = messages[messages.length - 1];
const messageId = String(Number(recentMessage.id) + 1);
const message: Message = {
id: messageId,
createdAt: new Date(),
sender: '', // TODO: Fill-in
recipient: '', // TODO: Fill-in
sender: currentUser.id,
recipient: chat.participants.find(p => p !== currentUser.id) as string,
content,
};

Expand Down
7 changes: 5 additions & 2 deletions tests/mutations/addMessage.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { createTestClient } from 'apollo-server-testing';
import { ApolloServer, PubSub, gql } from 'apollo-server-express';
import schema from '../../schema';
import { resetDb } from '../../db';
import { resetDb, users } from '../../db';

describe('Mutation.addMessage', () => {
beforeEach(resetDb);

it('should add message to specified chat', async () => {
const server = new ApolloServer({
schema,
context: () => ({ pubsub: new PubSub() }),
context: () => ({
pubsub: new PubSub(),
currentUser: users[0],
}),
});

const { query, mutate } = createTestClient(server);
Expand Down
8 changes: 7 additions & 1 deletion tests/queries/getChat.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createTestClient } from 'apollo-server-testing';
import { ApolloServer, gql } from 'apollo-server-express';
import schema from '../../schema';
import { users } from '../../db';

describe('Query.chat', () => {
it('should fetch specified chat', async () => {
const server = new ApolloServer({ schema });
const server = new ApolloServer({
schema,
context: () => ({
currentUser: users[0],
}),
});

const { query } = createTestClient(server);

Expand Down
8 changes: 7 additions & 1 deletion tests/queries/getChats.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createTestClient } from 'apollo-server-testing';
import { ApolloServer, gql } from 'apollo-server-express';
import schema from '../../schema';
import { users } from '../../db';

describe('Query.chats', () => {
it('should fetch all chats', async () => {
const server = new ApolloServer({ schema });
const server = new ApolloServer({
schema,
context: () => ({
currentUser: users[0],
}),
});

const { query } = createTestClient(server);

Expand Down

0 comments on commit 7da5e2e

Please sign in to comment.