NodeJS
TOC
Add GraphJin
link
Add Graphjin to your Node application. On install it will create a ./config folder with a sample dev.yml and prod.yml config files.
npm i graphjin# When enabled GraphJin runs with production level security defaults.
# For example only queries from saved in the queries folder can be used.
production: trueconst config = { production: true, default_limit: 50 };Using GraphJin
link
import graphjin from "graphjin";
// config can be a filename
const cf = process.env.NODE_ENV === "production" ? "prod.yml" : "dev.yml";
// or config can be an object
// const config = { production: true, default_limit: 50 }
const gj = await graphjin("./config", cf, db);Whats db ?
link
Its the database client. We currently support the following popular clients.
For pg and pg-pool for Postgres and mysql2 for MySQL. We recommend using pooling clients for performance, pg-pool, mysql2 has a createPool() function. ⚠️ We don't support mutations with MySQL and GraphJin requires the db client to support async/await (Promises)
import mysql from "mysql2";
const pool = mysql.createPool({
host: "localhost",
port: "/tmp/mysql.sock",
user: "root",
database: "db",
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
// GraphJin requires the db client to support async/await (Promises)
const db = pool.promise();
// Set GraphJin config db_type to "mysql" for the MySQL client
const graphjinConfig = { production: false, db_type: "mysql" };
// Initialize GraphJin with this config and the MySQL client
const gj = await graphjin("./config", graphjinConfig, db);import PgPool from "pg-pool";
const db = new PgPool({
host: "localhost",
port: 5432,
user: "postgres",
password: "postgres",
database: "42papers-development",
});Your first query
link
The query is the graphql query, the variables are the variables required by this query and the options are things like { userID: 1 } to set the user identifier for the query ($user_id).
const result = await gj.query("query", <variables>, <options>)If you would rather use a .gql or .graphql file for the query place it under ./config/queries and use the queryByName API instead. query name is the filename of the query (minus the extension)
const result = await gj.queryByName("query name", <variables>, <options>)Lets put this all together and query for the full_name and email of a user by his id ($id). Keep in mind you will need to have a users table with full_name and email columns in your database for this to work.
const res = await gj.query(
"query getUser { users(id: $id) { full_name email } }",
{ id: 1 },
{ userID: 1 }
);Alternatively using queryByName
query getUser {
users(id: $id) {
full_name
email
}
}const res = await gj.queryByName("getUser", { id: 1 }, { userID: 1 });Get the result
console.log(res.data());{
"users": {
"full_name": "Andy Anderson",
"email": "andyskates@hotmail.com"
}
}Using subscriptions
link
Did you ever need to have database changes streamed back to you in realtime. For example new sales that happened, comments added to a blog post, new likes that you want to stream back over websockets, whatever. This is not easy to implement efficiently. But with GraphJin its just as easy as making the above query and is designed to be very efficient.
A subscription query is just a normal query with the prefix subscription.
const result = await gj.subscribe("query", <variables>, <options>)Use the subscribe API that works similar to query in production mode
only allows you to use queries from the queries folder.
const res = await gj.subscribe(
"subscription getUpdatedUser { users(id: $userID) { id email } }",
{ id: 1 },
{ userID: 1 }
);Alterntively you can use the subscribeByName API which is similar to the queryByName API.
const res = await gj.subscribeByName(
"getUpdatedUser",
{ id: 1 },
{ userID: 1 }
);Getting the updates back from a subscription is a little different you have to use a callback since the results keep coming.
res.data(function (res1) {
console.log(res1.data());
});{"users":{"email":"user3@test.com","id":3,"phone":null}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0000"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0001"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0002"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0003"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0004"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0005"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0006"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0007"}}
{"users":{"email":"user3@test.com","id":3,"phone":"650-447-0008"}}