-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add updateQuery to useQuery (#1552)
- Loading branch information
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
packages/test-e2e-composable-vue3/src/components/UpdateQuery.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<script lang="ts" setup> | ||
import { useMutation, useQuery } from '@vue/apollo-composable' | ||
import gql from 'graphql-tag' | ||
import { computed, ref } from 'vue' | ||
import MessageItem from './MessageItem.vue' | ||
interface Channel { | ||
id: string | ||
label: string | ||
} | ||
const channelsQuery = useQuery<{ channels: Channel[] }>(gql` | ||
query channels { | ||
channels { | ||
id | ||
label | ||
} | ||
} | ||
`) | ||
const channels = computed(() => channelsQuery.result.value?.channels ?? []) | ||
const selectedChannelId = ref<string | null>(null) | ||
const selectedChannelQuery = useQuery(gql` | ||
query channel ($id: ID!) { | ||
channel (id: $id) { | ||
id | ||
label | ||
messages { | ||
id | ||
text | ||
} | ||
} | ||
} | ||
`, () => ({ | ||
id: selectedChannelId.value, | ||
}), () => ({ | ||
enabled: !!selectedChannelId.value, | ||
})) | ||
const addMessageMutation = useMutation(gql` | ||
mutation sendMessage ($input: AddMessageInput!) { | ||
message: addMessage (input: $input) { | ||
id | ||
text | ||
} | ||
} | ||
`) | ||
const selectedChannel = computed(() => selectedChannelQuery.result.value?.channel) | ||
const newMessageText = ref('') | ||
async function sendMessage () { | ||
if (!newMessageText.value.length) return | ||
const result = await addMessageMutation.mutate({ | ||
input: { | ||
channelId: selectedChannelId.value, | ||
text: newMessageText.value, | ||
}, | ||
}) | ||
newMessageText.value = '' | ||
selectedChannelQuery.updateQuery(previousResult => ({ | ||
...previousResult, | ||
channel: { | ||
...previousResult.channel, | ||
messages: [ | ||
...previousResult.channel.messages, | ||
result?.data.message, | ||
], | ||
}, | ||
})) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="h-full flex flex-col"> | ||
<div class="flex h-full"> | ||
<div class="flex flex-col"> | ||
<button | ||
v-for="channel of channels" | ||
:key="channel.id" | ||
class="channel-btn p-4" | ||
:class="{ | ||
'bg-green-200': selectedChannelId === channel.id, | ||
}" | ||
@click="selectedChannelId = channel.id" | ||
> | ||
{{ channel.label }} | ||
</button> | ||
</div> | ||
|
||
<div | ||
v-if="selectedChannel" | ||
class="the-channel flex flex-col w-full h-full overflow-auto" | ||
> | ||
<div class="flex-none p-6 border-b border-gray-200 bg-white"> | ||
# {{ selectedChannel.label }} | ||
</div> | ||
|
||
<div class="flex-1 overflow-y-auto"> | ||
<MessageItem | ||
v-for="message of selectedChannel.messages" | ||
:key="message.id" | ||
:message="message" | ||
class="m-2" | ||
/> | ||
</div> | ||
|
||
<div class="flex gap-x-2 p-4"> | ||
<div class="border border-gray-200 rounded-lg bg-white flex-1"> | ||
<input | ||
v-model="newMessageText" | ||
type="text" | ||
class="message-input w-full h-full bg-transparent px-3" | ||
placeholder="Type a message..." | ||
> | ||
</div> | ||
|
||
<button | ||
class="send-message-btn bg-green-200 py-3 px-4 rounded-lg" | ||
@click="sendMessage" | ||
> | ||
Send | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<div | ||
v-else | ||
class="no-data" | ||
> | ||
No data | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/test-e2e-composable-vue3/tests/e2e/specs/updateQuery.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
describe('updateQuery', () => { | ||
beforeEach(() => { | ||
cy.task('db:reset') | ||
cy.visit('/update-query') | ||
}) | ||
|
||
it('should add new message to cache using updateQuery', () => { | ||
cy.get('.channel-btn').eq(0).click() | ||
cy.get('.message-input').type('hello 1') | ||
cy.get('.message').should('have.lengthOf', 0) | ||
cy.get('.send-message-btn').click() | ||
cy.get('.message').should('have.lengthOf', 1) | ||
cy.get('.message-input').type('hello 2') | ||
cy.get('.send-message-btn').click() | ||
cy.get('.message').should('have.lengthOf', 2) | ||
cy.contains('.message', 'hello 1') | ||
cy.contains('.message', 'hello 2') | ||
|
||
cy.get('.channel-btn').eq(1).click() | ||
cy.get('.message-input').type('hello 3') | ||
cy.get('.message').should('have.lengthOf', 0) | ||
cy.get('.send-message-btn').click() | ||
cy.get('.message').should('have.lengthOf', 1) | ||
cy.get('.message-input').type('hello 4') | ||
cy.get('.send-message-btn').click() | ||
cy.get('.message').should('have.lengthOf', 2) | ||
cy.contains('.message', 'hello 3') | ||
cy.contains('.message', 'hello 4') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters