@croquiscom/crary-graphql
Advanced tools
Comparing version 0.2.2 to 0.3.0
import { GraphQLInputType, GraphQLResolveInfo } from 'graphql'; | ||
export declare function addArgumentToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, value: any, type: GraphQLInputType): T; | ||
export declare function addArgumentToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, value: any, type: GraphQLInputType, path?: string): T; |
@@ -34,4 +34,35 @@ "use strict"; | ||
} | ||
function addArgumentToInfo(info, name, value, type) { | ||
const varaiable_name = `_c_${name}`; | ||
function addArgumentToFieldNode(fieldNode, path, name, variable_name) { | ||
if (path.length === 0) { | ||
return Object.assign({}, fieldNode, { arguments: [ | ||
...(fieldNode.arguments || []), | ||
{ | ||
kind: graphql_1.Kind.ARGUMENT, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: name, | ||
}, | ||
value: { | ||
kind: graphql_1.Kind.VARIABLE, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: variable_name, | ||
}, | ||
}, | ||
}, | ||
] }); | ||
} | ||
if (!fieldNode.selectionSet) { | ||
return fieldNode; | ||
} | ||
const selections = fieldNode.selectionSet.selections.map((selection) => { | ||
if (selection.kind === 'Field' && selection.name.value === path[0]) { | ||
return addArgumentToFieldNode(selection, path.slice(1), name, variable_name); | ||
} | ||
return selection; | ||
}); | ||
return Object.assign({}, fieldNode, { selectionSet: Object.assign({}, fieldNode.selectionSet, { selections }) }); | ||
} | ||
function addArgumentToInfo(info, name, value, type, path) { | ||
const variable_name = `_c_${name}`; | ||
const variableDefinitions = [ | ||
@@ -46,3 +77,3 @@ ...(info.operation.variableDefinitions || []), | ||
kind: graphql_1.Kind.NAME, | ||
value: varaiable_name, | ||
value: variable_name, | ||
}, | ||
@@ -52,24 +83,8 @@ }, | ||
]; | ||
const fieldNode = Object.assign({}, info.fieldNodes[0], { arguments: [ | ||
...(info.fieldNodes[0].arguments || []), | ||
{ | ||
kind: graphql_1.Kind.ARGUMENT, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: name, | ||
}, | ||
value: { | ||
kind: graphql_1.Kind.VARIABLE, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: varaiable_name, | ||
}, | ||
}, | ||
}, | ||
] }); | ||
const fieldNode = addArgumentToFieldNode(info.fieldNodes[0], path ? path.split('.') : [], name, variable_name); | ||
return Object.assign({}, info, { fieldNodes: [fieldNode], operation: Object.assign({}, info.operation, { selectionSet: { | ||
kind: graphql_1.Kind.SELECTION_SET, | ||
selections: [fieldNode], | ||
}, variableDefinitions }), variableValues: Object.assign({}, info.variableValues, { [varaiable_name]: value }) }); | ||
}, variableDefinitions }), variableValues: Object.assign({}, info.variableValues, { [variable_name]: value }) }); | ||
} | ||
exports.addArgumentToInfo = addArgumentToInfo; |
import { GraphQLInputType, GraphQLResolveInfo, GraphQLSchema } from 'graphql'; | ||
import { addArgumentToInfo } from './addArgumentToInfo'; | ||
export { addArgumentToInfo }; | ||
import { addFieldToInfo } from './addFieldToInfo'; | ||
export { addFieldToInfo }; | ||
import { conformInfoToSchema } from './conformInfoToSchema'; | ||
@@ -11,3 +13,4 @@ export { conformInfoToSchema }; | ||
export interface IGraphQLResolveInfoMethods { | ||
addArgument(name: string, value: any, type: GraphQLInputType): this; | ||
addArgument(name: string, value: any, type: GraphQLInputType, path?: string): this; | ||
addField(name: string, path?: string): this; | ||
removeArgument(name: string): this; | ||
@@ -14,0 +17,0 @@ conformToSchema(schema: GraphQLSchema, fragments?: Array<{ |
@@ -5,2 +5,4 @@ "use strict"; | ||
exports.addArgumentToInfo = addArgumentToInfo_1.addArgumentToInfo; | ||
const addFieldToInfo_1 = require("./addFieldToInfo"); | ||
exports.addFieldToInfo = addFieldToInfo_1.addFieldToInfo; | ||
const conformInfoToSchema_1 = require("./conformInfoToSchema"); | ||
@@ -16,3 +18,4 @@ exports.conformInfoToSchema = conformInfoToSchema_1.conformInfoToSchema; | ||
function wrapInfo(info) { | ||
return Object.assign({}, info, { addArgument(name, value, type) { return addArgumentToInfo_1.addArgumentToInfo(this, name, value, type); }, | ||
return Object.assign({}, info, { addArgument(name, value, type, path) { return addArgumentToInfo_1.addArgumentToInfo(this, name, value, type, path); }, | ||
addField(name, path) { return addFieldToInfo_1.addFieldToInfo(this, name, path); }, | ||
removeArgument(name) { return removeArgumentFromInfo_1.removeArgumentFromInfo(this, name); }, | ||
@@ -19,0 +22,0 @@ conformToSchema(schema, fragments) { return conformInfoToSchema_1.conformInfoToSchema(this, schema, fragments); } }); |
@@ -17,6 +17,6 @@ "use strict"; | ||
} | ||
const varaiable_name = arg_to_remove.value.name.value; | ||
const variable_name = arg_to_remove.value.name.value; | ||
const fieldNode = Object.assign({}, info.fieldNodes[0], { arguments: info.fieldNodes[0].arguments.filter((arg) => arg !== arg_to_remove) }); | ||
const variableDefinitions = (info.operation.variableDefinitions || []) | ||
.filter((def) => def.variable.name.value !== varaiable_name); | ||
.filter((def) => def.variable.name.value !== variable_name); | ||
return Object.assign({}, info, { fieldNodes: [fieldNode], operation: Object.assign({}, info.operation, { selectionSet: { | ||
@@ -23,0 +23,0 @@ kind: graphql_1.Kind.SELECTION_SET, |
import { GraphQLField, GraphQLInputType } from 'graphql'; | ||
export declare function hookResolverAddArgument(field: GraphQLField<any, any>, name: string, value: any, type: GraphQLInputType): void; | ||
export declare function hookResolverAddArgument(field: GraphQLField<any, any>, name: string, value: any, type: GraphQLInputType, path?: string): void; |
@@ -5,6 +5,6 @@ "use strict"; | ||
const info_1 = require("../info"); | ||
function hookResolverAddArgument(field, name, value, type) { | ||
function hookResolverAddArgument(field, name, value, type, path) { | ||
const { resolve = graphql_1.defaultFieldResolver } = field; | ||
field.resolve = async (source, args, context, info) => { | ||
info = info_1.addArgumentToInfo(info, name, value, type); | ||
info = info_1.addArgumentToInfo(info, name, value, type, path); | ||
return resolve(source, args, context, info); | ||
@@ -11,0 +11,0 @@ }; |
{ | ||
"name": "@croquiscom/crary-graphql", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Croquis's library - graphql extension", | ||
@@ -34,3 +34,3 @@ "main": "./lib", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.1.4", | ||
"mocha": "^6.2.0", | ||
"rimraf": "^2.6.3", | ||
@@ -41,3 +41,3 @@ "ts-node": "^8.3.0", | ||
}, | ||
"gitHead": "112898db36ddac49f9c500e90bf75d0a296c928f" | ||
"gitHead": "ae9b97fc40baf40ea29eea74cb1f5817cc6e2711" | ||
} |
26907
34
592