Blog

🏁 Agora vocĂȘ pode mutar valores meta no Gato GraphQL

Leonardo Losoviz
Por Leonardo Losoviz ·

v11.3 do Gato GraphQL foi lançada hoje, com suporte a uma funcionalidade importante: Mutations de meta!

Agora vocĂȘ pode adicionar, atualizar e excluir valores meta para custom posts, tags, categorias, comentĂĄrios e usuĂĄrios.

A seguir, exemplos de queries que mutam meta.

Adicionando meta

VocĂȘ pode adicionar entradas meta a custom posts, tags, categorias, comentĂĄrios e usuĂĄrios.

Esta query adiciona uma entrada meta ao post com 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") 
    }
  }
}

Esta query adiciona a mesma chave meta com valores diferentes a posts diferentes, em lote:

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") 
    }
  }
}

Atualizando meta

Atualizar uma entrada meta de categoria:

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

Esta query usa nested mutations para atualizar um valor meta em um post:

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") 
      }
    }
  }
}

Excluindo meta

Excluir uma entrada meta de um post:

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

Excluir a mesma entrada meta de mĂșltiplos posts, em lote:

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") 
    }
  }
}

Definindo mĂșltiplas entradas meta de uma vez

VocĂȘ pode definir mĂșltiplas entradas meta de uma vez passando um JSON para as diferentes mutations set{Entity}Meta:

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"])
    }
  }
}

Definindo entradas meta ao criar/atualizar uma entidade

VocĂȘ pode definir entradas meta diretamente ao criar ou atualizar um custom post, tag, categoria ou comentĂĄrio, via parĂąmetro meta.

Esta query define meta ao adicionar um comentĂĄrio:

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"]) 
    }
  }
}

Esta query injeta o meta na nested mutation 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") 
      }
    }
  }
}

Assine nossa newsletter

Fique por dentro de todas as atualizaçÔes do Gato GraphQL.