-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
createCollectionRelationTailGqlFieldEntries.ts
64 lines (58 loc) · 2.6 KB
/
createCollectionRelationTailGqlFieldEntries.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
import { GraphQLFieldConfig } from 'graphql'
import { ObjectType, Collection, Relation } from '../../../interface'
import { formatName, scrib } from '../../utils'
import BuildToken from '../BuildToken'
import getCollectionGqlType from './getCollectionGqlType'
/**
* Creates the fields for which the collection argument is the tail.
* These fields will fetch a value from the head collection.
*/
// TODO: test
export default function createCollectionRelationTailGqlFieldEntries (
buildToken: BuildToken,
collection: Collection,
options: {
getCollectionValue?: (source: mixed) => ObjectType.Value,
getFieldName?: <TKey>(relation: Relation<TKey>, collection: Collection) => string,
} = {},
): Array<[string, GraphQLFieldConfig<mixed, ObjectType.Value>]> {
const { inventory } = buildToken
// Some tests may choose to not include the inventory. If this is the case,
// just return an empty array.
if (!inventory) return []
const collectionGqlType = getCollectionGqlType(buildToken, collection)
return (
// Add all of our many-to-one relations (aka tail relations).
inventory.getRelations()
// We only want the relations for which this collection is the tail
// collection and whose `headCollectionKey` have a `read`
// implementation.
.filter(relation =>
relation.tailCollection === collection &&
relation.headCollectionKey.read != null
)
// Transform the relation into a field entry.
.map(<THeadValue, TKey>(relation: Relation<TKey>): [string, GraphQLFieldConfig<ObjectType.Value, ObjectType.Value>] => {
const headCollectionKey = relation.headCollectionKey
const headCollection = headCollectionKey.collection
const headCollectionGqlType = getCollectionGqlType(buildToken, headCollection)
return [
options.getFieldName
? options.getFieldName(relation, collection)
: formatName.field(`${headCollection.type.name}-by-${relation.name}`),
{
description: `Reads a single ${scrib.type(headCollectionGqlType)} that is related to this \`${collectionGqlType}\`.`,
type: headCollectionGqlType,
async resolve (source, args, context): Promise<ObjectType.Value | undefined> {
const value = options.getCollectionValue ? options.getCollectionValue(source) : source
const key = relation.getHeadKeyFromTailValue(value)
const headValue = await headCollectionKey.read!(context, key)
if (headValue == null)
return
return headValue
},
},
]
})
)
}