forked from thomascullen/airtable-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.33 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
const { printSchema } = require("graphql");
const { ApolloServer } = require("apollo-server");
const convertSchema = require("./convertSchema");
const createResolvers = require("./createResolvers");
const airtable = require("airtable");
const fs = require('fs')
class AirtableGraphQL {
constructor(apiKey, config = {}) {
this.columns = {};
airtable.configure({ apiKey });
const schema = JSON.parse(fs.readFileSync(config.schemaPath || "./schema.json", "utf8"));
var normalizedPath = require("path").join(__dirname, "columns");
require("fs")
.readdirSync(normalizedPath)
.forEach(file => {
require("./columns/" + file)(this);
});
this.api = airtable.base(schema.id);
this.schema = convertSchema(schema, this.columns);
this.resolvers = createResolvers(
schema,
this.api,
this.columns
);
this.server = new ApolloServer({
typeDefs: printSchema(this.schema),
resolvers: this.resolvers,
playground: config.playground,
introspection: true
});
}
addColumnSupport(columnType, config) {
this.columns = {
...this.columns,
[columnType]: config
};
}
async listen(options) {
this.server.listen(options).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
}
}
module.exports = AirtableGraphQL;