vuejs / vuejs/apollo

Local state mutation runs after initial render

Open
#950 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
6k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

I'm not sure if it's a bug, it's intended, or it's something I'm just doing wrong.
Is the following behavior expected?

  • Do a local mutation (doesn't matter if $apollo.mutate or useMutation, i tried with both) on App.vue's setup function.
  • Add a debugger in the resolver of this mutation in order to see when the code runs.

What I would expect: This mutation being executed before things are mounted.
Actual behavior: This mutation happens asynchronously, after the first render happens.

Is this intended? Local mutations most of the times are synchronous.
In my project, the mutation is setting data based on the $route (from a non-lazy $watch), so it is forcing to render the app twice, once with the initial state (before the mutation), and then with the proper state (after the mutation).

Relevant piece of code:

setup(props, context) {
    const currentCategoryId = computed(() => context.root.$route.params.categoryId);
    watch(currentCategoryId, (categoryId) => {
      setCurrentCategory({ id: categoryId });
    });
    const { category: currentCategory } = useCurrentCategory();

    watch(currentCategory, (currentCategory) => {
      // This function is called twice:
      // - First (before app is mounted) with currentCategory === undefined
      // - Then (after app is mounted) with the proper category
      context.root.$vuetify.theme.currentTheme.primary = currentCategory
        ? currentCategory.color
        : '#333333';
    });
}

I've reproduced the same scenario with vuex, and nothing is rendered until mutation is done.

Versions:

  • vue-apollo@3.0.3
  • vue@2.6.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the App.vue setup function and the local mutation paths mentioned, $apollo.mutate and useMutation. Reproduce the debugger timing in the resolver and compare it with the Vuex behavior described. Done means establishing whether the post-render execution is intended and identifying the relevant lifecycle or scheduling behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.