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?