-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
utils.ts
76 lines (67 loc) · 2.24 KB
/
utils.ts
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
import slugify from "slugify"
import _ from "lodash"
import { lexicographicSortSchema } from "graphql"
import type { GraphQLSchema } from "graphql"
import { IDefinitionMeta } from "../../redux/types"
type DefinitionName = string
type DefinitionMap = Map<DefinitionName, IDefinitionMeta>
/**
* Makes the schema deterministic by sorting it (so on new saves the whole file doesn't change, only the change that was made). It can be used for e.g. tests when two schema diffs should be compared.
*/
export function stabilizeSchema(schema: GraphQLSchema): GraphQLSchema {
return lexicographicSortSchema(schema)
}
export function sortDefinitions(
a: IDefinitionMeta,
b: IDefinitionMeta
): number {
return a.name.localeCompare(b.name)
}
/**
* Internally in Gatsby we use the function generateQueryName:
* packages/gatsby/src/query/file-parser.js
* This function re-implements this partially to guess if a query is unnamed
*/
function guessIfUnnnamedQuery({
isStaticQuery,
name,
filePath,
}: IDefinitionMeta): boolean {
const queryType = isStaticQuery ? `static` : `page`
const generatedQueryName = slugify(filePath, {
replacement: ` `,
lower: false,
})
const pattern = _.camelCase(`${queryType}-${generatedQueryName}`)
return name.startsWith(pattern)
}
function guessIfThirdpartyDefinition({ filePath }: IDefinitionMeta): boolean {
return /(node_modules|\.yarn|\.cache)/.test(filePath)
}
function isFragmentDefinition(def: IDefinitionMeta): boolean {
return def.isFragment
}
function isThirdpartyFragment(def: IDefinitionMeta): boolean {
return isFragmentDefinition(def) && guessIfThirdpartyDefinition(def)
}
/**
* We don't want third-party definitions/queries unless it's a fragment.
* We also don't want unnamed queries ending up in the TS types.
*/
function isTargetDefinition(def: IDefinitionMeta): boolean {
if (isThirdpartyFragment(def)) {
return true
}
return !(guessIfThirdpartyDefinition(def) || guessIfUnnnamedQuery(def))
}
export function filterTargetDefinitions(
defMap: DefinitionMap
): Map<string, IDefinitionMeta> {
const defs: Array<[name: string, def: IDefinitionMeta]> = []
for (const [name, def] of defMap) {
if (isTargetDefinition(def)) {
defs.push([name, def])
}
}
return new Map(defs)
}