Archen is a simple, flexible and fast GraphQL library written in Typescript.
$ npm install archen
Archen is ridiculously easy to use. In the simplest form, it requires nothing more than the details to connect to an existing database. Below is an example to add GraphQL API to a MySQL database:
const { Archen } = require('archen');
const archen = new Archen({
database: {
connection: {
dialect: 'postgres',
connection: {
user: 'root',
password: 'secret',
database: 'example'
}
}
}
});
await archen.bootstrap();
const source = `
query {
users {
id
email
}
}
`;
const result = await archen.query({ source });
// console.log(result.users);See example folder for a demo app using Next.js.
Archen can be configured using an ArchenConfig object.
By default, archen exports all models and fields to the GraphQL API. To stop a model from being exported, add an entry in graphql.models field and set it to false. The following config shows how to export all models except for User:
{
graphql: {
models: {
User: false
}
}
}Setting graphql.allowAll to false stops all models being exported to the API by default. The following config exports only Product and Category to the API:
{
graphql: {
allowAll: false,
models: {
Product: true,
Category: true
}
}
}The following config forbids creating User objects via the generated API:
{
graphql: {
models: {
User: {
create: false,
},
}
}
}# Test for SQLite
$ DB_TYPE=sqlite3 npm run test
# Test for Postgres
$ DB_TYPE=postgres DB_USER=postgres npm run test
# Test for MySQL
$ DB_TYPE=mysql DB_USER=root DB_PASS=secret npm run test