Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • First we’ll create the directory for the query resolver by creating the following directory: /server/local/todo/classes/webapi/resolver/querymutation

  • Now let’s create the query mutation resolver class, itemsupdate_item.php in the directory we just created:

    Code Block
    languagephp
    <?php
    
    namespace local_todo\webapi\resolver\mutation;
    
    use core\webapi\execution_context;
    use core\webapi\mutation_resolver;
    use local_todo\entity\item;
    
    class update_item extends mutation_resolver {
    
        public static function resolve(array $args, execution_context $ec) {
            $item_id = $args['item_reference']['id'] ?? null;
            $new_title = $args['input']['title'] ?? null;
    
            $item = new item($item_id);
            $item->title = $new_title;
    
            return ['item' => $item];
        }
    }

    In this file we’ve defined the mutation resolver and we’re using the ORM entity of local_todo to update and return the item.

  • Now we create the type resolver directory /server/local/todo/classes/webapi/resolver/type/ and the type resolver for item item.php in the directory:

    Code Block
    languagephp
    <?php
    
    namespace local_todo\webapi\resolver\type;
    
    use core\webapi\execution_context;
    use core\webapi\type_resolver;
    
    class item extends type_resolver {
        /**
         * @param string $field - The field being requested 
         * @param $source - In the case, source will be our `item` entity class as it's what's returned from the query resolver
         * @param array $args
         * @param execution_context $ec
         * @return mixed|void
         */
        public static function resolve(string $field, $source, array $args, execution_context $ec) {
            return $source->$field;
        }
    }

Run the

...

mutation

If you haven’t already, please follow the Testing queries and mutations section to set up your developer environment.

The GraphQL data is cached by default, so you may need to run purge your cache to see the latest changes.

The mutation to run would be:

Code Block
languagegraphql
mutation local_todo_update_item {
    local_todo_update_item(
        item_reference: {id: 8}
        input: {title: "My new title"}
    ) {
        item: {
            id
            title
        }
    }
}

Using internal GraphQL in Tui components

...