Skip to content

Commit

Permalink
Sharing Saved Objects developer guide: Step 2
Browse files Browse the repository at this point in the history
This step demonstrates the changes to update client code to use the new
SavedObjectsClient `resolve()` method instead of `get()`.
  • Loading branch information
jportner committed Sep 9, 2021
1 parent 30b8fe6 commit c02076e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions x-pack/plugins/notes_test/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* 2.0.
*/

import type { CoreStart, SimpleSavedObject } from 'src/core/public';
import type { CoreStart, ResolvedSimpleSavedObject, SimpleSavedObject } from 'src/core/public';

import type { NoteAttributes } from '../common';
import { NOTE_OBJ_TYPE } from '../common';

export interface Services {
createNote: (subject: string, text: string) => Promise<void>;
findAllNotes: () => Promise<Array<SimpleSavedObject<NoteAttributes>>>;
getNoteById: (id: string) => Promise<SimpleSavedObject<NoteAttributes>>;
getNoteById: (id: string) => Promise<ResolvedSimpleSavedObject<NoteAttributes>>;
addSuccessToast: (message: string) => void;
}

Expand All @@ -33,8 +33,8 @@ export function getServices(core: CoreStart): Services {
return findResult.savedObjects;
},
getNoteById: async (id: string) => {
const savedObject = await savedObjectsClient.get<NoteAttributes>(NOTE_OBJ_TYPE, id);
return savedObject;
const resolveResult = await savedObjectsClient.resolve<NoteAttributes>(NOTE_OBJ_TYPE, id);
return resolveResult;
},
addSuccessToast: (message: string) => core.notifications.toasts.addSuccess(message),
};
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/notes_test/public/view_note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@elastic/eui';
import { Link, useParams } from 'react-router-dom';

import type { SimpleSavedObject } from 'src/core/public';
import type { ResolvedSimpleSavedObject } from 'src/core/public';
import type { NoteAttributes } from '../common';
import type { Services } from './services';

Expand All @@ -28,16 +28,17 @@ interface Props {
services: Services;
}

type NoteObject = SimpleSavedObject<NoteAttributes>;
type ResolvedNote = ResolvedSimpleSavedObject<NoteAttributes>;

export function ViewNote({ services }: Props) {
const { noteId } = useParams<Params>();
const [note, setNote] = useState<NoteObject | null>(null);
const [resolvedNote, setResolvedNote] = useState<ResolvedNote | null>(null);
const note = resolvedNote?.saved_object;

const fetchNote = async () => {
const savedObject = await services.getNoteById(noteId);
const resolveResult = await services.getNoteById(noteId);

setNote(savedObject);
setResolvedNote(resolveResult);
};

useEffect(() => {
Expand Down

0 comments on commit c02076e

Please sign in to comment.