Mapping all UIDs before Upgrade HELP!

I added a predicate to my DQL schema:

  old_xid: string .

Added a field to all of my GraphQL interfaces and non implementing types:

type Atype {
  xid: String @dgraph(pred:"old_xid") @search(by: [exact])
  ...
}

Wrote a quick and ugly node script:

const fetch = require('node-fetch')

const commit = true
const endpoint = 'https://<my-endpoint>.cloud.dgraph.io'
// TODO: You must enter a valid token from Dgraph Cloud GUI to use this script.
const xAuthToken = '<my-dgraph-cloud-token>'

async function main() {
  return fetch(`${endpoint}/query`, {
    method: 'post',
    headers: {
      'x-auth-token': xAuthToken,
      'Content-type': 'application/dql',
    },
    body: `
        query {
          n(func: has(dgraph.type), first: 100000) @filter(NOT has(old_xid)) {
            uid
          }
        }
      `,
  })
    .then((res) => res.json())
    .then((res) => {
      if (res.data.n.length === 0) {
        console.log('Completed UID Map!')
        process.exit()
      }
      console.log(res.data.n[0])
      let rdf = ''
      res.data.n.forEach((node) => {
        rdf += `<${node.uid}> <old_xid> "${node.uid}" .\n`
      })
      fetch(`${endpoint}/mutate${commit ? '?commitNow=true' : ''}`, {
        method: 'post',
        headers: {
          'x-auth-token': xAuthToken,
          'Content-type': 'application/rdf',
        },
        body: `{
            set {
              ${rdf}
            }
          }`,
      })
        .then((res) => res.json())
        .then((res) => {
          console.log(res)
          return main()
        })
    })
}

return main()

And Done!

Now after the upgrade we will be able to query a list of xids and get their mapped new uid:

query ($xids:[String]) {
  queryAtype(filter: {xid: {in: $xids}}) {
    xid
    id
  }
}

@MichelDiz thank you for your help as always!!

2 Likes