Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using vue-apollo-composable inside a Pinia setup store #1527

Open
bipsen opened this issue Jan 11, 2024 · 2 comments
Open

Using vue-apollo-composable inside a Pinia setup store #1527

bipsen opened this issue Jan 11, 2024 · 2 comments

Comments

@bipsen
Copy link

bipsen commented Jan 11, 2024

I expect to be able to make queries inside a pinia setup store as per #1505.

When I run the code below, however, result stays undefined. I can see from the watchEffect log that the query runs and gets the correct response, so why doesn't result seem to be reactive in my component?

// stores/campers.ts
import SOME_QUERY from "@/gql/queries/SOME_QUERY.gql";

export const useCamperStore = defineStore("campers", () => {
  const { data: authData } = useAuth();

  const camperQueryVariables = computed(() => {
    return {
      id: authData.value?.id,
    };
  });

  const { result, refetch } = useQuery(SOME_QUERY, camperQueryVariables);

  watchEffect(() => console.log(result.value));

  return {
    result,
    refetch,
    camperQueryVariables,
  };
});
//SomeComponent.ts
<template>
  <div>
    <!-- This is undefined -->
    result: {{ result }} 

    <!-- This works as expected -->
    camperQueryVariables: {{ camperQueryVariables }}
  </div>
</template>

<script lang="ts" setup>
import { useCamperStore } from "@/stores/campers";
const { result, camperQueryVariables } = useCamperStore();
</script>

Versions
"vue": "^3.3.4",
"nuxt": "^3.7.4",
"@nuxtjs/apollo": "^5.0.0-alpha.7",
"@vue/apollo-composable": "^4.0.0-beta.11",
"@apollo/client": "^3.8.7",

@bipsen
Copy link
Author

bipsen commented Jan 11, 2024

My best solution so far has been to use a reactive object with onResult. Is that best solution to this problem?

// stores/campers.ts
import SOME_QUERY from "@/gql/queries/SOME_QUERY.gql";

export const useCamperStore = defineStore("campers", () => {
  const result = reactive({ data: null });

  const { data: authData } = useAuth();

  const camperQueryVariables = computed(() => {
    return {
      id: authData.value?.id,
    };
  });

  const { onResult, refetch } = useQuery(SOME_QUERY, camperQueryVariables);

  onResult((queryResult) => {
    result.data = queryResult.data;
  });

  return {
    result,
    refetch,
  };
});

@Akryum
Copy link
Member

Akryum commented Jan 15, 2024

See a working example here that is passing in our automated tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants