To import existing data to Reindex, you can create a script that reads data from any data source like JSON files, SQL databases or REST APIs and then uses a GraphQL mutation to add the data to Reindex.
Here’s a simple example of a Node.js script that imports data using reindex-js:
import Reindex from 'reindex-js';
const reindex = new Reindex(process.env.REINDEX_URL);
reindex.setToken(process.env.REINDEX_TOKEN);
const todos = [
{ text: 'Build an app', complete: true },
{ text: '???', complete: false },
{ text: 'Profit', complete: true }
];
const mutation = `
mutation ImportTodo($todo: _CreateTodoInput!) {
createTodo(input: $todo) {
id
}
}
`;
async function importTodos() {
for (const todo of todos) {
const result = await reindex.query(mutation, { todo });
if (result.errors) {
console.error(result.errors);
} else {
console.log('Created a todo with id:', result.data.createTodo.id);
}
}
}
importTodos().catch((e) => console.error(e));