Laravel Маяк - Как обновить несколько моделей - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь обновить несколько моделей с помощью директивы, но текущая директива @update не поддерживает несколько идентификаторов. В основном мне нужна директива @delete (где вы можете использовать список идентификаторов). Чтобы обновить несколько моделей. Я предполагаю, что мог бы создать специальную директиву, но там много кода, который я не могу понять. Я попытался прочитать документы , чтобы понять, как создать настраиваемую директиву, но я не могу заставить ее работать.

Итак, DeleteDirective. php получил следующее:

/**
 * Bring a model in or out of existence.
 *
 * @param \Illuminate\Database\Eloquent\Model $model
 * @return void
 */
protected function modifyExistence(Model $model): void
{
    $model->delete();
}

И я бы в основном хотел это (для нескольких идентификаторов):

    /**
    * Update a model to read true
    *
    * @param \Illuminate\Database\Eloquent\Model $model
    * @return void
    */
protected function updateRead(Model $model): void
{
    $model->update(['read' => true]);
}

Определив запрос на мутацию следующим образом:

type Mutation {
  updatePostsToRead(id: [ID!]!): [Post!]! @updateRead
}

И выполнив запрос вот так:

{
   mutation {
      updatePostsToRead(id: [6,8]) {
         id
         amount
      }
   }
}

Кто-нибудь знает, как бы я сделал go этим? Или может указать мне правильное направление?

1 Ответ

0 голосов
/ 06 мая 2020

Нашел способ сделать это без создания специальной директивы. Только что произвел индивидуальную мутацию с помощью php artisan lighthouse:mutation updatePostsToRead.

updatePostsToRead. php:

class updatePostsToRead
{
    /**
     * Return a value for the field.
     *
     * @param  null  $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param  mixed[]  $args The arguments that were passed into the field.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context Arbitrary data that is shared between all fields of a single query.
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke(
        $rootValue,
        array $args,
        GraphQLContext $context,
        ResolveInfo $resolveInfo
    ) {
        // TODO implement the resolver
        \DB::table('posts')
            ->whereIn('id', $args["ids"])
            ->update(['read' => true]);

        $posts = Post::whereIn('id', $args["ids"])->get();

        return $posts;
    }
}

Схема:

type Mutation {
    updatePostsToRead(ids: [ID]): [Post]
}

Запрос на клиенте:

mutation{
  updatePostsToRead(ids: [2,6,8]) {
    id
    description
    read
  }
}
...