-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
56 lines (50 loc) · 1.45 KB
/
resolvers.js
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
const prisma = require("./prismaClient");
// Get Fields Which Are being Queried
const getFieldNamesFromSelectionSet = (selectionSet) => {
const fieldNames = [];
if (selectionSet && selectionSet.selections) {
for (const selection of selectionSet.selections) {
if (selection.kind === "Field") {
fieldNames.push(selection.name.value);
} else if (selection.kind === "InlineFragment") {
fieldNames.push(
...getFieldNamesFromSelectionSet(selection.selectionSet)
);
} else if (selection.kind === "FragmentSpread") {
// Assuming you handle fragment spreads separately
}
}
}
return fieldNames;
};
async function getAllEmployees(selectedFields) {
const employees = await prisma.user.findMany({ select: selectedFields });
return employees;
}
const resolvers = {
Query: {
// Define your Query resolvers here
hello: () => "Hello, World!",
users: async (_, __, ___, info) => {
const fieldNames = getFieldNamesFromSelectionSet(
info.fieldNodes[0].selectionSet
);
const selectedFields = {};
for (const fieldName of fieldNames) {
selectedFields[fieldName] = true;
}
const users = await getAllEmployees(selectedFields);
return users;
},
},
Mutation: {
createUser: (_, { input }) => {
return {
id: "123",
name: input.username,
email: input.email,
};
},
},
};
module.exports = resolvers;