-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfield.ts
79 lines (73 loc) · 2.04 KB
/
field.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
77
78
79
import {
GraphQLInt,
GraphQLList,
GraphQLScalarType,
GraphQLString,
GraphQLType,
} from 'graphql'
import { Association } from 'sequelize/types'
import { injectAssociations } from '..'
import { OutputTypes } from '../../types'
export default function generateAssociationField(
relation: Association,
types: OutputTypes,
graphqlSchemaDeclaration?: any,
models?: any,
globalPreCallback?: any,
resolver = null
): {
type: GraphQLList<GraphQLType>
isDeprecated: boolean
associationsInjected: boolean
name: string
args: {
name: string
type: GraphQLScalarType
}[]
resolve?: any
} {
const newBaseType =
graphqlSchemaDeclaration &&
// @ts-ignore
!types[relation.target.name].associationsInjected
? injectAssociations(
types[relation.target.name],
graphqlSchemaDeclaration,
types,
models,
globalPreCallback
)
: types[relation.target.name]
const type =
relation.associationType === 'BelongsToMany' ||
relation.associationType === 'HasMany'
? new GraphQLList(newBaseType)
: newBaseType
const field: any = {
type,
isDeprecated: false,
associationsInjected: true,
name: relation.as,
args: [
{
// An arg with the key order will automatically be converted to a order on the target
name: 'order',
type: GraphQLString,
},
],
}
if (relation.associationType === 'HasMany') {
// Limit and offset will only work for HasMany relation ship
// Having the limit on the include will trigger a "Only HasMany associations support include.separate" error.
// While sequelize N:M associations are not supported with hasMany. So BelongsToMany relationships
// cannot be limited in a subquery. If you want to query them, make a custom resolver, or create a view.
field.args.push({ name: 'limit', type: GraphQLInt })
field.args.push({ name: 'offset', type: GraphQLInt })
}
if (resolver) {
// @ts-ignore
field.resolve = resolver
}
// @ts-ignore
return field
}