-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useQuery): nullable query (auto disable)
- Loading branch information
Showing
4 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/test-e2e-composable-vue3/src/components/NullQuery.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,69 @@ | ||
<script lang="ts"> | ||
import gql from 'graphql-tag' | ||
import { useQuery } from '@vue/apollo-composable' | ||
import { defineComponent, computed, ref } from 'vue' | ||
export default defineComponent({ | ||
setup () { | ||
const selectedId = ref<{ id: string } | null>(null) | ||
const nullableQuery = () => selectedId.value ? gql` | ||
query channel ($id: ID!) { | ||
channel (id: $id) { | ||
id | ||
label | ||
messages { | ||
id | ||
} | ||
} | ||
} | ||
` : null | ||
const { result, loading } = useQuery(nullableQuery, () => ({ | ||
// Should not throw since it will not be called if the query is disabled | ||
id: selectedId.value!.id, | ||
}), () => ({ | ||
fetchPolicy: 'no-cache', | ||
})) | ||
const channel = computed(() => result.value?.channel) | ||
function load () { | ||
selectedId.value = { id: 'general' } | ||
} | ||
return { | ||
load, | ||
loading, | ||
channel, | ||
} | ||
}, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="m-6"> | ||
<div> | ||
<button | ||
class="bg-green-200 rounded-lg p-4" | ||
@click="load()" | ||
> | ||
Load channel | ||
</button> | ||
</div> | ||
|
||
<div | ||
v-if="loading" | ||
class="loading" | ||
> | ||
Loading... | ||
</div> | ||
|
||
<div | ||
v-if="channel" | ||
data-test-id="data" | ||
> | ||
<div>Loaded channel: {{ channel.label }}</div> | ||
<div>Messages: {{ channel.messages.length }}</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
17 changes: 17 additions & 0 deletions
17
packages/test-e2e-composable-vue3/tests/e2e/specs/nullableQuery.cy.js
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,17 @@ | ||
/// <reference types="Cypress" /> | ||
|
||
describe('nullableQuery', () => { | ||
beforeEach(() => { | ||
cy.task('db:reset') | ||
cy.visit('/null-query') | ||
}) | ||
|
||
it('should enable useQuery only if query is non-null', () => { | ||
cy.get('button').should('exist') | ||
cy.wait(100) | ||
cy.get('[data-test-id="data"]').should('not.exist') | ||
cy.get('.loading').should('not.exist') | ||
cy.get('button').click() | ||
cy.get('[data-test-id="data"]').should('contain', 'Loaded channel: General') | ||
}) | ||
}) |
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