Created
January 3, 2023 15:29
-
-
Save hipertracker/0682f2c88774cc5274c18f36db2e0a64 to your computer and use it in GitHub Desktop.
Villus for Vue >=2.7,<3.0 example
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
<script setup> | |
import { useClient } from "villus"; | |
import {graphqlOptions} from "@/grapql/client" | |
//... | |
useClient(graphqlOptions); | |
</script> |
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
import { defaultPlugins, handleSubscriptions } from "villus"; | |
const subscriptionClient = new SubscriptionClient(process.env.VUE_APP_HASURA_WEBSOCKET, {}); | |
const subscriptionForwarder = (operation) => subscriptionClient.request(operation); | |
export const graphqlOptions = { | |
url: process.env.VUE_APP_HASURA_URL, | |
use: [handleSubscriptions(subscriptionForwarder), ...defaultPlugins()], | |
cachePolicy: "network-only", | |
headers: { | |
"x-hasura-role": "reader", | |
}, | |
}; |
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
<script setup> | |
import { useSubscription } from "villus"; | |
import {myPiniaStore} from '@/stores/' | |
import MY_SUBSCRIPTION from "@/graphql/subscriptions/ | |
const { data, error } = useSubscription({ | |
query: MY_SUBSCRIPTION, | |
variables: { id: 1 } | |
}); | |
watchEffect(() => { | |
if (error.value) { | |
console.error(error.value); | |
} else { | |
if (data.value?.items) { | |
myPiniaStore.setItems(data.value.items); | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment