Issue with Vue Apollo: Unable to perform a query using a variable, resulting in an error message stating, "Cannot assign to read only property 'name' of object."

Currently, I am aiming to interact with a Django-Graphene powered GraphQL backend using a Vue Apollo / Nuxt front end in order to fetch data for a single product.

When attempting to send the query, I encountered the following error:

 ERROR  Cannot assign to read only property 'name' of object 'GraphQLError: Syntax Error: Expected Name, found $'

 at errorMiddleware (node_modules\@nuxt\server\dist\server.js:320:18)
 at call (node_modules\connect\index.js:235:7)
 at next (node_modules\connect\index.js:183:5)
 at nuxtMiddleware (node_modules\@nuxt\server\dist\server.js:205:5)

This is the code snippet that I am currently working with:

_slug.vue

  <template>
    <div>
      <h1>{{ product.name }}</h1>
      <div class="description" v-html="product.description"></div>
    </div>
  </template>

  <script>
    import { SINGLE_PRODUCT_QUERY } from '@/graphql/products'

    export default {
      props: ['id', 'slug'],
      data() {
        return {
          product: {
            name: 'Test Product',
            description: 'Test Description',
            slug: 'test'
          }
        }
      },
      apollo: {
        product: {
          query: SINGLE_PRODUCT_QUERY,
          variables: {
            slug: 'test-product'
          }
        }
      }
    }
  </script>

graphql/products.js

      import gql from 'graphql-tag'

      export const SINGLE_PRODUCT_QUERY = gql `
        query {
          product($slug: string!) {
            name
            description
            slug
          },
        }
      `;

The following query is functional with my backend:

    query {
        product (slug: "test-product") {
          id
          name
          slug
        }
      }

I suspect this issue may be caused by a simple syntax error, but I am unsure how to correct it.

UPDATE: The corrected query should look like this - credits to @DanielRearden:

    export const SINGLE_PRODUCT_QUERY = gql `
      query product( $slug: String!) {
        product(slug: $slug) {
          name
          description
          slug
        },
      }
    `;

Answer №1

The comments have highlighted the need to utilize String instead of string. Additionally, your query should resemble the following structure:

export const SINGLE_PRODUCT_QUERY = gql`
  query Product($slug: String!) {
    product(slug: $slug) {
      name
      description
      slug
    },
  }
`;

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

Checking the Vue Version in Laravel: A Guide to Finding and Updating

Inside my package.json file, I noticed the following line: "vue": "^2.1.10", however after running npm update, a different email address is displayed: [email protected]. How can I determine which version of Vue is currently installed in my Laravel Ap ...

JavaScript loading screen with media query support

I'm currently working on a JavaScript splash screen that features a video background. When the user clicks on the screen, it should smoothly transition to the home page. However, for mobile devices, I want to display an image instead of the video. My ...

Is it possible to access a comprehensive list of all the elements that are currently available?

Is there a way to retrieve all HTML tag names that are supported by the browser for my web application? I want it to be displayed like this: console.log(getAllElements()) //[a, abbr, acronym, address, applet, area, base, ...] ...

Is it possible to make text animation appear and fade after every click event in HTML, CSS, and JavaScript?

Currently, I have to press the button twice for the text to appear and fade. How can I make it so that clicking the button once will show and fade the "Added!" text every time, even if clicked while the text is fading? I've attempted using CSS focus, ...

What is the best way to verify a date entered into a textbox that utilizes a calendar feature?

I recently set up a javascript calendar on my asp.net textbox. Whenever I click on the textbox, a popup calendar appears allowing me to select a date. However, I am facing an issue where I need to ensure that the selected date is not in the past. Only tod ...

The animation in the HTML, CSS, and JavaScript code is not functioning properly

Recently, I came across an interesting YouTube video. Here is the link: https://www.youtube.com/watch?v=2ak37WrbSDg The video featured a captivating animation at the beginning, utilizing span text. Intrigued, I attempted to modify it by incorporating ima ...

Is there a way to retrieve the properties of another function within the same component?

I am trying to place NumberFormat inside OutlinedInput and I also need different properties for the format of NumberFormat. (There will be a select window that defines which format property to use). This is what I have: import OutlinedInput from "@ma ...

Disable the form after submission using Bootstrap 5

How can I submit a form using the Post method in Bootstrap v5 and then indicate that the form is being processed? I've encountered numerous examples for Bootstrap v4 and jQuery, but since Bootstrap v5 no longer supports jQuery and there are potential ...

Next.js encountered an issue with the element type, as it expected either a string for built-in components or a class/function for composite components, but received undefined instead

Recently, while working with next js, I encountered an issue when trying to import a rich text editor into my project. Specifically, when attempting to integrate react-draft-wysiwyg, an error message was displayed: Error: Element type is invalid... (full e ...

Identifying the source object when using JSON.stringify

There is an object set up like this: { item1: "value1", item2: "value2", item3: { item4: "value4", item5: "value5" } } The goal is to utilize JSON.stringify along with a replacer function that behaves differently for items 4 & 5 ...

JavaScript Code for Executing Function on Checkbox Checked/Unchecked

My goal is to display an image when the checkbox is checked and show text when it is unchecked. However, I am facing an issue where the text does not appear when I uncheck the checkbox. <input type="checkbox" id="checkword" onchang ...

The Bootstrap menu vanishes when tapped on mobile devices, and the header image fails to resize accordingly

After spending several hours attempting to troubleshoot on my own and seeking assistance from other online resources, I have been unsuccessful in resolving two key issues with my website. The first problem lies with the bootstrap navigation menu on www.di ...

What is the best way to transfer a mutated state to another page in your application?

Within my App.js file, I have set up a Route in the following manner: <Route exact path='/tools/ndalink' render={(props) => ( <React.Fragment> <Header /> < ...

JavaScript: Incorporating an operator into a specific object (instead of the entire class)

Are you familiar with how to include an operator in an object (rather than the entire class)? When it comes to a method, I know you can achieve that by: my_object.new_function = function(){return 1}; Then invoking my_object.new_function() will output ...

Creating a Vue.js Project on Github Pages

Despite following numerous stackoverflow posts and online tutorials, I am facing issues while trying to deploy a vuejs app on github pages. Unfortunately, no matter what I try, the page only displays the readme file. github repo github page The deploymen ...

Handling multiple image errors in Javascript: Onerror handlers for images

Seeking a solution to establish multiple fallbacks for image loading in the event that the original image source encounters an exception. If we have an image tag like <img id="art">, this JS code will set a fallback image src to album.jpg ...

Using JavaScript to Include CSS File or CSS Code

Greetings! I am curious about replicating the functionality of this PHP code in JavaScript: <?php if (strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') == true) { echo "<style>h1 { color: red; }</style>"; } ?> ...

Creating dynamic routes in React by pulling data from a JSON file

Creating multiple routes based on JSON data file is the goal: routes.jsx import React from 'react'; import { Route, Router, IndexRoute } from 'react-router'; import ReactDOM from 'react-dom'; import App from './index.j ...

Utilize the arrow keys to navigate through the search suggestions

I am facing an issue with my search bar where I am unable to navigate through the suggestions using arrow keys. I have been struggling with this problem for days and would appreciate any help in resolving it. var searchIndex = ["404 Error", "Address Bar ...

Enhance user experience in VueJS by highlighting an entire sentence upon hovering over a specific word within a paragraph

VueJS is the framework I am working with. In a specific scenario, there is content contained within a div having the class explainer, which consists of multiple paragraphs. The challenge at hand is to create functionality that allows users to hover over a ...