question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature idea: optimistic response with debounced server-calls mutation

See original GitHub issue

Would it be possible to have the optimistic response update immediately, while the actual request to the graphql server would be debounced?

Such that, if the first debounced mutation sends its request and the user makes another change, then when the response from the first mutation arrives it still uses the newer optimistic response, until the second mutation response resolves the end result.

I have simple debounce working, but the optimistic response isn’t used until the debounce expires, and if i remove the debounce then it makes a mutation per input event.

import React from 'react';
import debounce from 'lodash/debounce';
import { gql, graphql } from 'react-apollo';
import { View, TextInput } from 'react-native';

const Note = ({ content, updateDailynote }) =>
  <View>
    <TextInput
      value={content}
      onChangeText={updateDailynote}
    />
  </View>;

const updateDailynote = graphql(
  gql`
    mutation updateDailynote($input: UpdateDailynoteInput!) {
      updateDailynote(input: $input) {
        __typename
        dailynote {
          __typename
          nodeId
          content
          dailynoteId
        }
      }
    }
  `,
  { name: 'updateDailynote' },
);

export default updateDailynote(
  ({ nodeId, content, dailynoteId, updateDailynote }) =>
    <Note
      key={nodeId}
      content={content}
      updateDailynote={debounce(
        content =>
          updateDailynote({
            variables: {
              input: {
                nodeId,
                dailynotePatch: { content },
              },
            },
            optimisticResponse: {
              __typename: 'Mutation',
              updateDailynote: {
                __typename: 'UpdateDailynotePayload',
                dailynote: {
                  __typename: 'Dailynote',
                  nodeId,
                  content,
                  dailynoteId,
                },
              },
            },
          }),
        500,
      )}
    />,
);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jbaxleyiiicommented, Jul 13, 2017

@msand thank you for the great responses! I really appreciate the thoroughness of them. I will close this issue, but as a heads up, we are working on a new way to compose a client which would make this possible on the client level if desired. I’ll ping you when it is ready if you want to take a stab at writing a package for debouncing!

1reaction
jbaxleyiiicommented, Jul 13, 2017

@msand I wonder if this is the best approach? Since you are binding direct user interaction, it seems to me using a combination of local component state + a delayed mutation would be the better option? It will be more performant as it doesn’t go through any Apollo store actions, nor would rerender the graphql wrapper.

When possible, I’d like to steer clear of debouncing in library code since it is pretty much always tied to direct UI piping which is the better place to implement the delay

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimistic mutation results - Apollo GraphQL Docs
As this example shows, the value of optimisticResponse is an object that matches the shape of the mutation response we expect from the...
Read more >
Apollo client: Making optimistic updates while creation is still ...
An optimistic response accordingly, can be configured to this one mutation. On the server you need to check if an item with such...
Read more >
Optimistic updates with Apollo Graphql - Discuss Dgraph
I am trying to figure out how to get an optimistic response when I add a task to my todo list. ADD_TASK mutation...
Read more >
Implement an Optimistic Response to a React UI ... - Egghead.io
In this lesson, we'll show how you can have your mutations work when the user is offline. We'll handle the offline mutation with...
Read more >
[FIXED] Android live data - observe always fires after config ...
I have a simple activity that sends request for a password change to server and acts according to HTTP response code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found