Skip to content

Commit b1c5e5e

Browse files
author
Lucas Bento
authored
Handle Mongoose refs on schema (#57)
2 parents c8afb13 + 29f00ae commit b1c5e5e

File tree

8 files changed

+110
-55
lines changed

8 files changed

+110
-55
lines changed

packages/generator/src/graphqlrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"mutation": "mutation",
88
"mutation_test": "mutation/__tests__",
99
"type": "type",
10-
"type_test": "type/__tests__"
10+
"type_test": "type/__tests__",
11+
"interface": "interface"
1112
},
1213
"files": {
1314
"schema": "schema"
1415
}
15-
}
16+
}

packages/generator/src/loader/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class LoaderGenerator extends Generator {
3131

3232
generateLoader() {
3333
const schema = this.options.model ?
34-
getMongooseModelSchema(this.options.model, true)
34+
getMongooseModelSchema({ model: this.options.model, withTimestamps: true })
3535
: null;
3636

3737
const name = uppercaseFirstLetter(this.options.name);

packages/generator/src/mutation/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MutationGenerator extends Generator {
7272
generateMutation() {
7373
let schema = null;
7474
if (this.options.model) {
75-
const modelSchema = getMongooseModelSchema(this.options.model);
75+
const modelSchema = getMongooseModelSchema({ model: this.options.model });
7676
schema = this._parseSchema(modelSchema);
7777
}
7878

@@ -105,6 +105,8 @@ class MutationGenerator extends Generator {
105105
directories,
106106
};
107107

108+
// TODO: generate type and loader that do not exist yet
109+
108110
Object.keys(mutations).forEach((mutationType) => {
109111
const { template, fileName } = mutations[mutationType];
110112

packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Object {
66
} from \'graphql\';
77
import { globalIdField } from \'graphql-relay\';
88
9+
import { NodeInterface } from \'../interface/NodeInterface\';
10+
911
export default new GraphQLObjectType({
1012
name: \'Example\',
1113
description: \'Represents Example\',
@@ -16,6 +18,7 @@ export default new GraphQLObjectType({
1618
description: \'My example field\',
1719
resolve: obj => obj.example,
1820
},
21+
interfaces: () => [NodeInterface],
1922
}),
2023
});",
2124
"typeTest": "import { graphql } from \'graphql\';
@@ -62,10 +65,13 @@ Object {
6265
import {
6366
GraphQLObjectType,
6467
GraphQLString,
65-
GraphQLID,
6668
} from \'graphql\';
6769
import { globalIdField } from \'graphql-relay\';
6870
71+
import { NodeInterface } from \'../interface/NodeInterface\';
72+
import UserType from \'./UserType\';
73+
import UserLoader from \'../loader/UserLoader\';
74+
6975
export default new GraphQLObjectType({
7076
name: \'Post\',
7177
description: \'Represents Post\',
@@ -77,9 +83,9 @@ export default new GraphQLObjectType({
7783
resolve: obj => obj.title,
7884
},
7985
user: {
80-
type: GraphQLID,
86+
type: UserType,
8187
description: \'User that created this post\',
82-
resolve: obj => obj.user,
88+
resolve: async (obj, args, { user }) => await UserLoader.load(user, obj.user),
8389
},
8490
slug: {
8591
type: GraphQLString,
@@ -96,6 +102,7 @@ export default new GraphQLObjectType({
96102
description: \'\',
97103
resolve: obj => obj.updatedAt.toISOString(),
98104
},
105+
interfaces: () => [NodeInterface],
99106
}),
100107
});
101108
",
@@ -147,6 +154,8 @@ import {
147154
} from \'graphql\';
148155
import { globalIdField } from \'graphql-relay\';
149156
157+
import { NodeInterface } from \'../interface/NodeInterface\';
158+
150159
export default new GraphQLObjectType({
151160
name: \'User\',
152161
description: \'Represents User\',
@@ -172,6 +181,7 @@ export default new GraphQLObjectType({
172181
description: \'\',
173182
resolve: obj => obj.active,
174183
},
184+
interfaces: () => [NodeInterface],
175185
}),
176186
});
177187
",

packages/generator/src/type/index.js

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getMongooseModelSchema,
44
getConfigDir,
55
uppercaseFirstLetter,
6+
getRelativeConfigDir,
67
} from '../utils';
78

89
class TypeGenerator extends Generator {
@@ -23,13 +24,15 @@ class TypeGenerator extends Generator {
2324
}
2425

2526
generateType() {
26-
let schema = this.options.model ?
27-
getMongooseModelSchema(this.options.model, true)
27+
const schema = this.options.model ?
28+
getMongooseModelSchema({
29+
model: this.options.model,
30+
withTimestamps: true,
31+
ref: true,
32+
})
2833
: null;
2934

30-
if (schema) {
31-
schema = this._parseSchemaResolvers(schema);
32-
}
35+
const directories = this._getConfigDirectories();
3336

3437
const name = uppercaseFirstLetter(this.options.name);
3538
const typeFileName = `${name}Type`;
@@ -42,50 +45,34 @@ class TypeGenerator extends Generator {
4245
const templateVars = {
4346
name,
4447
schema,
48+
directories,
4549
};
4650

4751
this._generateTypeTest({
4852
name,
4953
schema,
5054
});
5155

56+
// TODO: generate types and loaders that do not exist yet
57+
5258
this.fs.copyTpl(templatePath, destinationPath, templateVars);
5359
}
5460

55-
/**
56-
* Parse schema resolvers checking if the fields need different resolvers.
57-
* @param schema {Array} The parsed Mongoose schema
58-
* @returns {Array} The parsed schema with resolvers
59-
*/
60-
_parseSchemaResolvers(schema) {
61-
const fields = schema.fields.map((field) => {
62-
if (field.originalType === 'Date') {
63-
return {
64-
...field,
65-
resolve: `obj.${field.name}.toISOString()`,
66-
};
67-
}
68-
69-
return {
70-
...field,
71-
resolve: `obj.${field.name}`,
72-
};
73-
});
74-
75-
return {
76-
...schema,
77-
fields,
78-
};
61+
_getConfigDirectories() {
62+
return getRelativeConfigDir('type', ['model', 'type', 'loader', 'connection', 'interface']);
7963
}
8064

8165
_generateTypeTest({ name, schema }) {
8266
const templatePath = this.templatePath('test/Type.js.template');
8367

8468
const destinationPath = this.destinationPath(`${this.destinationDir}/__tests__/${name}Type.spec.js`);
8569

70+
const directories = this._getConfigDirectories();
71+
8672
const templateVars = {
8773
name,
8874
schema,
75+
directories,
8976
};
9077

9178
this.fs.copyTpl(templatePath, destinationPath, templateVars);

packages/generator/src/type/templates/Type.js.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
} from 'graphql';
55
import { globalIdField } from 'graphql-relay';
66

7+
import { NodeInterface } from '<%= directories.interface %>/NodeInterface';
8+
79
export default new GraphQLObjectType({
810
name: '<%= name %>',
911
description: 'Represents <%= name %>',
@@ -14,5 +16,6 @@ export default new GraphQLObjectType({
1416
description: 'My example field',
1517
resolve: obj => obj.example,
1618
},
19+
interfaces: () => [NodeInterface],
1720
}),
1821
});

packages/generator/src/type/templates/TypeWithSchema.js.template

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import {
88
} from 'graphql';
99
import { globalIdField } from 'graphql-relay';
1010

11+
import { NodeInterface } from '<%= directories.interface %>/NodeInterface';
12+
<%_ for (i in schema.typeDependencies) { -%>
13+
import <%= schema.typeDependencies[i] %> from '<%= directories.type %>/<%=schema.typeDependencies[i]%>';
14+
<%_ } -%>
15+
<%_ for (i in schema.loaderDependencies) { -%>
16+
import <%= schema.loaderDependencies[i] %> from '<%= directories.loader %>/<%=schema.loaderDependencies[i]%>';
17+
<%_ } -%>
18+
1119
export default new GraphQLObjectType({
1220
name: '<%= name %>',
1321
description: 'Represents <%= name %>',
@@ -17,8 +25,9 @@ export default new GraphQLObjectType({
1725
<%- field.name %>: {
1826
type: <%= field.type %>,
1927
description: '<%= field.description %>',
20-
resolve: obj => <%= field.resolve %>,
28+
resolve: <%if(field.resolveArgs){%><%=field.resolveArgs%><%}else{ %>obj<%} %> => <%= field.resolve %>,
2129
},
2230
<%_ } -%>
31+
interfaces: () => [NodeInterface],
2332
}),
2433
});

0 commit comments

Comments
 (0)