AppSync GraphQL Mocking Resolver Map Fails to Create Distinct Items

My issue lies in the mock data generation process where instead of unique data for each item, all items end up having the same field value.

Suggested Solution 1: (ideal approach, incorrect results)

Within the AppSync schema, there is an items field that contains a list of Model. If I use the Model resolver on its own, all the Model objects within the items list end up with the same value.

const mocks = {
  ModelModelConnection: () => ({
    items: () => new MockList(5),
  }),
  Model: () => ({
    id: casual.uuid,
    name: casual.title,
  }),
};

This setup leads to...


Suggested Solution 2: (alternative method, correct results)

const mocks = {
  ModelModelConnection: () => ({
    items: () => new MockList(5, () => ({
      id: casual.uuid,
      name: casual.title,
    })),
  }),
};


I am inclined towards Option 1, but struggling to achieve unique data while mocking. Any insights or assistance would be greatly appreciated!

Answer №1

According to the documentation, if you want a field's resolver to generate a different value each time it is called, the mock resolver should be a function rather than a static value. Instead of:

Model: () => ({
  id: casual.uuid,
  name: casual.title,
}),

you should use:

Model: () => ({
  id: () => casual.uuid,
  name: () => casual.title,
}),

With this change, both id and name will be invoked every time the field is resolved.

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

Looking for ways to detect memory leaks in your JavaScript application using Selenium?

While utilizing Java and Selenium for automated testing of a JavaScript web application, the issue of memory leaks has arisen. I am interested in ways to effectively test for them. Is there a simple method to obtain memory usage and other profiling data fo ...

Ensuring Property Security with Sequelize JS

var Game = sequelize.define('Game', { secretField: { type: DataTypes.STRING(45), validate: { notEmpty: true } } }, { getterMethods: { secretFieldHash: function () { return cryp ...

storing a value in the browser's local storage

I am in the process of creating a new game that includes a high score feature. The idea is that when the current score surpasses the existing one stored locally, it will be replaced: localStorage.setItem('highScore', highScore); var HighScore = ...

Click to switch CodeMirror's theme

http://jsbin.com/EzaKuXE/1/edit I've been attempting to switch the theme from default to cobalt and vice versa, toggling each time the button is clicked. However, I am facing an issue where it only switches to the new theme once and doesn't togg ...

Discovering and revising an item, delivering the complete object, in a recursive manner

After delving into recursion, I find myself at a crossroads where I struggle to return the entire object after making an update. Imagine you have an object with nested arrays containing keys specifying where you want to perform a value update... const tes ...

Alter the value of a parameter within a script tag with JavaScript/jQuery

Looking to dynamically change a parameter value using JavaScript or jQuery. Here is an example URL: www.exampleurl.com/somepage?foo=test I have a function that can extract the value after the 'foo' parameter: function getParameterByName(name, ...

Can the background image of a div be cropped using CSS/JavaScript techniques?

I have a div with a background image of a world map that I need to clip at two precise points. Clipping from the left side is straightforward. In my CSS: .le-map { background-image: url('../img/le-map-of-le-world.mlg'); background-size: 100% ...

Conquering challenges arising from organizing subdirectories for js/css/xml files

During the process of developing a website with html, css, js and xml files stored on my computer (not yet online), I initially kept all the files in one folder along with an images folder. However, as the project progressed and things started to get mes ...

The challenge of customizing card styling in conditional rendering with React Native

There are two primary components in this setup: homeScreen.js (the parent component) Card.js (the child component) The card component is rendered when the search is not toggled, and all necessary data is passed down as props without any rendering is ...

Tips on leveraging dynamic queries in your REST API usage

As a new Javascript learner, I am experimenting with creating a basic rest api using node.js. Currently, I have set up a database called testDb and a table named testMeasurement in influxdb. The testMeasurement table consists of the columns DateOfBirth, ID ...

Guide to applying external styles to sub-components in Material UI while maintaining a cohesive common style throughout the entire component

I have a main page that consists of various smaller components. Within the page folder, there is the main page file, an external styles file, and folders for each individual sub-component - each of which has its own unique style. I'm struggling to fi ...

Tips for implementing cloudinary jquery to showcase a positive outcome notification

I've been utilizing the cloudinary Upload widget to upload multiple images successfully. However, I'm encountering an issue where I can't display a success message after the process is completed. Below is the JavaScript code I am using for u ...

Efficient AJAX validation using PHP and JavaScript

I am currently working on a small website project that requires User registration functionality. One key aspect is ensuring that the system prevents users from registering with usernames that are already in use. Most modern websites employ AJAX to verify ...

Utilize the provided parameter within a JavaScript function

<script type="text/javascript"> function updateTrackName(trackNum) { document.form1.track_(track_number)_name.value=document.form1.track_(track_number)_parent_work.value; } </script> To modify the line inside of the parent_wor ...

Is it possible to control the positioning of a tooltip for an input element?

Currently, I am creating a filtering list that requires text input elements for each column due to limited space. Each input will initially display the name of the corresponding column and then clear when clicked by the user. To help users remember which ...

What is the best way to get jsDoc "import" to function properly in vscode?

Is it possible to import a node module using @import in Visual Studio Code? I'm trying it but it doesn't seem to be recognized. Am I missing something? https://i.stack.imgur.com/zq1rz.png ...

VueJS 3 custom Checkbox fails to update UI upon clicking

I'm attempting to implement a customized checkbox using Vue 3 and the composition API based on this example. However, despite confirming through devtools that all my props and bound data are successfully passed from the parent component to the child c ...

When calling an API endpoint, nodeJS is unable to access the local path

I've encountered a strange issue with my code. When I run it as a standalone file, everything works perfectly fine. However, when I try to utilize it in my API endpoint and make a request using Postman, it doesn't seem to function properly. What ...

Dropdown menu does not appear upon hovering over an item

I have been trying to create a menu that appears when hovering over #navigation-region. Below, you can see the HTML, CSS, and JavaScript code: The JavaScript is simply for adding the active class on the #navigation-region element. In the CSS, there is a s ...

Guide on modifying the value of a web element attribute in C# with the help of Selenium

Topic Example: <input type="text" id="cargo_q" autocomplete="off" value="5.5"/> What is a way to modify the value of the "value" attribute using Selenium web driver? Can I use a method like: IWebElement search_cargo =driver.FindElement(By.Id("car ...