This repository was archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathschemaSearch.js
106 lines (103 loc) · 4.08 KB
/
schemaSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { getFieldDirective } from './utils';
import {
DirectiveDefinition,
getDirective,
getDirectiveArgument
} from './augment/directives';
import { isNodeType, isUnionTypeDefinition } from './augment/types/types';
import { TypeWrappers, unwrapNamedType } from './augment/fields';
import { GraphQLID, GraphQLString } from 'graphql';
import { ApolloError } from 'apollo-server-errors';
const CREATE_NODE_INDEX = `CALL db.index.fulltext.createNodeIndex`;
export const schemaSearch = ({ schema }) => {
let statement = '';
let statements = [];
if (schema) {
const searchFieldTypeMap = mapSearchDirectives({
schema
});
statements = Object.entries(searchFieldTypeMap).map(([name, config]) => {
const { labelMap, properties } = config;
const labels = Object.keys(labelMap);
const labelVariable = JSON.stringify(labels);
const propertyVariable = JSON.stringify(properties);
// create the index anew
return ` ${CREATE_NODE_INDEX}("${name}",${labelVariable},${propertyVariable})`;
});
}
if (statements.length) {
statement = `${statements.join('\n')}
RETURN TRUE`;
}
return statement;
};
export const mapSearchDirectives = ({ schema }) => {
const typeMap = schema ? schema.getTypeMap() : {};
return Object.entries(typeMap).reduce(
(mapped, [typeName, { astNode: definition }]) => {
if (
isNodeType({ definition }) &&
!isUnionTypeDefinition({ definition })
) {
const type = schema.getType(typeName);
const fieldMap = type.getFields();
Object.entries(fieldMap).forEach(([name, field]) => {
const { astNode } = field;
if (astNode) {
const unwrappedType = unwrapNamedType({ type: astNode.type });
const fieldTypeName = unwrappedType.name;
const fieldTypeWrappers = unwrappedType.wrappers;
const directives = astNode.directives;
const directive = getDirective({
directives,
name: DirectiveDefinition.SEARCH
});
if (directive) {
const isStringType = fieldTypeName === GraphQLString.name;
const isIDType = fieldTypeName === GraphQLID.name;
const isListField = fieldTypeWrappers[TypeWrappers.LIST_TYPE];
if (isIDType || isStringType) {
if (!isListField) {
let searchIndexName = getDirectiveArgument({
directive,
name: 'index'
});
if (!searchIndexName) searchIndexName = `${typeName}Search`;
if (!mapped[searchIndexName]) {
mapped[searchIndexName] = {
labelMap: {
[typeName]: true
},
properties: [name]
};
} else {
const indexEntry = mapped[searchIndexName];
const labelMap = indexEntry.labelMap;
const firstLabel = Object.keys(labelMap)[0];
if (labelMap[typeName]) {
mapped[searchIndexName].properties.push(name);
} else {
throw new ApolloError(
`The ${searchIndexName} index on the ${firstLabel} type cannot be used on the ${name} field of the ${typeName} type, because composite search indexes are not yet supported.`
);
}
}
} else {
throw new ApolloError(
`The @search directive on the ${name} field of the ${typeName} type is invalid, because search indexes cannot currently be set for list type fields.`
);
}
} else {
throw new ApolloError(
`The @search directive on the ${name} field of the ${typeName} type is invalid, because search indexes can only be set for String and ID type fields.`
);
}
}
}
});
}
return mapped;
},
{}
);
};