@croquiscom/crary-graphql
Advanced tools
Comparing version 0.3.1 to 0.3.2
import { GraphQLInputType, GraphQLResolveInfo } from 'graphql'; | ||
export declare function addArgumentToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, value: any, type: GraphQLInputType, path?: string): T; | ||
export interface IAddArgumentToInfoOptions { | ||
path?: string; | ||
as_value?: boolean; | ||
} | ||
export declare function addArgumentToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, value: any, type: GraphQLInputType, options?: IAddArgumentToInfoOptions): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const graphql_1 = require("graphql"); | ||
function valueToNode(value, type) { | ||
if (type instanceof graphql_1.GraphQLNonNull) { | ||
return valueToNode(value, type.ofType); | ||
} | ||
else if (type instanceof graphql_1.GraphQLList) { | ||
if (!Array.isArray(value)) { | ||
throw new Error('Value is not array'); | ||
} | ||
return { | ||
kind: graphql_1.Kind.LIST, | ||
values: value.map((item) => valueToNode(item, type.ofType)), | ||
}; | ||
} | ||
else { | ||
let kind; | ||
if (type === graphql_1.GraphQLInt) { | ||
kind = graphql_1.Kind.INT; | ||
} | ||
else if (type === graphql_1.GraphQLFloat) { | ||
kind = graphql_1.Kind.FLOAT; | ||
} | ||
else if (type === graphql_1.GraphQLBoolean) { | ||
kind = graphql_1.Kind.BOOLEAN; | ||
} | ||
else if (type instanceof graphql_1.GraphQLEnumType) { | ||
kind = graphql_1.Kind.ENUM; | ||
} | ||
else { | ||
throw new Error('unknow type: ' + type.toString()); | ||
} | ||
return { | ||
kind, | ||
value, | ||
}; | ||
} | ||
} | ||
function addArgumentToFieldNodeAsValue(fieldNode, path, name, value, type) { | ||
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: valueToNode(value, type), | ||
}, | ||
] }); | ||
} | ||
if (!fieldNode.selectionSet) { | ||
return fieldNode; | ||
} | ||
const selections = fieldNode.selectionSet.selections.map((selection) => { | ||
if (selection.kind === 'Field' && selection.name.value === path[0]) { | ||
return addArgumentToFieldNodeAsValue(selection, path.slice(1), name, value, type); | ||
} | ||
return selection; | ||
}); | ||
return Object.assign({}, fieldNode, { selectionSet: Object.assign({}, fieldNode.selectionSet, { selections }) }); | ||
} | ||
function typeToAst(type) { | ||
@@ -65,24 +126,35 @@ if (type instanceof graphql_1.GraphQLNonNull) { | ||
} | ||
function addArgumentToInfo(info, name, value, type, path) { | ||
const variable_name = `_c_${name}`; | ||
const variableDefinitions = [ | ||
...(info.operation.variableDefinitions || []), | ||
{ | ||
kind: graphql_1.Kind.VARIABLE_DEFINITION, | ||
type: typeToAst(type), | ||
variable: { | ||
kind: graphql_1.Kind.VARIABLE, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: variable_name, | ||
function addArgumentToInfo(info, name, value, type, options = {}) { | ||
if (options.as_value) { | ||
const path = options.path ? options.path.split('.') : []; | ||
const fieldNode = addArgumentToFieldNodeAsValue(info.fieldNodes[0], path, name, value, type); | ||
return Object.assign({}, info, { fieldNodes: [fieldNode], operation: Object.assign({}, info.operation, { selectionSet: { | ||
kind: graphql_1.Kind.SELECTION_SET, | ||
selections: [fieldNode], | ||
} }) }); | ||
} | ||
else { | ||
const variable_name = `_c_${name}`; | ||
const variableDefinitions = [ | ||
...(info.operation.variableDefinitions || []), | ||
{ | ||
kind: graphql_1.Kind.VARIABLE_DEFINITION, | ||
type: typeToAst(type), | ||
variable: { | ||
kind: graphql_1.Kind.VARIABLE, | ||
name: { | ||
kind: graphql_1.Kind.NAME, | ||
value: variable_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, { [variable_name]: value }) }); | ||
]; | ||
const path = options.path ? options.path.split('.') : []; | ||
const fieldNode = addArgumentToFieldNode(info.fieldNodes[0], path, 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, { [variable_name]: value }) }); | ||
} | ||
} | ||
exports.addArgumentToInfo = addArgumentToInfo; |
import { GraphQLResolveInfo } from 'graphql'; | ||
export declare function addFieldToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, path?: string): T; | ||
export interface IAddFieldToInfoOptions { | ||
path?: string; | ||
} | ||
export declare function addFieldToInfo<T extends GraphQLResolveInfo = GraphQLResolveInfo>(info: T, name: string, options?: IAddFieldToInfoOptions): T; |
@@ -35,4 +35,5 @@ "use strict"; | ||
} | ||
function addFieldToInfo(info, name, path) { | ||
const fieldNode = addFieldToFieldNode(info.fieldNodes[0], path ? path.split('.') : [], name); | ||
function addFieldToInfo(info, name, options = {}) { | ||
const path = options.path ? options.path.split('.') : []; | ||
const fieldNode = addFieldToFieldNode(info.fieldNodes[0], path, name); | ||
return Object.assign({}, info, { fieldNodes: [fieldNode], operation: Object.assign({}, info.operation, { selectionSet: { | ||
@@ -39,0 +40,0 @@ kind: graphql_1.Kind.SELECTION_SET, |
import { GraphQLInputType, GraphQLResolveInfo, GraphQLSchema } from 'graphql'; | ||
import { addArgumentToInfo } from './addArgumentToInfo'; | ||
import { addArgumentToInfo, IAddArgumentToInfoOptions } from './addArgumentToInfo'; | ||
export { addArgumentToInfo }; | ||
import { addFieldToInfo } from './addFieldToInfo'; | ||
import { addFieldToInfo, IAddFieldToInfoOptions } from './addFieldToInfo'; | ||
export { addFieldToInfo }; | ||
@@ -13,4 +13,4 @@ import { conformInfoToSchema } from './conformInfoToSchema'; | ||
export interface IGraphQLResolveInfoMethods { | ||
addArgument(name: string, value: any, type: GraphQLInputType, path?: string): this; | ||
addField(name: string, path?: string): this; | ||
addArgument(name: string, value: any, type: GraphQLInputType, options?: IAddArgumentToInfoOptions): this; | ||
addField(name: string, options?: IAddFieldToInfoOptions): this; | ||
removeArgument(name: string): this; | ||
@@ -17,0 +17,0 @@ conformToSchema(schema: GraphQLSchema, fragments?: Array<{ |
@@ -17,4 +17,4 @@ "use strict"; | ||
function wrapInfo(info) { | ||
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); }, | ||
return Object.assign({}, info, { addArgument(name, value, type, options) { return addArgumentToInfo_1.addArgumentToInfo(this, name, value, type, options); }, | ||
addField(name, options) { return addFieldToInfo_1.addFieldToInfo(this, name, options); }, | ||
removeArgument(name) { return removeArgumentFromInfo_1.removeArgumentFromInfo(this, name); }, | ||
@@ -21,0 +21,0 @@ conformToSchema(schema, fragments) { return conformInfoToSchema_1.conformInfoToSchema(this, schema, fragments); } }); |
import { GraphQLField, GraphQLInputType } from 'graphql'; | ||
export declare function hookResolverAddArgument(field: GraphQLField<any, any>, name: string, value: any, type: GraphQLInputType, path?: string): void; | ||
export declare function hookResolverAddArgument(field: GraphQLField<any, any>, name: string, value: any, type: GraphQLInputType, options?: { | ||
path?: string; | ||
}): void; |
@@ -5,6 +5,6 @@ "use strict"; | ||
const info_1 = require("../info"); | ||
function hookResolverAddArgument(field, name, value, type, path) { | ||
function hookResolverAddArgument(field, name, value, type, options) { | ||
const { resolve = graphql_1.defaultFieldResolver } = field; | ||
field.resolve = async (source, args, context, info) => { | ||
info = info_1.addArgumentToInfo(info, name, value, type, path); | ||
info = info_1.addArgumentToInfo(info, name, value, type, options); | ||
return resolve(source, args, context, info); | ||
@@ -11,0 +11,0 @@ }; |
{ | ||
"name": "@croquiscom/crary-graphql", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Croquis's library - graphql extension", | ||
@@ -40,3 +40,3 @@ "main": "./lib", | ||
}, | ||
"gitHead": "18a79f16db9606942f0272a4b5432896d97ec208" | ||
"gitHead": "04aaad960c3d5b9f399680db2e45bdace61fa82b" | ||
} |
29426
663