React the first Apollo object from the subscription without merging with previous data that is actually deleted

I have a request that gets me a list of notes and a subscription that listens and inserts new notes, modifying the request. However, the problem in the first note is not added.

So, let me add more detailed information, initially responding to a query with an object that contains an attribute called notes, which is an array of length 0, if we try to add a note, the attribute will be deleted. A note is created, so if I update my application, the request will return a note. If I try to add a note again, the note will be added to the array in the request object.

Here is my notes container where I request notes and create a new property to subscribe to other notes.

export const NotesDataContainer = component => graphql(NotesQuery,{

name: 'notes',
props: props => {

  console.log(props); // props.notes.notes is undefined on first note added when none exists.

  return {
    ...props,
    subscribeToNewNotes: () => {

      return props.notes.subscribeToMore({
        document: NotesAddedSubscription,
        updateQuery: (prevRes, { subscriptionData }) => {

          if (!subscriptionData.data.noteAdded) return prevRes;

          return update(prevRes, {
            notes: { $unshift: [subscriptionData.data.noteAdded] }
          });

        },
      })
    }
  }
}

})(component);

Any help would be great, thanks.

EDIT:

export const NotesQuery = gql`
  query NotesQuery {
    notes {
      _id
      title
      desc
      shared
      favourited
    }
  }
`;

export const NotesAddedSubscription = gql`
  subscription onNoteAdded {
    noteAdded {
      _id
      title
      desc
    }
  }
`;

Another EDIT

class NotesPageUI extends Component {

  constructor(props) {

    super(props);

    this.newNotesSubscription = null;

  }

   componentWillMount() {

      if (!this.newNotesSubscription) {

      this.newNotesSubscription = this.props.subscribeToNewNotes();

      }

   }

   render() {

     return (
        <div>

          <NoteCreation onEnterRequest={this.props.createNote} />

            <NotesList
              notes={ this.props.notes.notes }
              deleteNoteRequest={    id => this.props.deleteNote(id) }
              favouriteNoteRequest={ this.props.favouriteNote }
            />

        </div>
     )
   }
 }

Other editing:

https://github.com/jakelacey2012/react-apollo-subscription-problem

+6
source share
1 answer

YAY made it work, just the new data sent over the wire should be the same form as the original request.

eg.

NotesQuery has this form ...

query NotesQuery {
  notes {
    _id
    title
    desc
    shared
    favourited
  }
}

but the data coming through the subscription transaction had this form.

subscription onNoteAdded {
  noteAdded {
    _id
    title
    desc
  }
}

notice sharedand are favouritednot in the subscription request. If we added them, they will now work.

, react-apollo , , . , .

react-apollo, , - .

https://github.com/apollographql/react-apollo/issues/649

0

Source: https://habr.com/ru/post/1016417/


All Articles