To reuse types generated by graphql-codegen, typescript-operations, and typescript-vue-urql as fragments in Vue, you can follow these steps:
fragment
keyword:fragment UserFragment on User {
name
email
...
}
fragment-matcher
plugin:$ npx graphql-codegen --config codegen.yml
schema: http://localhost:3000/api/graphql
documents: path/to/*.graphql
plugins:
- typescript
- typescript-operations
- typescript-vue-urql
- fragment-matcher:
typePrefix: "FragmentTypes."
src: path/to/fragment-types.ts
import { UserFragmentFragment } from '@/path/to/fragment-types.ts';
<template>
<div>
<ul>
<li v-for="user in users" :key="user.id">
{{ user.name }} - {{ user.email }}
</li>
</ul>
</div>
</template>
<script lang="ts">
import { useQuery, gql } from '@vue/apollo-composable';
import { UserFragmentFragment } from '@/path/to/fragment-types.ts';
const USERS_QUERY = gql`
query {
users {
...UserFragment # <-- using the UserFragment as a fragment
}
}
${UserFragmentFragment}
`;
export default {
setup() {
const { result } = useQuery(USERS_QUERY);
return {
users: result.value?.data?.users || [],
};
},
};
</script>
<template>
<div>
<button @click="updateUser(user.id)">Update User</button>
</div>
</template>
<script lang="ts">
import { useMutation, gql } from '@vue/apollo-composable';
import { UserFragmentFragment } from '@/path/to/fragment-types.ts';
const UPDATE_USER_MUTATION = gql`
mutation($id: ID!, $data: UserInput!) {
updateUser(id: $id, data: $data) {
...UserFragment # <-- using the UserFragment as a fragment
}
}
${UserFragmentFragment}
`;
export default {
props: {
user: {
type: Object,
required: true,
},
},
setup(props) {
const { mutate } = useMutation(UPDATE_USER_MUTATION);
const updateUser = async (id: string) => {
const data = { /* ... */ };
await mutate({ id, data });
};
return {
updateUser,
};
},
};
</script>
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-19 15:29:45 +0000
Seen: 8 times
Last updated: May 19 '23
How can one ensure that sub-classes have uniform method parameters in TypeScript?
Can a TypeScript definition for icon names be provided in MaterialCommunityIcons for React Native?
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?