Skip to content

Commit 6e646dc

Browse files
committed
Dynamically generate GraphQL resolvers
1 parent a6e517d commit 6e646dc

4 files changed

Lines changed: 45 additions & 21 deletions

File tree

lib/apiSpec.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
var fs = require('fs')
2-
3-
var apiSchema = fs.readFileSync('./app/prisma/api-schema/api-schema.json', { encoding: 'utf8', flag: 'r' })
4-
apiSchema = JSON.parse(apiSchema);
5-
var schemas = Object.entries(apiSchema.schemas)
1+
var fs = require('fs');
62

73
function getGraphQLSchemas() {
4+
var apiSchema = fs.readFileSync('./app/api-schema.json', { encoding: 'utf8', flag: 'r' })
5+
apiSchema = JSON.parse(apiSchema);
6+
var schemas = Object.entries(apiSchema.schemas)
87
var types = [];
98
for (const [key, value] of Object.entries(schemas)) {
109
var type = value[0];
1110
var endpoints = Object.entries(value[1]);
1211
for (const [endpointKey, endpointValue] of endpoints) {
1312
if (endpointValue != null && endpointValue['input_validation_rules'] != null) {
14-
rules = Object.entries(endpointValue['input_validation_rules']);
13+
var rules = Object.entries(endpointValue['input_validation_rules']);
1514
var startLine = ` type ` + type + ` {`
1615
var fieldLines = {};
1716
for (const [fieldName, rulesValue] of rules) {
@@ -55,12 +54,12 @@ function getGraphQLSchemas() {
5554
`;
5655
schemas = Object.entries(apiSchema.schemas);
5756
for (const [key, value] of schemas) {
58-
queryString = queryString + ' ' + key + ': [' + key + ']\n';
57+
queryString = queryString + ' ' + key + ': [' + key + ']\n';
5958
}
6059
queryString = queryString + `
6160
}`;
6261
string = string + queryString;
63-
62+
6463
return string;
6564

6665
// GraphQL Schema String example:
@@ -84,6 +83,32 @@ function getGraphQLSchemas() {
8483
*/
8584
}
8685

86+
function getGraphQLResolvers() {
87+
var apiSchema = fs.readFileSync('./app/api-schema.json', { encoding: 'utf8', flag: 'r' })
88+
apiSchema = JSON.parse(apiSchema);
89+
var schemas = Object.entries(apiSchema.schemas)
90+
var types = [];
91+
var resolvers = {};
92+
for (const [key, value] of Object.entries(schemas)) {
93+
const { Resource } = require('../models/resource');
94+
var resource = new Resource(value[0]).browse();
95+
resolvers[value[0]] = () => resource;
96+
}
97+
98+
return resolvers;
99+
100+
/*
101+
// Result should produce an object that looks like this:
102+
var resolvers =
103+
{
104+
Query: {
105+
'analytic_events': () => data.browse()
106+
},
107+
};
108+
*/
109+
}
110+
87111
module.exports = {
88-
getGraphQLSchemas
112+
getGraphQLSchemas,
113+
getGraphQLResolvers
89114
}

models/resource.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class Resource {
5656
}
5757

5858
async add(data: any) {
59-
var apiSchema = fs.readFileSync('./prisma/api-schema/api-schema.json', { encoding: 'utf8', flag: 'r' })
59+
var apiSchema = fs.readFileSync('./app/api-schema.json', { encoding: 'utf8', flag: 'r' })
6060
apiSchema = JSON.parse(apiSchema)
6161
var validationRules = apiSchema.schemas?.[this.pluralizeType()]?.['add']?.['input_validation_rules']
6262
console.log(validationRules)
@@ -83,7 +83,7 @@ export class Resource {
8383
}
8484

8585
async edit(id: Number, data: any) {
86-
var apiSchema = fs.readFileSync('./prisma/api-schema/api-schema.json', { encoding: 'utf8', flag: 'r' })
86+
var apiSchema = fs.readFileSync('./app/api-schema.json', { encoding: 'utf8', flag: 'r' })
8787
apiSchema = JSON.parse(apiSchema)
8888
var validationRules = apiSchema.schemas?.[this.pluralizeType()]?.['edit']?.['input_validation_rules']
8989
console.log(validationRules)
@@ -129,5 +129,4 @@ export class Resource {
129129
}
130130

131131

132-
}
133-
132+
}

src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ if (process.env.PUBLIC_DIRECTORY != null) {
3636
if (Boolean(process.env.ENABLE_GRAPHQL) != false) {
3737

3838
var gqlSchema = ApiSpec.getGraphQLSchemas()
39-
console.log(gqlSchema)
39+
// console.log(gqlSchema)
4040

4141
const typeDefs = gqlSchema
4242

43-
var users = new Resource('user');
43+
//var data = new Resource('analytic_events');
44+
console.log(ApiSpec.getGraphQLResolvers())
45+
var generatedResolvers = ApiSpec.getGraphQLResolvers();
4446

4547
// Resolvers define the technique for fetching the types defined in the
4648
// schema. This resolver retrieves books from the "books" array above.
4749
const resolvers = {
48-
Query: {
49-
users: () => users.browse()
50-
,
51-
},
50+
Query: generatedResolvers,
5251
};
5352
const server = new ApolloServer({
5453
typeDefs,
@@ -60,8 +59,8 @@ var users = new Resource('user');
6059

6160
// Rest API Schema
6261
app.get('/api', function (req, res) {
63-
var jsonSchema = fs.readFileSync('./app/prisma/json-schema/json-schema.json', { encoding: 'utf8', flag: 'r' })
64-
var apiSchema = fs.readFileSync('./app/prisma/api-schema/api-schema.json', { encoding: 'utf8', flag: 'r' })
62+
var jsonSchema = fs.readFileSync('./app/json-schema.json', { encoding: 'utf8', flag: 'r' })
63+
var apiSchema = fs.readFileSync('./app/api-schema.json', { encoding: 'utf8', flag: 'r' })
6564
apiSchema = JSON.parse(apiSchema)
6665
var output = {
6766
apiSchema: apiSchema

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"strict": true,
66
"lib": ["esnext"],
77
"esModuleInterop": true,
8+
"allowJs": true
89
}
910
}

0 commit comments

Comments
 (0)