Error writing result to store for query Invariant Violation: 2
Собираю чат на React+Typescript+GraphQL+Mongo+Redis, купил готовый, все работает через Docker, когда запускаю локально development версию, сообщения в чат отправляются, а когда запускаю на сервере с измененным Nginx, привязкой к домену и сертификатами ssl - не отправляются.
Причем коннект по сокету идет, когда пишешь сообщение собеседник видит, работают видеозвонки.
И коннект к базе идет, так как когда создаешь чат в окне переписки выводятся сообщения типа "Developer create new chat" и "Developer invite User1", которые записываются в базу туда же, куда должны сохраняться пользовательские сообщения.
Но при отправке своего меседжа в консоли хрома следующая ошибка:
2.8495629a.chunk.js:1 Uncaught (in promise) Invariant Violation: Invariant Violation: 2 (see https://github.com/apollographql/invariant-packages)
at new t (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:95517)
at s (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:95776)
at e.match (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:633876)
at https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:642302
at Array.forEach (<anonymous>)
at e.writeSelectionSetToStore (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:641639)
at e.writeFieldToStore (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:642901)
at https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:641761
at Array.forEach (<anonymous>)
at e.writeSelectionSetToStore (https://mydomain.com/chat/static/js/2.8495629a.chunk.js:1:641639)
а в консоли мазилы такая:
Error: Error writing result to store for query:
{"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"SendMessage"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"MessageSendInput"}}},"directives":[]}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"messages"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"send"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"MessageFragment"},"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"MessageFragment"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Message"}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"_id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"chat_id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"recipient_id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"sender_id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"date"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"text"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"isService"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"sender"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"UserFragment"},"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"attachment"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"photo"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"PictureFragment"},"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"UserFragment"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"User"}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"_id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"lastName"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"firstName"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"username"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"picture"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"PictureFragment"},"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"PictureFragment"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Picture"}},"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"url"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"width"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"height"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"preview"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}],"loc":{"start":0,"end":659}}
Invariant Violation: 2
Вот функции которые отвечают за отправку, первая которая работает исправно и сохраняет системные сообщения вида "Developer create new chat":
async createChat(_, { name, picture }, { user_id }) {
const newChat = new ChatModel({ name, picture });
const chat = await newChat.save();
const newChatMember = new ChatMemberModel({
chat_id: chat._id,
member_id: user_id,
recipient_id: chat._id,
type: ChatType.GROUP_CHAT
});
await newChatMember.save();
const newMessage = new MessageModel({
chat_id: chat._id,
sender_id: user_id,
recipient_id: chat._id,
isService: true,
text: 'create new chat'
});
const message = await newMessage.save();
const user = await UserModel.findOne({ _id: user_id }).lean();
const memberCount = await ChatMemberModel.find({ chat_id: chat._id }).countDocuments();
return {
_id: chat._id,
type: ChatType.GROUP_CHAT,
unreadCount: 0,
member: null,
info: {
_id: chat._id,
name: chat.name!,
memberCount,
picture: null
},
lastMessage: {
_id: message._id,
chat_id: message.chat_id,
recipient_id: message.recipient_id,
sender_id: message.sender_id,
date: message.date,
attachment: prepareAttachment(message.attachment),
sender: user,
text: message.text,
isService: message.isService
}
};
}
и вторая, которая отвечает за пользовательские сообщения и выдает ошибки:
export const sendMessage = async ({ input, socket, user_id, isService }: sendMessageAgrs) => {
const { recipient_id, text, attachment } = input;
let chat: ChatDocument | null = null;
let chatMember = await ChatMemberModel.findOne(
{ member_id: user_id, recipient_id },
);
if (!chatMember) {
const recipient = await UserModel.findOne({ _id: recipient_id }, { _id: 1 });
if (!recipient) {
throw new Error('Recipient does not exist');
}
chat = await new ChatModel().save();
if (recipient_id !== user_id!.toString()) {
// For recipient
await new ChatMemberModel({
type: ChatType.CONVERSATION,
chat_id: chat._id,
member_id: recipient_id,
recipient_id: user_id,
unreadCount: 1
}).save();
}
// For me
chatMember = await new ChatMemberModel({
type: ChatType.CONVERSATION,
chat_id: chat._id,
member_id: user_id,
recipient_id
}).save();
} else {
chat = await ChatModel.findOne({ _id: chatMember.chat_id });
// Update recipient unread count only for CONVERSATION
if (chatMember.type === ChatType.CONVERSATION && recipient_id !== user_id!.toString()) {
await ChatMemberModel.updateOne(
{ member_id: recipient_id, recipient_id: user_id },
{ $inc: { unreadCount: 1 } }
);
}
}
const newMessage = new MessageModel({
...(attachment && { attachment }),
...(isService && { isService: true }),
chat_id: chatMember.chat_id,
sender_id: user_id,
recipient_id,
text
});
const messageDoc = await newMessage.save();
// Fort send notifications
const senderDoc = await UserModel.findOne(
{ _id: user_id },
{ firstName: 1, lastName: 1, username: 1, picture: 1 }
).lean();
const sender: User = {
__typename: 'User',
_id: senderDoc._id,
firstName: senderDoc.firstName,
lastName: senderDoc.lastName,
username: senderDoc.username,
picture: preparePictrue(senderDoc.picture)
};
const message: Message = {
__typename: 'Message',
_id: messageDoc._id,
chat_id: messageDoc.chat_id,
recipient_id: messageDoc.recipient_id,
sender_id: messageDoc.sender_id,
date: messageDoc.date,
attachment: prepareAttachment(messageDoc.attachment),
sender,
text: messageDoc.text,
isService: messageDoc.isService
};
Мучаюсь уже неделю, подскажите что не так? куда копать?
Заранее благодарю