Encountered an uncaught error in Vue3 Vue-Apollo: Apollo client with the identifier "default" was not found

I have been working with Vue3 and the composition API along with vue-apollo. I am trying to send a mutation to a GraphQL endpoint using useMutation(). However, while useQuery works perfectly fine, I am facing some issues with useMutation.

Initially, everything was working smoothly. But after running npm build on a different git branch, it seems to have stopped functioning properly.

try {
      const { mutate, onDone, onError, error } = useMutation(ADD_CARDOWNER_MUTATION)

      mutate({
        email: register.email,
        username: register.email,
        password: '',
        first_name: register.firstName,
        last_name: register.lastName,
        nfc_user_avatar: register.avatar_id,
        nfc_user_addresses: register.addresses,
        nfc_user_contacts: register.contacts,
        nfc_user_links: register.links,
        nfc_user_company: register.companyName,
        nfc_user_position: register.position,
        nfc_user_title: register.title,
        nfc_user_position__public: register.positionPublic,
        nfc_user_company__public: register.companyPublic,
        nfc_user_agb__accepted: register.agbAccepted,
      })

      onDone((data) => {
        //formNav.next()
        console.log('data', data)
      })

      onError(() => {
        console.log(error.value)
      })
    } catch (error) {
      console.error(error)
    }
  }

The mutation being used is ADD_CARDOWNER_MUTATION

mutation AddCardOwner(
  $email: String
  $password: String
  $username: String
  $first_name: String
  $last_name: String
  $nfc_user_addresses: [NFCUserAddress]
  $nfc_user_contacts: [NFCUserContact]
  $nfc_user_links: [NFCUserLink]
  $nfc_user_agb__accepted: Boolean
  $nfc_user_position__public: Boolean
  $nfc_user_company__public: Boolean
  $nfc_user_company: String
  $nfc_user_position: String
  $nfc_user_title: String
  $nfc_user_avatar: String
) {
  registerNFCUser(
    input: {
      email: $email
      password: $password
      username: $username
      first_name: $first_name
      last_name: $last_name
      nfc_user_addresses: $nfc_user_addresses
      nfc_user_contacts: $nfc_user_contacts
      nfc_user_links: $nfc_user_links
      nfc_user_agb__accepted: $nfc_user_agb__accepted
      nfc_user_company__public: $nfc_user_company__public
      nfc_user_position__public: $nfc_user_position__public
      nfc_user_company: $nfc_user_company
      nfc_user_position: $nfc_user_position
      nfc_user_title: $nfc_user_title
      nfc_user_avatar: $nfc_user_avatar
    }
  ) {
    nfc_user_id
    user_id
    registered
    username
    status
    error
  }
}

I'm encountering an error displaying here https://i.sstatic.net/dafLC.png

Here's my main.js file:

import { provide, createApp, defineAsyncComponent, h } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import './assets/main.css'
import UUID from 'vue3-uuid'
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core'
import { DefaultApolloClient } from '@vue/apollo-composable'

const httpLink = createHttpLink({
  uri: import.meta.env.VITE_PUBLIC_API_URI,
  credentials: 'include',
})

const cache = new InMemoryCache()
const apolloClient = new ApolloClient({
  link: httpLink,
  cache: cache,
})

const app = createApp({
  setup() {
    provide(DefaultApolloClient, apolloClient)
  },
  render: () => h(App),
})

const requireComponent = import.meta.glob('./components/**/**/*.vue')

Object.entries(requireComponent).forEach(([path, definition]) => {
  const componentName = path
    .split('/')
    .pop()
    .replace(/\.\w+$/, '')
  app.component(componentName, defineAsyncComponent(definition))
})

app.use(router)
app.use(createPinia())
app.use(UUID)
app.mount('#app')

Answer №1

I stumbled upon a solution that turned out to be quite simple. All I had to do was move the line containing

const { mutate, onDone, onError, error } = useMutation(ADD_CARDOWNER_MUTATION)
outside of the function where it was originally placed. After making this adjustment, everything started functioning as expected.

Before

<script setup>
  import { useRegisterDataStore } from '@/stores/RegisterDataStore.js'
  import { useFormNavStore } from '@/stores/FormNavStore.js'
  import { ref } from 'vue'
  import { useMutation } from '@vue/apollo-composable'
  import ADD_CARDOWNER_MUTATION from '@/graphql/AddCardOwner.mutation.gql'

  const register = useRegisterDataStore()
  const formNav = useFormNavStore()
  const email = ref('')
 

  const addEmail = () => {
    register.updateCardOwner('email', email.value)
    email.value = ''

    try {
       //--> this line was in the wrong place
       const { mutate, onDone, onError, error } = useMutation(ADD_CARDOWNER_MUTATION)
      
       mutate({
        email: register.email,
        username: register.email,
        password: '',
        first_name: register.firstName,
        last_name: register.lastName,
        nfc_user_avatar: register.avatar_id,
        nfc_user_addresses: register.addresses,
        nfc_user_contacts: register.contacts,
        nfc_user_links: register.links,
        nfc_user_company: register.companyName,
        nfc_user_position: register.position,
        nfc_user_title: register.title,
        nfc_user_position__public: register.positionPublic,
        nfc_user_company__public: register.companyPublic,
        nfc_user_agb__accepted: register.agbAccepted,
      })

      onDone((data) => {
        //formNav.next()
        console.log('data', data)
      })

      onError(() => {
        console.error(error.value)
      })
    } catch (error) {
      console.error(error)
    }
  }
</script>

Now

<script setup>
  import { useRegisterDataStore } from '@/stores/RegisterDataStore.js'
  import { useFormNavStore } from '@/stores/FormNavStore.js'
  import { ref } from 'vue'
  import { useMutation } from '@vue/apollo-composable'
  import ADD_CARDOWNER_MUTATION from '@/graphql/AddCardOwner.mutation.gql'

  const register = useRegisterDataStore()
  const formNav = useFormNavStore()
  const email = ref('')
  
  //--> Moved the line
  const { mutate, onDone, onError, error } = useMutation(ADD_CARDOWNER_MUTATION)

  const addEmail = () => {
    register.updateCardOwner('email', email.value)
    email.value = ''

    try {
      mutate({
        email: register.email,
        username: register.email,
        password: '',
        first_name: register.firstName,
        last_name: register.lastName,
        nfc_user_avatar: register.avatar_id,
        nfc_user_addresses: register.addresses,
        nfc_user_contacts: register.contacts,
        nfc_user_links: register.links,
        nfc_user_company: register.companyName,
        nfc_user_position: register.position,
        nfc_user_title: register.title,
        nfc_user_position__public: register.positionPublic,
        nfc_user_company__public: register.companyPublic,
        nfc_user_agb__accepted: register.agbAccepted,
      })

      onDone((data) => {
        //formNav.next()
        console.log('data', data)
      })

      onError(() => {
        console.error(error.value)
      })
    } catch (error) {
      console.error(error)
    }
  }
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for displaying specific data from a sub-array using VueJS

I am facing an issue with a collection of JSON format files structured as follows: { "_id" : "name_id", "apkMode" : "SCROLL", "date" : "2022-04-25", "timestamp" : NumberLong(16551858 ...

Mobile devices do not support CSS3 transition functionality

I have a straightforward div setup that smoothly transitions upward when swiped up on the screen, functioning perfectly on my desktop. However, I am encountering difficulties getting the transition to work on my mobile device (also tested on an Android emu ...

The Ajax form is failing to send any headers

Whenever I submit my form, the header data doesn't seem to be coming through. Even though I've done this type of submission numerous times (figuratively speaking), there's always a chance that I might be overlooking something. Any ideas? Che ...

What is the best way to convert mm:ss into seconds with the help of momentjs?

In my current project using reactjs, I am faced with a challenge regarding time format. The frontend displays the time in the format "mm:ss" as shown in this example data: const data = [{ duration: "00:10" //I want to retrieve 10 seconds }, { duration: ...

Displaying Populated Dropdown Menus from Remote Sources Using Vue.JS

Here is the UI I am trying to implement for better clarity. You can view it here. I have successfully managed to dynamically add and remove drop-down lists and text-boxes using the + and - buttons. However, I am facing an issue where selecting an item fro ...

Encountering a no-loops/no-loops eslint error in node.js code while attempting to utilize the for and for-of loops

While working on my nodejs application, I have encountered an issue with es-lint giving linting errors for using 'for' and 'for-of' loops. The error message states error loops are not allowed no-loops/no-loops. Below is the snippet of c ...

What is the best way to transform an HTML <script> tag into React code?

I am new to the world of JavaScript and React. Can someone help me convert the script below into a React component? <div id="SOHUCS" sid="xxx"></div> <script charset="utf-8" type="text/javascript" sr ...

I'm currently utilizing lint in my Angular 2+ project. Is there a way to arrange the constructor parameters in alphabetical order

I am struggling to organize the constructor parameters in TypeScript while using TSLINT with Angular9. I am looking for a rule similar to member-ordering that can help me sort them effectively. constructor( // Sort these private readonly router: R ...

Enabling dynamic HTML escaping in Flask template

Using Flask's jinja2 templates, I have set up a webpage that allows users to input data in Markdown format. When the user submits the data, a request is sent to the server and the corresponding view converts the data to HTML. Using jquery, I then inse ...

What could be causing some elements to not be generated while utilizing HTTrack or Save All Resources on an HTML webpage?

I'm interested in understanding the code and logic behind an online game called Paper.io. In order to accomplish this, I decided to save the entire webpage along with its resources on my computer and observe how each element reacts individually. My ...

Is it possible to find a more efficient approach than calling setState just once within useEffect?

In my react application, I find myself using this particular pattern frequently: export default function Profile() { const [username, setUsername] = React.useState<string | null>(null); React.useEffect(()=>{ fetch(`/api/userprofil ...

Exploring Vue Slots: A guide to parsing and rendering slot components

Currently facing a challenge while developing a web page using Vue, specifically with parsing and rendering the child components inside the <slot>. I need to extract the slot content, convert it into an array of components, and display these compo ...

Manipulating Select2 without using jQuery

Is there a way to manage the select2 component programmatically without using jQuery? I need to execute this code through Selenium, and since I cannot access the jQuery object (as it's bundled with Webpack), I have to control it using pure JS. I atte ...

Developing a function that takes a parameter which can be used with or without an additional argument when invoked

In my React application, I have a method that accepts a parameter for displaying a modal. const displayModal = (p:Result) => { setConfirm(true); if(p) { //check variable for truthy setSelectedRow(p); } ...

A guide on how to activate an event when a radio button is selected using jQuery

I experimented with implementing this. When I try clicking on the radio button, the code functions correctly, but the radio button does not appear to be checked and remains unchanged. Any ideas on how to resolve this issue? <p> <input id="p ...

Having an additional slash in the VueJS3 route

My code is functioning correctly, but I have noticed an unsightly additional "/" in the link URL (appearing as "http://localhost:5173/%2FLink" in the browser's address bar). I am not happy with this and would like to have a clean URL. How can I get ri ...

The request for the specified URL is incorrect

When I send an axios request to an Express URL called "getstatus" in my local development environment, everything works fine. However, once the files are uploaded to a server, the URL path still contains "localhost." this.$axios.get('api/getstatus&ap ...

Guide on presenting an image retrieved from a database with ajax

I am currently developing a straightforward mobile application that utilizes a database to store various types of content, including images. I have implemented ajax requests to retrieve this information and display it within the app. However, I am encounte ...

Unable to input AJAX output variable into an HTML form

Currently, I'm trying to display the result of a calculation inside a span tag with the id "estimation2". The calculation involves adding the values in two input boxes named "SHm2" and "STm2". To achieve this, I have implemented an AJAX call. Everythi ...

What is the best way to delete an input field once it has been cleared?

After scouring through resources, I found a way to dynamically add input fields on keystroke by referencing an answer from this question. To see my implementation, check out this example. However, one challenge remains - removing a field if the user delete ...