Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dataIdFromObject: object => {
switch (object.__typename) {
case 'TaskStatus': {
const taskId = object.taskId || null;
return taskId
? `${object.taskId}-${object.__typename}`
: defaultDataIdFromObject(object);
}
default: {
// fall back to default handling
return defaultDataIdFromObject(object);
}
}
},
/* eslint-enable no-underscore-dangle */
dataIdFromObject: (obj: any) => {
// We need to set manually shop's ID, since it is singleton and
// API does not return its ID
if (obj.__typename === "Shop") {
return "shop";
}
return defaultDataIdFromObject(obj);
}
}),
it('updates group by id with provided function', () => {
const UPDATED_GROUP = {};
const fn = jest.fn().mockReturnValue(UPDATED_GROUP);
client.readFragment.mockReturnValue(getFakeGroup());
manager.updateById(getFakeGroup().id, fn);
expect(client.readFragment).toHaveBeenCalledWith({
fragment: ImportSourceGroupFragment,
id: defaultDataIdFromObject(getFakeGroup()),
});
expect(client.writeFragment).toHaveBeenCalledWith({
fragment: ImportSourceGroupFragment,
id: defaultDataIdFromObject(getFakeGroup()),
data: UPDATED_GROUP,
});
});
update: (client, { data: { addMessage } }) => {
client.writeFragment({
id: defaultDataIdFromObject(addMessage),
fragment: fragments.message,
data: addMessage,
})
let fullChat
try {
fullChat = client.readFragment({
id: defaultDataIdFromObject(addMessage.chat),
fragment: fragments.fullChat,
fragmentName: 'FullChat',
})
} catch (e) {}
if (fullChat && !fullChat.messages.some(message => message.id === addMessage.id)) {
fullChat.messages.push(addMessage)
fullChat.lastMessage = addMessage
let fullChat
try {
fullChat = client.readFragment({
id: defaultDataIdFromObject(messageAdded.chat),
fragment: fragments.fullChat,
fragmentName: 'FullChat',
})
} catch (e) {}
if (fullChat && !fullChat.messages.some(message => message.id === messageAdded.id)) {
fullChat.messages.push(messageAdded)
fullChat.lastMessage = messageAdded
client.writeFragment({
id: defaultDataIdFromObject(fullChat),
fragment: fragments.fullChat,
fragmentName: 'FullChat',
data: fullChat,
})
}
let chats
try {
chats = client.readQuery({
query: queries.chats,
}).chats
} catch (e) {}
if (chats) {
const index = chats.findIndex(chat => chat.id === messageAdded.chat.id)
const chat = chats[index]
export const writeChat = (client: Client, chat: ChatFragment) => {
const chatId = defaultDataIdFromObject(chat);
if (chatId === null) {
return;
}
client.writeFragment({
id: chatId,
fragment: fragments.chat,
fragmentName: 'Chat',
data: chat,
});
let data;
try {
data = client.readQuery({
query: queries.chats,
});
function dataIdFromObject(obj) {
switch (obj.__typename) {
case 'NewsItem':
return obj.link ? `NewsItem:${obj.link}` : defaultDataIdFromObject(obj);
default:
return defaultDataIdFromObject(obj);
}
}
update: store => store.data.delete(defaultDataIdFromObject(branch)),
});