Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { onUser, onProject } from './nodes'
import { User, Project } from './queries'

interface INode {
user: (...args: any) => string,
project: (...args: any) => string
}

interface IQuery {
user: (...args: any) => string,
project: (...args: any) => string
}
export default class QueryBuilder {
private _query: string
private _nodeTitleMap: INode = {
user: onUser,
project: onProject
}

private _queryTitleMap: IQuery = {
user: User,
project: Project
}

on (node: keyof INode, params: Record<string, string | number>) {
const args = Object.entries(params).map(([, val]) => val)
const nodeFragment = this._nodeTitleMap[node](...args)
const regex = new RegExp(node, 'g')

this._query = this._query.replace(regex, nodeFragment)
return this
}

get query () {
return this._query
}

request (query: keyof IQuery, params: Record<string, string | number>) {
const args = Object.entries(params).map(([, val]) => val)

this._query = this._queryTitleMap[query](...args)
return this
}
}

const QB = new QueryBuilder()

const query = QB.request('user', {
fields: 'id email age project',
login: 'ioedeveloper'
}).on('project', {
fields: 'name dateCreated'
}).query

console.log('query: ', query)