如何使用graphql更新集合中的所有记录 [英] How to update all records in a collection using graphql

查看:76
本文介绍了如何使用graphql更新集合中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Graph.cool graphql作为服务,并且想知道如何对集合进行大量更新,类似于SQL更新.

I'm using Graph.cool graphql as a service and am wondering how to do a mass update to the collection, similar to a SQL update.

就我而言,我需要在数据库的imageUrl列中更新URL的后缀.我需要将{someid} _sm.jpg换成{someid} _lg.jpg

In my case I need to update the suffix of a url, in the imageUrl column of my database. I need to swap out a {someid}_sm.jpg to {someid}_lg.jpg

我该如何处理graphql突变?我不想再次重新加载整个数据集,而是在寻找一种方法,它不涉及使用graphql客户端手动遍历整个列表.

How do I do that with a graphql mutation? I don't want to reload the entire dataset again and am looking for a way to do it that doesn't involve manually interating through the entire list with a graphql client.

mutation {
  updatePost() // what goes here?
}

推荐答案

迁移脚本

最好的方法确实是使用结合了多个变异,因此只有一个HTTP请求发送到GraphQL后端.

Migration script

The best approach is indeed to use a migration script that combines multiple mutations so only one HTTP request is sent to the GraphQL backend.

考虑以下模式:

type Image {
  id: ID!
  name: String!
}

我们可以使用GraphQL别名在一个请求中多次包含相同的突变:

We can include the same mutation multiple times in one request with GraphQL aliases:

mutation {
  first: updateImage(id: "first-id", name: "01_lg.jpg") {
    id
    name
  }

  second: updateImage(id: "second-id", name: "02_lg.jpg") {
    id
    name
  }
}

我们将在迁移脚本中使用此机制.我将使用 Lokka 和Node进行描述,但是您可以选择喜欢的任何语言和GraphQL客户端.

We'll make use of this mechanism in our migration script. I'll describe it with Lokka and Node, however you can choose whatever language and GraphQL client you prefer.

首先,我们查询所有现有图像以获取其 id name :

First, we query all existing images to obtain their id and name:

const queryImages = async() => {
  const result = await client.query(`{
    images: allImages {
      id
      name
    }
  }`)

  return result.images
}

然后我们相应地替换名称,并构造一个大请求,其中包括必要的 updateImage 突变,每个突变都有不同的GraphQL别名.

Then we replace the names accordingly and construct one big request including the necessary updateImage mutations with a different GraphQL alias for each.

如果您的图像名称在问题中提到的 {someid} 部分中可能包含字符串 sm ,则此脚本将中断!这种情况下,请进行相应的调整.

If your image names might contain the string sm in the {someid} part mentioned in your question, this script will break! In that case, please adjust accordingly.

const migrateImages = async(images) => {
  // beware! if your ids contain the string 'sm', adjust the string replacement accordingly!
  const updateMutations = _.chain(images)
    .map(image => ({ id: image.id, name: image.name.replace('sm', 'lg')}))
    .map(image => `
      ${image.id}: updateImage(id: "${image.id}", name: "${image.name}") {
        id
        name
      }`)
    .value()
    .join('\n')

  const result = await client.mutate(`{
    ${updateMutations}
  }`)

  console.log(`Updated ${Object.keys(result).length} images`)
  console.log(result)
}  

就是这样.如果您必须更新数千张图像,则以一百为一组的方式对突变进行批处理可能比在一个请求中对所有突变进行批处理更好.请注意,突变在GraphQL服务器上顺序运行.

That's it. If you have to update thousands of images, batching the mutations in say groups of a hundred might be better than to batch all of them in one request. Note that mutations run sequentially on the GraphQL server.

当前,我建议使用以下工作流程来运行迁移:

Currently, I suggest the following workflow for running the migration:

  • 克隆您的项目
  • 在克隆的项目上运行迁移脚本
  • 验证迁移是否成功运行.仔细检查:)
  • 在原始项目上运行迁移

您可以在此处找到代码和更多说明.

You can find the code and further instructions here.

尽管这种方法非常适合您的示例中的迁移,但并不是在所有情况下都完美.我们已经在考虑为此用例创建集成体验,例如Graphcool项目中的交互式迁移,模拟迁移,检查等.如果您有任何建议,请在松弛中告诉我.

While this approach is great for migrations that are as straightforward as in your example, it's not perfect for all situations. We're already thinking about creating an integrated experience for this use case, such as an interactive migration right in your Graphcool project, with simulated migrations, checks and more. If you have suggestions, let me know in Slack.

这篇关于如何使用graphql更新集合中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆