Skip to content

Commit

Permalink
Use gql-query-builder to generate GraphQL queries [WEB]
Browse files Browse the repository at this point in the history
  • Loading branch information
atulmy committed Oct 1, 2018
1 parent cd3f625 commit 43c5116
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 50 deletions.
5 changes: 5 additions & 0 deletions code/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cookie-parser": "^1.4.3",
"dotenv": "^6.0.0",
"express": "^5.0.0-alpha.6",
"gql-query-builder": "1.0.5",
"js-cookie": "^2.2.0",
"morgan": "1.9.1",
"prop-types": "^15.6.0",
Expand Down
2 changes: 1 addition & 1 deletion code/web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<!-- JS Bundle -->
<script type="text/javascript" src="http://localhost:3000/js/bundles/vendor.js"></script>
<script type="text/javascript" src="http://localhost:3000/js/bundles/app.js?0.5392168268749451"></script>
<script type="text/javascript" src="http://localhost:3000/js/bundles/app.js?0.12702802820390224"></script>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker
Expand Down
2 changes: 1 addition & 1 deletion code/web/src/modules/crate/api/actions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Imports
import axios from 'axios'
import queryBuilder from 'gql-query-builder'

// App Imports
import { routeApi } from '../../../setup/routes'
import { queryBuilder } from '../../../setup/helpers'

// Actions Types
export const CRATES_GET_LIST_REQUEST = 'CRATES/GET_LIST_REQUEST'
Expand Down
2 changes: 1 addition & 1 deletion code/web/src/modules/product/api/actions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Imports
import axios from 'axios'
import queryBuilder from 'gql-query-builder'

// App Imports
import { routeApi } from '../../../setup/routes'
import { queryBuilder } from '../../../setup/helpers'

// Actions Types
export const PRODUCTS_GET_LIST_REQUEST = 'PRODUCTS/GET_LIST_REQUEST'
Expand Down
2 changes: 1 addition & 1 deletion code/web/src/modules/subscription/api/actions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Imports
import axios from 'axios'
import queryBuilder from 'gql-query-builder'

// App Imports
import { routeApi } from '../../../setup/routes'
import { queryBuilder } from '../../../setup/helpers'

// Actions Types
export const SUBSCRIPTIONS_GET_LIST_REQUEST = 'SUBSCRIPTIONS/GET_LIST_REQUEST'
Expand Down
4 changes: 2 additions & 2 deletions code/web/src/modules/user/api/actions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Imports
import axios from 'axios'
import queryBuilder from 'gql-query-builder'
import cookie from 'js-cookie'

// App Imports
import { routeApi } from '../../../setup/routes'
import { queryBuilder } from '../../../setup/helpers'
import cookie from 'js-cookie'

// Actions Types
export const LOGIN_REQUEST = 'AUTH/LOGIN_REQUEST'
Expand Down
44 changes: 0 additions & 44 deletions code/web/src/setup/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,3 @@ export function slug(text) {
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}

// GraphQL Query Builder
export function queryBuilder(options) {
options.type = options.type ? options.type : 'query'
options.operation = options.operation ? options.operation : ''
options.fields = options.fields ? options.fields : []
options.data = options.data ? options.data : {}
options.variables = options.variables ? options.variables : {}

const query = {
query: `
${ options.type } ${ queryDataArgumentAndTypeMap(options.data) } {
${ options.operation } ${ queryDataNameAndArgumentMap(options.data) } {
${ options.fields.join(',') }
}
}`,
variables: Object.assign(options.data, options.variables)
}

return query
}

// Private - Convert object to name and argument map eg: (id: $id)
function queryDataNameAndArgumentMap(data) {
return Object.keys(data).length ? `(${ Object.keys(data).reduce((dataString, key, i) => `${ dataString }${ i !== 0 ? ', ' : '' }${ key }: $${ key }`, '') })` : ''
}

// Private - Convert object to argument and type map eg: ($id: Int)
function queryDataArgumentAndTypeMap(data) {
return Object.keys(data).length ? `(${ Object.keys(data).reduce((dataString, key, i) => `${ dataString }${ i !== 0 ? ', ' : '' }$${ key }: ${ queryDataType(data[key]) }`, '') })` : ''
}

// Private - Get GraphQL equivalent type of data passed (String, Int, Float, Boolean)
function queryDataType(data) {
switch (typeof data) {
case 'boolean':
return 'Boolean'
case 'number':
return (data % 1 === 0) ? 'Int' : 'Float'
case 'string':
default:
return 'String'
}
}

0 comments on commit 43c5116

Please sign in to comment.