Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const $concatArrays = categories.map(cate => `$${cate.toLowerCase()}`)
aggregates.push({ $project: { result: { $concatArrays } } })
aggregates.push({ $sort: { scientificName: -1 } })
const queryResult = await Plant.aggregate(aggregates)
const result = _.flatMap(queryResult, item => item.result)
console.timeEnd('Find plant by category and scientific name')
return {
result: _(result)
.slice(skip, skip + limit),
count: result.length
}
}
})
PlantTC.setResolver('autoCompletion', new Resolver({
name: 'autoCompletion',
args: {
text: { type: 'String', defaultValue: '' }
},
resolve: async ({ args, context }) => {
const { Plant } = context
const test = new RegExp(args.text.split('.*').join('|'), 'ig')
const result = await Plant.find({
$or: [
{ scientificName: test },
{ familyName: test },
{ name: test }
]
})
.limit(40)
return result
const AutoCompleteResultItem = new GraphQLObjectType({
name: 'ScientificNameItem',
fields: {
scientificName: { type: GraphQLString },
familyName: { type: GraphQLString },
name: { type: GraphQLString },
_id: { type: GraphQLString }
}
})
const AutoCompleteResultItemTC = TypeComposer.create(AutoCompleteResultItem)
AutoCompleteResultItemTC.extendField('scientificName', {
resolve: source => (scientificSplit(source.scientificName))
})
const PlantSearchResolver = new Resolver({
name: 'search',
type: new GraphQLObjectType({
name: 'PlantSearchResult',
fields: {
result: { type: new GraphQLList(PlantSearchResultItemType) },
count: { type: 'Int' }
}
}),
args: {
text: { type: '[String]', defaultValue: [] },
categories: { type: new GraphQLList(CategoryEnum), defaultValue: ['garden', 'herbarium', 'museum'] },
skip: { type: 'Int', defaultValue: 0 },
limit: { type: 'Int', defaultValue: 20 }
},
resolve: async ({
args: { text, skip, limit, categories },
const _buildResolverForGqlType = (mysqlConfig, mysqlTableName, gqlType) => {
return new Resolver({
name: [_clearNameForResolver(mysqlTableName)],
type: [gqlType],
args: Object.assign({ _limit: "Int" }, gqlType.getFields()),
resolve: ({ source, args, context, info }) => {
if (!context) {
throw new Error("You must provide a GraphQL context to the server, even if empty (e.g. contextValue: {})")
}
/**
* Use a namespace specific to the current mysqlConfig to avoid collisions in context
*/
const namespace = `gqlComposeMysql${md5(JSON.stringify(mysqlConfig))}`
context[namespace] = !context[namespace] ? {} : context[namespace]
_addKnexInContext(context[namespace], mysqlConfig)
switch (typeof c._args) {
case 'string':
gqlResolverParams.args = nestedInput({
record: schemaComposer
.getTC(c._args)
.getInputTypeComposer()
.getFields()
}, c._name)
break
case 'object':
gqlResolverParams.args = nestedInput(c._args, c._name)
break
}
}
return new GQLResolver(gqlResolverParams)
}