|
| 1 | +# @vue3-apollo/core |
| 2 | + |
| 3 | +> π§© Composable utilities for using [Apollo Client v4](https://www.apollographql.com/docs/react/) with [Vue 3](https://vuejs.org/). |
| 4 | +
|
| 5 | +Vue3 Apollo Core provides a **lightweight, composable-first integration** between Vue 3 and Apollo Client, allowing you to perform GraphQL queries, mutations, and subscriptions in a fully reactive way. |
| 6 | + |
| 7 | +## β¨ Features |
| 8 | + |
| 9 | +- πͺΆ Minimal and tree-shakeable |
| 10 | +- π Reactive GraphQL queries & mutations via Vue composables |
| 11 | +- β‘ Multiple client support (default + named clients) |
| 12 | +- π§ TypeScript-first API |
| 13 | +- π§© Seamless integration with Nuxt via [`@vue3-apollo/nuxt`](https://www.npmjs.com/package/@vue3-apollo/nuxt) |
| 14 | +- π Full documentation at [vue3-apollo.guen.dev](https://vue3-apollo.guen.dev/) |
| 15 | + |
| 16 | +## π¦ Installation |
| 17 | + |
| 18 | +You can install using your preferred package manager: |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install @vue3-apollo/core @apollo/client graphql |
| 22 | +``` |
| 23 | + |
| 24 | +## π Getting Started |
| 25 | + |
| 26 | +### 1. Create an Apollo Client |
| 27 | + |
| 28 | +```ts |
| 29 | +import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client/core' |
| 30 | + |
| 31 | +export const defaultClient = new ApolloClient({ |
| 32 | + cache: new InMemoryCache(), |
| 33 | + link: new HttpLink({ |
| 34 | + // Example public GraphQL API |
| 35 | + uri: 'https://graphqlplaceholder.vercel.app/graphql', |
| 36 | + }), |
| 37 | +}) |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Register the Plugin |
| 41 | + |
| 42 | +```ts |
| 43 | +import { createApp } from 'vue' |
| 44 | +import { apolloPlugin } from '@vue3-apollo/core' |
| 45 | +import { defaultClient, analyticsClient } from './apollo-clients' |
| 46 | + |
| 47 | +const app = createApp(App) |
| 48 | + |
| 49 | +app.use(apolloPlugin, { |
| 50 | + clients: { |
| 51 | + default: defaultClient, |
| 52 | + analytics: analyticsClient, |
| 53 | + }, |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +This injects Apollo clients into your Vue app, making them available across composables and components. |
| 58 | + |
| 59 | +### 3. Use in a Component |
| 60 | + |
| 61 | +```vue |
| 62 | +<script setup lang="ts"> |
| 63 | +import { useQuery } from '@vue3-apollo/core' |
| 64 | +import gql from 'graphql-tag' |
| 65 | +
|
| 66 | +const GET_POSTS = gql` |
| 67 | + query Posts { |
| 68 | + posts { |
| 69 | + id |
| 70 | + title |
| 71 | + body |
| 72 | + } |
| 73 | + } |
| 74 | +` |
| 75 | +
|
| 76 | +const { result, loading, error } = useQuery(GET_POSTS) |
| 77 | +</script> |
| 78 | +
|
| 79 | +<template> |
| 80 | + <div v-if="loading">Loading...</div> |
| 81 | + <div v-else-if="error">{{ error.message }}</div> |
| 82 | + <ul v-else> |
| 83 | + <li v-for="post in result.posts" :key="post.id"> |
| 84 | + <strong>{{ post.title }}</strong> β {{ post.body }} |
| 85 | + </li> |
| 86 | + </ul> |
| 87 | +</template> |
| 88 | +``` |
| 89 | + |
| 90 | +## π§ Composables Overview |
| 91 | + |
| 92 | +| Composable | Description | |
| 93 | +|-------------|--------------| |
| 94 | +| `useQuery` | Reactive GraphQL query | |
| 95 | +| `useLazyQuery` | Run query on demand | |
| 96 | +| `useMutation` | Execute GraphQL mutations | |
| 97 | +| `useSubscription` | Subscribe to GraphQL streams | |
| 98 | +| `useApolloClient` | Access current Apollo client | |
| 99 | +| `provideApolloClients` / `useApolloClients` | Manage multiple clients | |
| 100 | + |
| 101 | +See the [full API reference](https://vue3-apollo.guen.dev/core/composables/use-query) for details. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## π§© Multi-Client Example |
| 106 | + |
| 107 | +You can register and switch between multiple clients: |
| 108 | + |
| 109 | +```ts |
| 110 | +const { result: analyticsData } = useQuery(ANALYTICS_QUERY, null, { |
| 111 | + clientId: 'analytics', |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | +## π§° TypeScript Support |
| 116 | + |
| 117 | +All composables are fully typed, providing autocompletion and inference for query variables and responses. |
| 118 | + |
| 119 | +```ts |
| 120 | +const { result } = useQuery<{ posts: { id: string; title: string }[] }>(GET_POSTS) |
| 121 | +``` |
| 122 | + |
| 123 | +## π§βπ» Contributing |
| 124 | + |
| 125 | +This package is part of the [Vue3 Apollo monorepo](https://github.com/guendev/vue3-apollo). |
| 126 | + |
| 127 | +To develop locally: |
| 128 | + |
| 129 | +```bash |
| 130 | +pnpm install |
| 131 | +pnpm build |
| 132 | +pnpm dev:docs |
| 133 | +``` |
| 134 | + |
| 135 | +## π License |
| 136 | + |
| 137 | +[MIT](https://github.com/guendev/vue3-apollo/blob/main/LICENSE) |
| 138 | + |
| 139 | +## π Links |
| 140 | + |
| 141 | +- π [Documentation](https://vue3-apollo.guen.dev/) |
| 142 | +- πΎ [GitHub Repository](https://github.com/guendev/vue3-apollo) |
| 143 | +- π¦ [npm - @vue3-apollo/core](https://www.npmjs.com/package/@vue3-apollo/core) |
| 144 | +- π§± [Nuxt Integration - @vue3-apollo/nuxt](https://www.npmjs.com/package/@vue3-apollo/nuxt) |
0 commit comments