-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
156 lines (142 loc) · 7.34 KB
/
index.mjs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function nullableIf(GraphQLNonNull, condition, Type)
{
return condition ? Type : new GraphQLNonNull(Type);
}
/**
*
* @param {Object} options
* @param {function} options.nameFn
* @param {string} options.defaultFormat Default format of the timestamp
* @returns {function(*): *}
*/
export default function(
{
nameFn = (name) => name + 'Formatted',
defaultFormat = 'YYYY-MM-DD"T"HH24:MI:SSOF' // ISO 8601
} = {}
)
{
return function(builder)
{
return builder.hook(
'GraphQLObjectType:fields',
(fields, build, context) =>
{
const {
extend,
getSafeAliasFromAlias,
getSafeAliasFromResolveInfo,
pgSql: sql,
pg2gqlForType,
graphql: { GraphQLNonNull, GraphQLString },
pgColumnFilter,
inflection,
pgOmit: omit,
pgGetSelectValueForFieldAndTypeAndModifier,
describePgEntity,
sqlCommentByAddingTags,
} = build;
const {
scope: { isPgRowType, isPgCompoundType, pgIntrospection: table },
fieldWithHooks,
} = context;
if (
!(isPgRowType || isPgCompoundType) ||
!table ||
table.kind !== 'class'
) {
return fields;
}
return extend(
fields,
table.attributes.reduce((memo, attr) => {
if (!pgColumnFilter(attr, build, context))
return memo;
if (omit(attr, 'read'))
return memo;
if(attr.type.name !== 'timestamptz')
return memo;
// Exclude columns that are primary key
if(table.primaryKeyConstraint && table.primaryKeyConstraint.keyAttributeNums.includes(attr.num))
return memo;
// Exclude columns that are foreign keys
if(table.constraints.some(constraint => constraint.type === 'f' && constraint.keyAttributeNums.includes(attr.num)))
return memo;
const fieldName = nameFn(inflection.column(attr));
memo = extend(
memo,
{
[fieldName]: fieldWithHooks(
fieldName,
fieldContext => {
const { type, typeModifier } = attr;
const sqlColumn = sql.identifier(attr.name);
const { addDataGenerator } = fieldContext;
const ReturnType = GraphQLString;
addDataGenerator(
parsedResolveInfoFragment =>
{
const { args } = parsedResolveInfoFragment;
const tz = args.tz ? sql.value(args.tz) : sql.fragment`current_setting('TimeZone')`;
const format = args.format ? sql.value(args.format) : sql.value(defaultFormat);
return {
pgQuery: queryBuilder =>
{
queryBuilder.select(
pgGetSelectValueForFieldAndTypeAndModifier(
ReturnType,
fieldContext,
parsedResolveInfoFragment,
sql.fragment`(public.date_format_tz(${queryBuilder.getTableAlias()}.${sqlColumn}, ${format}, ${tz}))`, // The brackets are necessary to stop the parser getting confused, ref: https://www.postgresql.org/docs/9.6/static/rowtypes.html#ROWTYPES-ACCESSING
type,
typeModifier
),
getSafeAliasFromAlias(parsedResolveInfoFragment.alias)
);
},
};
}
);
const convertFromPg = pg2gqlForType(type);
return {
args:
{
format: { type: GraphQLString, description: 'See https://www.postgresql.org/docs/current/functions-formatting.html' },
tz: { type: GraphQLString, description: 'Timezone to format the date in' }
},
description: attr.description,
type: nullableIf(
GraphQLNonNull,
!attr.isNotNull &&
!attr.type.domainIsNotNull &&
!attr.tags.notNull,
ReturnType
),
resolve(data, _args, _ctx, _info)
{
return convertFromPg(data[getSafeAliasFromResolveInfo(_info)]);
}
};
},
{
pgFieldIntrospection: attr,
isPostGraphilePluginTimestampFormatField: true
}
),
},
`Adding formatted timestamp field for ${describePgEntity(
attr
)}. You can rename this field with a 'Smart Comment':\n\n ${sqlCommentByAddingTags(
attr,
{ name: "newNameHere" }
)}`
);
return memo;
}, {}),
`Adding columns to '${describePgEntity(table)}'`
);
},
['PgColumns']
);
}
}