@dreamonkey/graphql-codegen-vue-apollo-plugin
GraphQL Code Generator plugin for Vue Apollo on steroids 🚀
Installation
Make sure you have installed and configured GraphQL Code Generator and Vue Apollo first:
Install the package:
pnpm add -D @dreamonkey/graphql-codegen-vue-apollo-plugin
Add the plugin to your codegen config:
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
generates: {
'src/generated/graphql.ts': {
plugins: [
'typed-document-node',
'typescript-operations',
'@dreamonkey/graphql-codegen-vue-apollo-plugin',
],
},
},
};
export default config;
Usage
Define the GraphQL document (gql
template string, .graphql
file, etc.):
fragment PostDetails on Post {
id
title
body
}
query getPosts {
posts {
...PostDetails
}
}
mutation createPost($input: PostInput!) {
posts {
...PostDetails
}
}
Import and use the generated composables and types:
import {
GetPostsDocument,
useGetPostsQuery,
useCreatePostMutation,
} from './path/to/generated/graphql';
const { posts, loading } = useGetPostsQuery();
const { createPost, loading: creatingPost } = useCreatePostMutation();
async function create() {
const { data: newPost, cache } = await createPost({
input: { title: 'Title', body: 'Body' },
});
cache.updateQuery(
{
query: GetPostsDocument,
},
(data) => {
const posts = data?.posts;
if (!posts) {
return;
}
return {
posts: [...posts, newPost],
};
},
);
}
Usage Comparison with @graphql-codegen/typescript-vue-apollo
Queries
@graphql-codegen/typescript-vue-apollo
:
import { computed } from 'vue';
import { useGetPostsQuery } from './path/to/generated/graphql';
const { result } = useGetPostsQuery();
const posts = computed(() => result.value?.posts ?? []);
@dreamonkey/graphql-codegen-vue-apollo-plugin
:
import { useGetPostsQuery } from './path/to/generated/graphql';
const { posts } = useGetPostsQuery();
Mutations
@graphql-codegen/typescript-vue-apollo
:
import { ApolloError } from '@apollo/client/errors';
import { useApolloClient } from '@vue/apollo-composable';
import { useCreatePostMutation } from './path/to/generated/graphql';
const { resolveClient } = useApolloClient();
const { mutate: createPost } = useCreatePostMutation({});
async function create() {
try {
const result = await createPost({
input: { title: 'Title', body: 'Body' },
});
const newPost = result?.data?.createPost;
if (!newPost || (result.errors && result.errors.length > 0)) {
return;
}
const { cache } = resolveClient();
cache.updateQuery();
} catch (_error) {
const error = _error as ApolloError;
console.error(error);
}
}
@dreamonkey/graphql-codegen-vue-apollo-plugin
:
import { ApolloError } from '@apollo/client/errors';
import { useCreatePostMutation } from './path/to/generated/graphql';
const { createPost } = useCreatePostMutation();
async function create() {
try {
const { data: newPost, cache } = await createPost({
input: { title: 'Title', body: 'Body' },
});
cache.updateQuery();
} catch (_error) {
const error = _error as ApolloError;
console.error(error);
}
}
Donate
If you appreciate the work that went into this package, please consider donating.