Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr committed Jan 14, 2018
1 parent 940903a commit 3eb3db8
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 148 deletions.
33 changes: 33 additions & 0 deletions introspection-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fetch = require('node-fetch');
const fs = require('fs');

fetch(`http://localhost:3001/api`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}
`,
}),
})
.then(result => result.json())
.then(result => {
fs.writeFile(
'./fragmentTypes.json',
JSON.stringify(result.data, null, 2),
err => {
if (err) console.error('Error writing fragmentTypes file', err);
console.log('Fragment types successfully extracted!');
}
);
});
2 changes: 1 addition & 1 deletion src/api/fragments/thread/threadMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { messageInfoFragment } from '../message/messageInfo';

export const threadMessagesFragment = gql`
fragment threadMessages on Thread {
messageConnection(after: $after, first: $first, before: $before, last: $last) {
messageConnection(after: $after, first: $first, before: $before, last: $last) @connection(key: "messageConnection") {
pageInfo {
hasNextPage
hasPreviousPage
Expand Down
217 changes: 104 additions & 113 deletions src/api/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,18 @@ const SEND_MESSAGE_MUTATION = gql`
mutation sendMessage($message: MessageInput!) {
addMessage(message: $message) {
...messageInfo
thread {
id
receiveNotifications
messageCount
creator {
...userInfo
contextPermissions {
communityId
reputation
isOwner
isModerator
}
}
participants {
...userInfo
}
isLocked
content {
title
body
}
}
}
}
${messageInfoFragment}
${userInfoFragment}
`;
const SEND_MESSAGE_OPTIONS = {
props: ({ ownProps, mutate }) => ({
sendMessage: message =>
mutate({
sendMessage: message => {
console.log('[sendMessage] new message:', message);
const fakeId = Math.round(Math.random() * -1000000);
console.log('[sendMessage] fake ID:', fakeId);
console.log('[sendMessage] calling mutate');
return mutate({
variables: {
message: {
...message,
Expand All @@ -123,104 +104,114 @@ const SEND_MESSAGE_OPTIONS = {
optimisticResponse: {
__typename: 'Mutation',
addMessage: {
__typename: 'Message',
thread: {
...ownProps.threadData,
__typename: 'Thread',
},
sender: {
...ownProps.currentUser,
contextPermissions: {
communityId: ownProps.threadData.community.id,
reputation: 0,
isOwner: false,
isModerator: false,
__typename: 'ContextPermissions',
__typename: 'ThreadMessageEdge',
cursor: window.btoa(fakeId),
node: {
__typename: 'Message',
sender: {
...ownProps.currentUser,
contextPermissions: {
communityId: ownProps.threadData.community.id,
reputation: 0,
isOwner: false,
isModerator: false,
__typename: 'ContextPermissions',
},
__typename: 'User',
},
__typename: 'User',
},
timestamp: +new Date(),
content: {
...message.content,
__typename: 'MessageContent',
},
id: Math.round(Math.random() * -1000000),
reactions: {
count: 0,
hasReacted: false,
__typename: 'ReactionData',
timestamp: +new Date(),
content: {
...message.content,
__typename: 'MessageContent',
},
id: fakeId,
reactions: {
count: 0,
hasReacted: false,
__typename: 'ReactionData',
},
messageType: message.messageType,
},
messageType: message.messageType,
},
},
update: (store, { data: { addMessage } }) => {
// we have to split out the optimistic update by thread type
// because DMs and story threads have different queries and response
// shapes
if (ownProps.threadType === 'story') {
// Read the data from our cache for this query.
const data = store.readQuery({
query: GET_THREAD_MESSAGES_QUERY,
variables: {
id: ownProps.thread,
},
});
update: (store, { data: { addMessage }, data: object }) => {
if (typeof addMessage.id === 'string') {
console.log('[update] New message from server, ignoring');
console.log(object);
return;
} else {
console.log('[update] New optimistic response for message');
console.log(object);
}
console.log(addMessage);
// // we have to split out the optimistic update by thread type
// // because DMs and story threads have different queries and response
// // shapes
// if (ownProps.threadType === 'story') {
console.log(
`[update] readQuery for thread messages of thread#${ownProps.thread}`
);
// Read the data from our cache for this query.
const data = store.readQuery({
query: GET_THREAD_MESSAGES_QUERY,
variables: {
id: ownProps.thread,
},
});

// ignore the addMessage from the server, apollo will automatically
// override the optimistic object
if (
!addMessage ||
(typeof addMessage.id === 'string' &&
addMessage.messageType === 'text')
) {
return;
}
console.log(
`[update] data for thread message query for thread#${ownProps.thread}:`,
data
);

data.thread.messageConnection.edges.push({
cursor: addMessage.id,
node: addMessage,
__typename: 'ThreadMessageEdge',
});
console.log('[update] add message to messageConnection');
data.thread.messageConnection.edges.push(addMessage);
console.log('[update] message added:', data.thread.messageConnection);

// Write our data back to the cache.
store.writeQuery({
query: GET_THREAD_MESSAGES_QUERY,
data,
variables: {
id: ownProps.thread,
},
});
} else if (ownProps.threadType === 'directMessageThread') {
// Read the data from our cache for this query.
const data = store.readQuery({
query: GET_DIRECT_MESSAGE_THREAD_QUERY,
variables: {
id: ownProps.thread,
},
});
console.log(
`[update] writeQuery with message added for thread#${ownProps.thread}`
);
// Write our data back to the cache.
store.writeQuery({
query: GET_THREAD_MESSAGES_QUERY,
data,
variables: {
id: ownProps.thread,
},
});
console.log('[update] message written to store');
// } else if (ownProps.threadType === 'directMessageThread') {
// // Read the data from our cache for this query.
// const data = store.readQuery({
// query: GET_DIRECT_MESSAGE_THREAD_QUERY,
// variables: {
// id: ownProps.thread,
// },
// });

// ignore the addMessage from the server, apollo will automatically
// override the optimistic object
if (!addMessage || typeof addMessage.id === 'string') {
return;
}
// // ignore the addMessage from the server, apollo will automatically
// // override the optimistic object
// if (!addMessage || typeof addMessage.id === 'string') {
// return;
// }

data.directMessageThread.messageConnection.edges.push({
cursor: addMessage.id,
node: addMessage,
__typename: 'DirectMessageEdge',
});
// Write our data back to the cache.
store.writeQuery({
query: GET_DIRECT_MESSAGE_THREAD_QUERY,
data,
variables: {
id: ownProps.thread,
},
});
}
// data.directMessageThread.messageConnection.edges.push({
// cursor: addMessage.id,
// node: addMessage,
// __typename: 'DirectMessageEdge',
// });
// // Write our data back to the cache.
// store.writeQuery({
// query: GET_DIRECT_MESSAGE_THREAD_QUERY,
// data,
// variables: {
// id: ownProps.thread,
// },
// });
// }
},
}),
});
},
}),
};
export const sendMessageMutation = graphql(
Expand Down
Loading

0 comments on commit 3eb3db8

Please sign in to comment.