This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (79 loc) · 2.87 KB
/
index.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
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
const fs = require("fs");
const path = require("path");
const { ApolloServer } = require("apollo-server");
const Abstract = require("abstract-sdk");
function resolversForAbstractClient({ only, ignore } = {}) {
const transport = new Abstract.TRANSPORTS.API({
accessToken: "NOT_AUTHED"
});
return Reflect.ownKeys(transport).reduce(
(schema, endpointKey) => {
const transportEndpoint = transport[endpointKey];
// Ignore non-endpoints
if (
!transportEndpoint ||
["string", "number", "boolean"].includes(typeof transportEndpoint)
) {
return schema;
}
Reflect.ownKeys(transportEndpoint).forEach(methodKey => {
const property = `${endpointKey}.${methodKey}`;
if (endpointKey === "accessToken") return;
if (only && !only.includes(property)) return;
if (ignore && ignore.includes(property)) return;
if (["create", "update", "delete"].includes(methodKey)) {
const endpointResolverKey = `${endpointKey.charAt(0).toUpperCase() +
endpointKey.slice(1)}MutationEndpoint`;
schema[endpointResolverKey] = schema[endpointResolverKey] || {};
schema.Mutation[endpointKey] = (root, args, context) =>
context.abstract[endpointKey];
schema[endpointResolverKey][methodKey] = (endpoint, args) => {
console.log(endpoint, args)
return Promise.resolve(
endpoint[methodKey](
args.objectDescriptor,
args.input ? args.input : args.options,
args.input ? args.options : undefined,
)
);
}
} else {
const endpointResolverKey = `${endpointKey.charAt(0).toUpperCase() +
endpointKey.slice(1)}Endpoint`;
schema[endpointResolverKey] = schema[endpointResolverKey] || {};
schema.Query[endpointKey] = (root, args, context) =>
context.abstract[endpointKey];
schema[endpointResolverKey][methodKey] = (endpoint, args) =>
Promise.resolve(
endpoint[methodKey](args.objectDescriptor, args.options)
);
}
});
return schema;
},
{ Query: {}, Mutation: {} }
);
}
const server = new ApolloServer({
typeDefs: fs.readFileSync(path.resolve(__dirname, "schema.graphql"), "utf8"),
resolvers: resolversForAbstractClient(),
context: ({ req }) => {
const [_, accessToken = process.env.ABSTRACT_TOKEN] = (
req.headers.authorization || ""
).split(" ", 2);
return {
abstract: accessToken
? Abstract.client({
transport: Abstract.TRANSPORTS.API, // TODO: remove
accessToken
})
: undefined
};
},
engine: process.env.ENGINE_API_KEY && {
apiKey: process.env.ENGINE_API_KEY
}
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});