Skip to content

Commit 7a69034

Browse files
authored
Merge pull request #39 from tomyitav/more-resolvers-checks
More resolvers checks, some fixes for resolver generation
2 parents 8cda9e8 + 8699e21 commit 7a69034

File tree

9 files changed

+142
-27
lines changed

9 files changed

+142
-27
lines changed

src/commands/resolver/resolver-constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ export const resolverPrefix = 'const resolveFunctions = {\n'
22
export const resolverSuffix = '};\n'
33
export const operationPrefix = ': {\n'
44
export const operationSuffix = '\t},\n'
5-
export const operationFuntionsSuffix = '\t\t},\n'
6-
export const functionOperationPrefix = '=> {\n'
5+
export const operationFuntionsSuffix = '\t},\n'
6+
export const functionOperationPrefix = ' => {},\n'
77
export const gqlMethodSignature = '(rootObj: any, args: any, context: any)'
8-
export const gqlMethodSuffix = ' {\n'
8+
export const gqlMethodSuffix = ' {},\n'
99
export const subscriptionSubscribeDefinition = '\t\t\tsubscribe: '
1010
export const keysToGenerate = ['Query', 'Mutation', 'Subscription']

src/commands/resolver/resolver.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
keysToGenerate,
1010
operationFuntionsSuffix,
1111
operationPrefix,
12-
operationSuffix,
1312
resolverPrefix,
1413
resolverSuffix,
1514
subscriptionSubscribeDefinition
@@ -61,7 +60,7 @@ export class Resolver extends AbstractCommand {
6160
let resolverContent = resolverPrefix
6261
Object.entries(schemaDefinitions).forEach(([key, operation]: [string, Operation]) => {
6362
if (keysToGenerate.includes(key)) {
64-
resolverContent += this.getTypeOperationContent(operation) + operationSuffix
63+
resolverContent += this.getTypeOperationContent(operation)
6564
}
6665
})
6766
resolverContent += resolverSuffix
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const schema = 'abc'
2+
3+
export default schema
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const schema = `
2+
type Tweet {
3+
id: ID!
4+
# The tweet text. No more than 140 characters!
5+
body: String
6+
# When the tweet was published
7+
date: Date
8+
# Who published the tweet
9+
Author: User
10+
# Views, retweets, likes, etc
11+
Stats: Stat
12+
}
13+
14+
type User {
15+
id: ID!
16+
username: String
17+
first_name: String
18+
last_name: String
19+
full_name: String
20+
name: String @deprecated
21+
avatar_url: Url
22+
}
23+
24+
type Stat {
25+
views: Int
26+
likes: Int
27+
retweets: Int
28+
responses: Int
29+
}
30+
31+
type Notification {
32+
id: ID
33+
date: Date
34+
type: String
35+
}
36+
37+
type Meta {
38+
count: Int
39+
}
40+
41+
scalar Url
42+
scalar Date
43+
44+
type Query {
45+
Tweet(id: ID!): Tweet
46+
Tweets(limit: Int, skip: Int, sort_field: String, sort_order: String): [Tweet]
47+
TweetsMeta: Meta
48+
User(id: ID!): User
49+
Notifications(limit: Int): [Notification]
50+
NotificationsMeta: Meta
51+
}
52+
53+
type Mutation {
54+
createTweet (
55+
body: String
56+
): Tweet
57+
deleteTweet(id: ID!): Tweet
58+
markTweetRead(id: ID!): Boolean
59+
}
60+
`
61+
62+
export default schema

test/commands/resolver/resolver.test.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import * as fse from 'fs-extra'
55

66
describe('Resolver command test', () => {
77
let res: AbstractCommand
8-
const pathToType = './test/commands/resolver/test-schema.ts'
9-
const pathToActualResolver = './test/output/actual/test-resolver.ts'
8+
const pathToType1 = './test/commands/resolver/legal-schemas/test-schema1.ts'
9+
const pathToTypeTweets = './test/commands/resolver/legal-schemas/tweet-test-schema.ts'
10+
const pathToIllegalSchema1 = './test/commands/resolver/illegal-schemas/illegal-schema1.ts'
11+
const pathToActualResolver1 = './test/output/actual/test-resolver.ts'
12+
const pathToActualResolverTweets = './test/output/actual/tweets-test-resolver.ts'
1013
const pathToDirectoryActualResolver = './test/output/actual/non/existing/dir/test-resolver.ts'
1114
const resolverDirLocation = './test/output/actual/non'
12-
const pathToExpectedResolver = './test/output/expected/commands/resolver/test-resolver.ts'
15+
const pathToExpectedResolver1 = './test/output/expected/commands/resolver/test-resolver.ts'
16+
const pathToExpectedResolverTweets =
17+
'./test/output/expected/commands/resolver/tweets-test-resolver.ts'
1318
const pathToNonOverridenFile =
1419
'./test/output/expected/commands/resolver/test-not-overriden-by-resolver.ts'
1520

@@ -18,8 +23,11 @@ describe('Resolver command test', () => {
1823
})
1924

2025
afterEach(() => {
21-
if (fs.existsSync(pathToActualResolver)) {
22-
fs.unlinkSync(pathToActualResolver)
26+
if (fs.existsSync(pathToActualResolver1)) {
27+
fs.unlinkSync(pathToActualResolver1)
28+
}
29+
if (fs.existsSync(pathToActualResolverTweets)) {
30+
fs.unlinkSync(pathToActualResolverTweets)
2331
}
2432
fs.stat(resolverDirLocation, (err, stats) => {
2533
if (!err) {
@@ -40,39 +48,67 @@ describe('Resolver command test', () => {
4048
expect(act).toBeInstanceOf(Function)
4149
})
4250

43-
it('works if resolver file was generated', async () => {
51+
it('works if resolver file was not generated for illegal schema 1', async () => {
4452
const actFunction = res.getAction()
45-
await actFunction(pathToType, pathToActualResolver)
46-
const resolverFileExist: boolean = fs.existsSync(pathToActualResolver)
53+
await actFunction(pathToIllegalSchema1, pathToActualResolver1)
54+
const resolverFileExist: boolean = fs.existsSync(pathToActualResolver1)
55+
expect(resolverFileExist).not.toBeTruthy()
56+
})
57+
58+
it('works if resolver file was generated for test-schema-1', async () => {
59+
const actFunction = res.getAction()
60+
await actFunction(pathToType1, pathToActualResolver1)
61+
const resolverFileExist: boolean = fs.existsSync(pathToActualResolver1)
62+
expect(resolverFileExist).toBeTruthy()
63+
})
64+
65+
it('works if resolver file was generated for tweet-test-schema', async () => {
66+
const actFunction = res.getAction()
67+
await actFunction(pathToTypeTweets, pathToActualResolverTweets)
68+
const resolverFileExist: boolean = fs.existsSync(pathToActualResolverTweets)
4769
expect(resolverFileExist).toBeTruthy()
4870
})
4971

5072
it('works if resolver file was generated in directory structure', async () => {
5173
const actFunction = res.getAction()
52-
await actFunction(pathToType, pathToDirectoryActualResolver)
74+
await actFunction(pathToType1, pathToDirectoryActualResolver)
5375
const resolverFileExist: boolean = fs.existsSync(pathToDirectoryActualResolver)
5476
expect(resolverFileExist).toBeTruthy()
5577
})
5678

57-
it('works if resolver file is identical to expected file', async () => {
79+
it('works if resolver file is identical to expected file for test-schema1', async () => {
80+
const actFunction = res.getAction()
81+
await actFunction(pathToType1, pathToActualResolver1)
82+
const expectedContent = fs
83+
.readFileSync(pathToExpectedResolver1)
84+
.toString()
85+
.replace(/\s/g, '')
86+
const actualContent = fs
87+
.readFileSync(pathToActualResolver1)
88+
.toString()
89+
.replace(/\s/g, '')
90+
expect(actualContent).toEqual(expectedContent)
91+
})
92+
93+
it('works if resolver file is identical to expected file for tweets-test-schema', async () => {
5894
const actFunction = res.getAction()
59-
await actFunction(pathToType, pathToActualResolver)
95+
await actFunction(pathToTypeTweets, pathToActualResolverTweets)
6096
const expectedContent = fs
61-
.readFileSync(pathToExpectedResolver)
97+
.readFileSync(pathToExpectedResolverTweets)
6298
.toString()
6399
.replace(/\s/g, '')
64100
const actualContent = fs
65-
.readFileSync(pathToActualResolver)
101+
.readFileSync(pathToActualResolverTweets)
66102
.toString()
67103
.replace(/\s/g, '')
68104
expect(actualContent).toEqual(expectedContent)
69105
})
70106

71107
it('works if resolver command does not override existing file', async () => {
72108
const actFunction = res.getAction()
73-
await actFunction(pathToType, pathToNonOverridenFile)
109+
await actFunction(pathToType1, pathToNonOverridenFile)
74110
const expectedContent = fs
75-
.readFileSync(pathToExpectedResolver)
111+
.readFileSync(pathToExpectedResolver1)
76112
.toString()
77113
.replace(/\s/g, '')
78114
const actualContent = fs
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
const resolveFunctions = {
22
Query: {
3-
car(rootObj: any, args: any, context: any) {
4-
},
3+
car(rootObj: any, args: any, context: any) {},
54
},
65
Mutation: {
7-
updateCarName(rootObj: any, args: any, context: any) {
8-
},
6+
updateCarName(rootObj: any, args: any, context: any) {},
97
},
108
Subscription: {
119
carChanged: {
12-
subscribe: (rootObj: any, args: any, context: any)=> {
13-
},
10+
subscribe: (rootObj: any, args: any, context: any) => {},
1411
},
1512
},
1613
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const resolveFunctions = {
2+
Query: {
3+
Tweet(rootObj: any, args: any, context: any) {},
4+
Tweets(rootObj: any, args: any, context: any) {},
5+
TweetsMeta(rootObj: any, args: any, context: any) {},
6+
User(rootObj: any, args: any, context: any) {},
7+
Notifications(rootObj: any, args: any, context: any) {},
8+
NotificationsMeta(rootObj: any, args: any, context: any) {},
9+
},
10+
Mutation: {
11+
createTweet(rootObj: any, args: any, context: any) {},
12+
deleteTweet(rootObj: any, args: any, context: any) {},
13+
markTweetRead(rootObj: any, args: any, context: any) {},
14+
},
15+
};

tslint.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2+
"rules": {
3+
"no-empty": false
4+
},
25
"extends": [
36
"tslint-config-standard",
47
"tslint-config-prettier"
58
]
6-
}
9+
}

0 commit comments

Comments
 (0)