Kashuab / mobx-depot

Scaffold MobX-powered models, queries and mutations with your GraphQL schema.
https://mobx-depot.dev
MIT License
8 stars 0 forks source link

Implement React utilities #1

Closed KashubaK closed 1 year ago

KashubaK commented 1 year ago

Hooks

Currently we generate mutation/query classes. I'd like to abstract the direct implementation of those classes, for example:


import { useQuery, useInstance, useMutation } from 'mobx-depot';

import { AllTodosQuery } from '~/models/queries/AllTodosQuery';
import { CreateTodoMutation } from '~/models/mutations/CreateTodoMutation';
import { TodoModel } from '~/models/TodoModel';

function Todos() {
  const { loading, data } = useQuery(AllTodosQuery, { sortBy: 'dateCreated' });

  data?.todos; // -> TodoModel[] | undefined;

  const newTodo = useInstance(TodoModel, { title: '', content: '' });
  const { loading, data, mutate } = useMutation(CreateTodoMutation);

  const handleSubmit = async () => {
    const data = await mutate({ todo: newTodo });

    data.todo; // -> TodoModel, ideally same reference as newTodo...? (sounds hard!)
  }
}