ブログ

🏁 Gato GraphQLでメタ値をミューテートできるようになりました

Leonardo Losoviz
著者: Leonardo Losoviz ·

本日、Gato GraphQL の v11.3 がリリースされました。重要な機能が追加されています:メタミューテーション

カスタム投稿、タグ、カテゴリ、コメント、ユーザーに対してメタ値を追加・更新・削除できるようになりました。

以下に、メタをミューテートするクエリの例を示します。

メタの追加

カスタム投稿、タグ、カテゴリ、コメント、ユーザーにメタエントリを追加できます。

このクエリは、ID 4 の投稿にメタエントリを追加します:

mutation {
  addCustomPostMeta(input: {
    id: 4
    key: "some_key"
    value: "Some value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

このクエリは、同じメタキーに異なる値を複数の投稿に対してまとめて追加します:

mutation {
  addCustomPostMetas(inputs: [
    {
      id: 4
      key: "some_key"
      value: "Some value"
    },
    {
      id: 5
      key: "some_key"
      value: "Some other value"
    },
    {
      id: 6
      key: "some_key"
      value: "Yet another value"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

メタの更新

カテゴリのメタエントリを更新します:

mutation {
  updateCategoryMeta(input: {
    id: 20
    key: "_source"
    value: "Updated source value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      __typename
      id
      metaValue(key: "_source") 
    }
  }
}

このクエリは、ネストされたミューテーションを使用して投稿のメタ値を更新します:

mutation {
  post(by: {id: 1}) {
    updateMeta(input: {
      key: "some_key"
      value: "Updated description"
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

メタの削除

投稿からメタエントリを削除します:

mutation {
  deletePostMeta(input: {
    id: 5
    key: "some_key"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

複数の投稿から同じメタエントリをまとめて削除します:

mutation {
  deletePostMetas(inputs: [
    {
      id: 5
      key: "some_key"
    },
    {
      id: 6
      key: "some_key"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

複数のメタエントリを一度に設定する

各種 set{Entity}Meta ミューテーションに JSON を渡すことで、複数のメタエントリを一度に設定できます:

mutation {
  setCustomPostMeta(input: {
    id: 4
    entries: {
      single_meta_key: [
        "This is a single entry",
      ],
      object_meta_key: [
        {
          key: "This is a key",
          value: "This is a value",
        },
      ],
      array_meta_key: [
        "This is a string",
        "This is another string",
      ],
      object_array_meta_key: [
        [
          {
            key: "This is a key 1",
            value: "This is a value 1",
          },
          {
            key: "This is a key 2",
            value: "This is a value 2",
          },
        ]
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      meta(keys: ["single_meta_key", "object_meta_key", "array_meta_key", "object_array_meta_key"])
    }
  }
}

エンティティの作成・更新時にメタエントリを設定する

カスタム投稿、タグ、カテゴリ、またはコメントを作成・更新する際に、meta パラメータを使って直接メタエントリを定義できます。

このクエリは、コメントを追加する際にメタを設定します:

mutation {
  addCommentToCustomPost(input: {
    customPostID: 1130
    commentAs: { html: "New comment" }
    meta: {
      some_meta_key: [
        "This is a single entry",
      ],
      another_meta_key: [
        "This is an array entry 1",
        "This is an array entry 2",
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    comment {
      id
      meta(keys: ["some_meta_key", "another_meta_key"]) 
    }
  }
}

このクエリは、ネストされたミューテーション Post.update にメタを注入します:

mutation {
  post(by: {id: 1}) {
    update(input: {
      meta: {
        single_meta_key: [
          "This is an updated value",
        ]
      }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

ニュースレターを購読する

Gato GraphQL のすべてのアップデートを把握しましょう。