-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUploadFieldPlugin.js
More file actions
137 lines (122 loc) · 3.93 KB
/
UploadFieldPlugin.js
File metadata and controls
137 lines (122 loc) · 3.93 KB
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
module.exports = function UploadFieldPlugin(
builder,
{ uploadFieldDefinitions }
) {
const findMatchingDefinitions = (def, table, attr) =>
def.match({
schema: table.namespaceName,
table: table.name,
column: attr.name,
tags: attr.tags,
});
builder.hook("build", (_, build) => {
const {
addType,
graphql: { GraphQLScalarType },
} = build;
const GraphQLUpload = new GraphQLScalarType({
name: "Upload",
description: "The `Upload` scalar type represents a file upload.",
parseValue: value => value,
parseLiteral() {
throw new Error("‘Upload’ scalar literal unsupported.");
},
serialize() {
throw new Error("‘Upload’ scalar serialization unsupported.");
},
});
addType(GraphQLUpload);
return _;
});
builder.hook(
"GraphQLInputObjectType:fields:field",
(field, build, context) => {
const { getTypeByName } = build;
const {
scope: { pgIntrospection: table, pgFieldIntrospection: attr },
} = context;
if (!table || !attr) {
return field;
}
const foundUploadFieldDefinition =
uploadFieldDefinitions.filter(def =>
findMatchingDefinitions(def, table, attr)
).length === 1;
if (!foundUploadFieldDefinition) {
return field;
}
// Replace existing GraphQL type with `Upload` type
return Object.assign({}, field, {
type: getTypeByName("Upload"),
});
}
);
builder.hook("GraphQLObjectType:fields:field", (field, build, context) => {
const {
pgIntrospectionResultsByKind: introspectionResultsByKind,
inflection,
} = build;
const {
scope: { isRootMutation, fieldName, pgFieldIntrospection: table },
} = context;
if (!isRootMutation || !table) {
return field;
}
// It's possible that `resolve` isn't specified on a field, so in that case
// we fall back to a default resolver.
const defaultResolver = obj => obj[fieldName];
// Extract the old resolver from `field`
const { resolve: oldResolve = defaultResolver, ...rest } = field;
const uploadResolversByFieldName = introspectionResultsByKind.attribute
.filter(attr => attr.classId === table.id)
.reduce((memo, attr) => {
const defs = uploadFieldDefinitions.filter(def =>
findMatchingDefinitions(def, table, attr)
);
if (defs.length > 1) {
throw new Error("Upload field definitions are ambiguous");
}
if (defs.length === 1) {
const fieldName = inflection.column(attr);
memo[fieldName] = {
resolve: defs[0].resolve,
attribute: attr
};
}
return memo;
}, {});
return {
// Copy over everything except 'resolve'
...rest,
// Add our new resolver which wraps the old resolver
async resolve(source, args, context, info) {
// Recursively check for Upload promises to resolve
async function resolvePromises(obj) {
for (let key of Object.keys(obj)) {
if (obj[key] instanceof Promise) {
if (uploadResolversByFieldName[key]) {
const upload = await obj[key];
obj[key] = await uploadResolversByFieldName[key].resolve(
upload,
args,
{
...context,
attribute: uploadResolversByFieldName[key].attribute
},
info
);
}
} else if (obj[key] !== null && typeof obj[key] === "object") {
await resolvePromises(obj[key]);
}
}
}
await resolvePromises(args);
// Call the old resolver
const oldResolveResult = await oldResolve(source, args, context, info);
// Finally return the result.
return oldResolveResult;
},
};
});
};