Apollo GQL useMutation does not return data field

Viewed 27

I have the following mutation that works just fine in the Sandbox (it creates and returns the bookmark):

mutation CreateBookmark ($options: BookmarkInput!) {
  createBookmark(options: $options) {
    bookmark {
      id
      title
      url
    }
  }
}

When I try to create a bookmark from my react app, the bookmark is being created but data is undefined.

This is my code:

const CREATE_BOOKMARK = gql`
    mutation CreateBookmark($options: BookmarkInput!) {
        createBookmark(options: $options) {
            bookmark {
                id
                title
                url
            }
        }
    }
`;

const [mutateFunction, {data, error, loading}] = useMutation(CREATE_BOOKMARK);

mutateFunction({
    variables: {
        options: {
            title: bookmark.title,
            url: bookmark.url,
        },
    },
});

When I try to console.log(data) it returns undefined.

I have also tried to not destructure the object that is being returned like this:

const [mutateFunction, obj] = useMutation(CREATE_BOOKMARK);

When I console.log(obj), it returns this:

{
  error: ...,
  loading: ...,
  client: ...,
  reset: ...
}

As you can see, there is no data field. Why is this happening?

0 Answers
Related