odl-graphql-client
Advanced tools
Comparing version 1.0.0-rc.4 to 1.0.0-rc.5
@@ -1,7 +0,7 @@ | ||
import upperFirst from 'lodash/fp/upperFirst'; | ||
import upperFirst from "lodash/fp/upperFirst"; | ||
import { | ||
parseBodyQueryVariable, | ||
convertSelectFieldsArrayToString, | ||
} from '../util'; | ||
import {MutationOperation, MutationVariables} from '../types'; | ||
} from "../util"; | ||
import { MutationOperation, MutationVariables } from "../types"; | ||
@@ -12,7 +12,7 @@ export default class MutationBuilder { | ||
case MutationOperation.Add: | ||
return 'add'; | ||
return "add"; | ||
case MutationOperation.Update: | ||
return 'update'; | ||
return "update"; | ||
case MutationOperation.Delete: | ||
return 'delete'; | ||
return "delete"; | ||
} | ||
@@ -26,7 +26,7 @@ }; | ||
selectedFields?: string[], | ||
id?: string, | ||
id?: string | ||
) => { | ||
const mutationVariables = this.getMutationVariables(payloadModel, id); | ||
let {inputVariables, declareVariables} = parseBodyQueryVariable( | ||
mutationVariables, | ||
let { inputVariables, declareVariables } = parseBodyQueryVariable( | ||
mutationVariables | ||
); | ||
@@ -36,4 +36,4 @@ | ||
declareVariables = declareVariables.replace( | ||
'Input!', | ||
`${upperFirst(entityName)}Input!`, | ||
"Input!", | ||
`${upperFirst(entityName)}Input!` | ||
); | ||
@@ -52,5 +52,27 @@ const operationName = this.getOperationName(operation); | ||
buildCustomMutationOperation = ( | ||
entityName: string, | ||
operationName: string, | ||
payloadModel: any, | ||
onGetVariableType: (variableKey: string) => string | ||
) => { | ||
const mutationVariables = this.getMutationVariables(payloadModel); | ||
let { inputVariables, declareVariables } = parseBodyQueryVariable( | ||
mutationVariables, | ||
onGetVariableType | ||
); | ||
return `mutation${declareVariables} { | ||
${entityName} { | ||
${operationName}${inputVariables} { | ||
didSuccess | ||
errorCode | ||
} | ||
} | ||
}`; | ||
}; | ||
getMutationVariables = ( | ||
payloadModel?: any, | ||
id?: string, | ||
id?: string | ||
): MutationVariables => { | ||
@@ -57,0 +79,0 @@ let mutationVariables: MutationVariables = {}; |
96
index.ts
@@ -1,5 +0,5 @@ | ||
import {GraphQLClient} from 'graphql-request'; | ||
import omit from 'lodash/fp/omit'; | ||
import set from 'lodash/fp/set'; | ||
import {QueryRequestBuilder, MutationRequestBuilder} from './builders'; | ||
import { GraphQLClient } from "graphql-request"; | ||
import omit from "lodash/fp/omit"; | ||
import set from "lodash/fp/set"; | ||
import { QueryRequestBuilder, MutationRequestBuilder } from "./builders"; | ||
import { | ||
@@ -10,3 +10,3 @@ QueryParams, | ||
ODLGraphQLClientOptions, | ||
} from './types'; | ||
} from "./types"; | ||
@@ -26,5 +26,5 @@ export default class ODLGraphqlClient { | ||
endpoint: string, | ||
options?: ODLGraphQLClientOptions, | ||
options?: ODLGraphQLClientOptions | ||
) => { | ||
const {authenticationToken} = options || {}; | ||
const { authenticationToken } = options || {}; | ||
@@ -34,4 +34,4 @@ let clientOption = {}; | ||
clientOption = set( | ||
'headers.authorization', | ||
`Bearer ${authenticationToken}`, | ||
"headers.authorization", | ||
`Bearer ${authenticationToken}` | ||
)(clientOption); | ||
@@ -49,3 +49,3 @@ } | ||
defaultvalue: any, | ||
id?: string, | ||
id?: string | ||
) => { | ||
@@ -62,3 +62,3 @@ const queryVariables = this.queryBuilder.getQueryVariables(queryParams, id); | ||
operationName, | ||
defaultvalue, | ||
defaultvalue | ||
); | ||
@@ -71,12 +71,14 @@ }); | ||
query: string, | ||
operation: MutationOperation, | ||
operation: MutationOperation | string, | ||
payloadModel: any, | ||
defaultvalue: any, | ||
id?: string, | ||
id?: string | ||
) => { | ||
const mutationVariables = this.mutationBuilder.getMutationVariables( | ||
payloadModel, | ||
id, | ||
id | ||
); | ||
const operationName = this.mutationBuilder.getOperationName(operation); | ||
const operationName = | ||
this.mutationBuilder.getOperationName(operation as MutationOperation) || | ||
operation; | ||
@@ -89,4 +91,4 @@ return this.graphQLClient | ||
response, | ||
operationName, | ||
defaultvalue, | ||
operationName as string, | ||
defaultvalue | ||
); | ||
@@ -99,3 +101,3 @@ }); | ||
queryParams: QueryParams, | ||
selectFields?: string[], | ||
selectFields?: string[] | ||
) => { | ||
@@ -106,3 +108,3 @@ const query = this.queryBuilder.buildQuery( | ||
queryParams, | ||
selectFields, | ||
selectFields | ||
); | ||
@@ -115,3 +117,3 @@ | ||
queryParams, | ||
[], | ||
[] | ||
); | ||
@@ -123,3 +125,3 @@ }; | ||
queryParams: QueryParams, | ||
selectFields?: string[], | ||
selectFields?: string[] | ||
) => { | ||
@@ -130,3 +132,3 @@ const query = this.queryBuilder.buildQuery( | ||
queryParams, | ||
selectFields, | ||
selectFields | ||
); | ||
@@ -139,3 +141,3 @@ | ||
queryParams, | ||
{}, | ||
{} | ||
); | ||
@@ -148,3 +150,3 @@ }; | ||
queryParams: QueryParams, | ||
selectFields?: string[], | ||
selectFields?: string[] | ||
) => { | ||
@@ -156,3 +158,3 @@ const query = this.queryBuilder.buildQuery( | ||
selectFields, | ||
id, | ||
id | ||
); | ||
@@ -166,3 +168,3 @@ | ||
{}, | ||
id, | ||
id | ||
); | ||
@@ -174,3 +176,3 @@ }; | ||
return this.graphQLClient | ||
.request(queryBody, {query}) | ||
.request(queryBody, { query }) | ||
.then((response: any) => { | ||
@@ -182,3 +184,3 @@ return this.queryBuilder.compactCountResponse(entityName, response); | ||
addAsync = (entityName: string, model: any, selectFields?: string[]) => { | ||
const formattedModel = omit(['id'])(model); | ||
const formattedModel = omit(["id"])(model); | ||
const mutation = this.mutationBuilder.build( | ||
@@ -188,3 +190,3 @@ entityName, | ||
formattedModel, | ||
selectFields, | ||
selectFields | ||
); | ||
@@ -197,3 +199,3 @@ | ||
formattedModel, | ||
{}, | ||
{} | ||
); | ||
@@ -206,3 +208,3 @@ }; | ||
model: any, | ||
selectFields?: string[], | ||
selectFields?: string[] | ||
) => { | ||
@@ -214,3 +216,3 @@ const mutation = this.mutationBuilder.build( | ||
selectFields, | ||
id, | ||
id | ||
); | ||
@@ -223,3 +225,3 @@ return this._executeMutationAsync( | ||
{}, | ||
id, | ||
id | ||
); | ||
@@ -233,7 +235,29 @@ }; | ||
undefined, | ||
['id', 'code'], | ||
id, | ||
["id", "code"], | ||
id | ||
); | ||
return this.graphQLClient.request(mutation, {id}); | ||
return this.graphQLClient.request(mutation, { id }); | ||
}; | ||
executeCustomMutationAsync = ( | ||
entityName: string, | ||
mutationName: string, | ||
payload: any, | ||
onGetVariableType: (variableKey: string) => string | ||
) => { | ||
const mutation = this.mutationBuilder.buildCustomMutationOperation( | ||
entityName, | ||
mutationName, | ||
payload, | ||
onGetVariableType | ||
); | ||
return this._executeMutationAsync( | ||
entityName, | ||
mutation, | ||
mutationName, | ||
payload, | ||
{} | ||
); | ||
}; | ||
} |
{ | ||
"name": "odl-graphql-client", | ||
"repository": { | ||
"url": "https://github.com/hieutran3010/odl-graphql-client", | ||
"url": "git+https://github.com/hieutran3010/odl-graphql-client", | ||
"type": "git" | ||
}, | ||
"version": "1.0.0-rc.4", | ||
"version": "1.0.0-rc.5", | ||
"dependencies": { | ||
"graphql-request": "^1.8.2" | ||
"graphql-request": "^1.8.2", | ||
"lodash": "^4.17.15" | ||
} | ||
} |
@@ -45,1 +45,6 @@ export type QueryParams = { | ||
}; | ||
export type MutationResult = { | ||
didSuccess: boolean; | ||
errorCode: string; | ||
}; |
35
util.ts
@@ -1,7 +0,7 @@ | ||
import keys from 'lodash/fp/keys'; | ||
import isEmpty from 'lodash/fp/isEmpty'; | ||
import isNil from 'lodash/fp/isNil'; | ||
import isUndefined from 'lodash/fp/isUndefined'; | ||
import get from 'lodash/fp/get'; | ||
import {SUPPORTED_VARIABLES_TYPE} from './constants'; | ||
import keys from "lodash/fp/keys"; | ||
import isEmpty from "lodash/fp/isEmpty"; | ||
import isNil from "lodash/fp/isNil"; | ||
import isUndefined from "lodash/fp/isUndefined"; | ||
import get from "lodash/fp/get"; | ||
import { SUPPORTED_VARIABLES_TYPE } from "./constants"; | ||
import { | ||
@@ -12,6 +12,7 @@ GraphQLVariable, | ||
CountQueryVariables, | ||
} from './types'; | ||
} from "./types"; | ||
export const parseBodyQueryVariable = ( | ||
variables: QueryVariables | MutationVariables | CountQueryVariables, | ||
onGetVariableType?: (variableKey: string) => string | ||
): GraphQLVariable => { | ||
@@ -21,3 +22,4 @@ const variableKeys = keys(variables); | ||
const declareVariables = variableKeys.map((paramKey) => { | ||
const paramType = get(paramKey)(SUPPORTED_VARIABLES_TYPE); | ||
const getVariableTypeFunc = onGetVariableType || getVariableType; | ||
const paramType = getVariableTypeFunc(paramKey); | ||
return `$${paramKey}: ${paramType}`; | ||
@@ -27,13 +29,13 @@ }); | ||
const inputVariables = variableKeys.map( | ||
(paramKey) => `${paramKey}: $${paramKey}`, | ||
(paramKey) => `${paramKey}: $${paramKey}` | ||
); | ||
return { | ||
declareVariables: `(${declareVariables.join(',')})`, | ||
inputVariables: `(${inputVariables.join(',')})`, | ||
declareVariables: `(${declareVariables.join(",")})`, | ||
inputVariables: `(${inputVariables.join(",")})`, | ||
}; | ||
} | ||
return { | ||
declareVariables: '', | ||
inputVariables: '', | ||
declareVariables: "", | ||
inputVariables: "", | ||
}; | ||
@@ -48,5 +50,8 @@ }; | ||
) { | ||
return ''; | ||
return ""; | ||
} | ||
return selectFields.join(','); | ||
return selectFields.join(","); | ||
}; | ||
const getVariableType = (paramKey: string) => | ||
get(paramKey)(SUPPORTED_VARIABLES_TYPE); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16957
496
2
+ Addedlodash@^4.17.15
+ Addedlodash@4.17.21(transitive)