Best way to migrate from a graphql database running on top of MongoDB?

I’m doing a proof-of-concept migrating an existing app running on a MongoDB database with an Apollo Graphql server/client architecture.

I think I’ve figured it out - but it seems a little bit more convoluted than it needs to be.

Let’s say I have two types that I want to migrate (highly simplified):

type Member {
  id: ID!
  name: String!
  concepts: [Concept]
}

and Concepts:

type Concept {
  id: ID!
  name: String
  parent: Concept
  children: [Concept] @hasInverse(field: parent)
}

To do this, I create two Apollo clients, one is pointing at the SOURCE_MONGO, the other at the DEST_SLASH.

I can populate the Concept nodes like this:

const createConcepts = (rootConcepts: Array<MongoConcept>) => {
  console.log('Mapping concepts.');
  const conceptInputs: Array<AddConceptInput> = rootConcepts.map(
    (con: MongoConcept) => {
      let input: AddConceptInput = {
        name: con.name,
        children: con.children.map((kid) => {
          let ref: ConceptRef = {
            name: kid.name,
          };
          return ref;
        }),
      };
      return input;
    }
  );
  console.log(`Adding concepts`);
  return toClient
    .mutate({
      mutation: ADD_CONCEPTS,
      variables: { input: conceptInputs },
    })
    .then((response) => {
      console.log(response);
    })
    .catch((error) => {
      console.log('Error adding concepts.');
      console.error(JSON.stringify(error));
    });
};

That all works fine. Now, what’s the best way to add the Members? I tried a brute force approach by returning the list of Concepts, and adding them with the Add mutation, but it just creates duplicate concepts.

How would you add Members with references to newly created Concepts?

Hi,
so happy that you’ve decided to use Dgraph. I’m here to help.

You generally want to do an upsert. But upserts in GraphQL is Hard. If it’s a one-off, you might do with some custom DQL. I can show you how too.

The general process (in GraphQL) looks like this:

  1. create the Members without the Concepts. This is doable because you have no ! in your concepts field.
  2. For each member, use updateMember to add concepts

Let’s get you over to the dark side! We have cookies.

2 Likes

Dgraph generates some CRUD GraphQL APIs, which I guess could be used for that purpose.

1 Like

You will most likely have to either create a live loader rdf map of your data using blank nodes, or you will have to use XIDs with the @id directive using the id from Mongo in the import to make the link. The good thing os that after you have imported all of your data you could roll back to regular id without the @id directive to use dgraphs native ids

1 Like