Skip to content

Add the return property 'called' to 'useQuery` #1066

@DarkLite1

Description

@DarkLite1

Is your feature request related to a problem? Please describe.
useMutation has a boolean return ref named called. Would it be possible to add a similar ref to useQuery?

called: boolean Ref holding true if the mutation was already called.

The issue we're having is that we can't define the correct order in the code below. Ideally we need the query (useViewerQuery) needs to be called before the mutation (useSetPreferenceDefaultMutation). This is in the code below not always happening which replaces a user's preferences with defaults.

export default defineComponent({
  setup() {
    const {
      result: queryResult,
      loading: queryLoading,
      error: queryError,
    } = useViewerQuery(() => ({
      enabled: isAuthenticated.value,
    }))

    const {
      mutate: setDefaultPreferences,
      loading: mutationLoading,
      error: mutationError,
      called: mutationCalled,
    } = useSetPreferenceDefaultMutation({
      variables: {
        language: 'en-us',
        darkMode: false,
      },
    })

    onMounted(() => {
      watchEffect(() => {
        if (
          isAuthenticated.value &&
          !queryLoading.value &&
          !queryResult.value?.viewer?.preference &&
          !mutationCalled.value
        ) {
          void setDefaultPreferences()
        }
      })
    })

    return {
      isAuthenticated,
      loading: queryLoading || mutationLoading,
      error: queryError || mutationError,
    }
  },
})

Describe the solution you'd like
If we could check if the query was called first we can adapt watchEffect so it will only be triggered when afterwards.

Describe alternatives you've considered
We've tried wrapping the watchEffect within the onMounted hook but this results in the same issue being that the mutation is called first and only then query.

Additional context
This is of course a bit related to the previous feature request.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions