Skip to content

Commit 5a3bf05

Browse files
committed
Refactor + Travis CI + Coveralls + Tests
1 parent 014373a commit 5a3bf05

File tree

9 files changed

+109
-28
lines changed

9 files changed

+109
-28
lines changed

.jestrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"transform": {
3+
"^.+\\.tsx?$": "ts-jest"
4+
},
5+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json"],
6+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$"
7+
}

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js: "node"
3+
cache:
4+
yarn: true
5+
directories:
6+
- node_modules
7+
after_success:
8+
- npm run coveralls

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<p align="center">
22
<img src="./assets/title.png" alt="salesforce graphql" width="326">
33
<br>
4-
<a href="https://travis-ci.org/jpmonette/salesforce-graphql"><img src="https://travis-ci.org/jpmonette/salesforce-graphql.svg?branch=master" alt="Build Status"></a>
4+
<a href="https://travis-ci.org/jpmonette/salesforce-graphql"><img src="https://travis-ci.org/jpmonette/salesforce-graphql.svg?branch=master" alt="Build Status"></a> <a href='https://coveralls.io/github/jpmonette/salesforce-graphql?branch=master'><img src='https://coveralls.io/repos/github/jpmonette/salesforce-graphql/badge.svg?branch=master' alt='Coverage Status' /></a>
55
</p>
66

77
<p align="center"><code>salesforce-graphql</code> - Bringing the <strong>GraphQL</strong> query language to <strong>Salesforce</strong></p>
@@ -42,6 +42,14 @@ const resolvers = {
4242
Contact: (parentobj, args, context, info) =>
4343
context.db.query({}, info).then(res => res.records[0]),
4444
},
45+
Account: {
46+
Contacts: (parent, args, context, info) =>
47+
context.db.query({ AccountId: parent.Id }, info).then(res => res.records),
48+
},
49+
Contact: {
50+
Account: (parent, args, context, info) =>
51+
context.db.query({ Id: parent.AccountId }, info).then(res => res.records[0]),
52+
},
4553
};
4654

4755
const conn = new jsforce.Connection({});
@@ -78,10 +86,13 @@ type Account {
7886
IsDeleted: Boolean
7987
Name: String
8088
Type: String
89+
90+
Contacts(limit: Int): [Contact]
8191
}
8292

8393
type Contact {
8494
Id: ID!
95+
Account: Account
8596
AccountId: String
8697
LastName: String
8798
FirstName: String
@@ -100,10 +111,16 @@ Head over to `http://localhost:4000/playground` to test with the following query
100111

101112
```graphql
102113
{
103-
Contacts(limit: 2) {
114+
Account(Id: "001E000001KnMkTIAV") {
104115
Id
105116
Name
106-
Salutation
117+
Contacts(limit: 1) {
118+
Name
119+
AccountId
120+
Account {
121+
Name
122+
}
123+
}
107124
}
108125
}
109126
```
@@ -120,4 +137,8 @@ Head over to `http://localhost:4000/playground` to test with the following query
120137

121138
- [`salesforce-graphql` on NPM](https://www.npmjs.com/package/salesforce-graphql)
122139
- Learn more about [GraphQL](http://graphql.org/)
123-
- [Salesforce REST API](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm) documentation
140+
- [Salesforce REST API](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm) documentation
141+
142+
## Extra
143+
144+
- Looking for [new opportunities](https://mavens.com/careers/)? Have a look at [Mavens](https://mavens.com/) website!

assets/output.png

34.9 KB
Loading

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
"main": "build/index.js",
99
"license": "MIT",
1010
"scripts": {
11+
"coveralls": "cat ./coverage/lcov.info | coveralls",
1112
"build": "rm -rf build && tsc",
12-
"prepare": "yarn build"
13+
"prepare": "yarn build",
14+
"test": "jest --coverage"
15+
},
16+
"devDependencies": {
17+
"@types/graphql": "^0.13.0",
18+
"@types/jest": "^22.2.3",
19+
"coveralls": "^3.0.0",
20+
"jest": "^22.4.3",
21+
"ts-jest": "^22.4.2",
22+
"typescript": "^2.8.1"
1323
}
1424
}

src/__tests__/q.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Q from '../q';
2+
3+
describe('Query generator', () => {
4+
it('should generate simple query', () => {
5+
const query = new Q('Account').build();
6+
expect(query).toBe(`SELECT Id FROM Account`);
7+
});
8+
9+
it('should add fields', () => {
10+
const query = new Q('Account').select(['Id', 'Name', 'BillingCountry']).build();
11+
expect(query).toBe(`SELECT Id, Name, BillingCountry FROM Account`);
12+
});
13+
14+
it('should add limit', () => {
15+
const query = new Q('Account').limit(10).build();
16+
expect(query).toBe(`SELECT Id FROM Account LIMIT 10`);
17+
});
18+
19+
it('should add where condition', () => {
20+
const query = new Q('Account').where('Id', '=', '001E000001KnMkTIAV').build();
21+
expect(query).toBe(`SELECT Id FROM Account WHERE Id = '001E000001KnMkTIAV'`);
22+
});
23+
});

src/index.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
import Q from './q';
22

33
function getFields(info) {
4-
const fields = [];
5-
info.fieldNodes[0].selectionSet.selections.map((value, key) => {
6-
if (!value.selectionSet) fields.push(value.name.value);
4+
const fields: string[] = [];
5+
info.fieldNodes.map(fieldNode => {
6+
if (fieldNode.selectionSet) {
7+
fieldNode.selectionSet.selections.map(value => {
8+
if (!value.selectionSet) fields.push(value.name.value);
9+
});
10+
}
711
});
812
return fields;
913
}
1014

11-
function getWheres(info: any): any[] {
12-
const wheres = [];
13-
info.fieldNodes[0].arguments.map((val, key) => {
14-
const field = val.name.value;
15-
const value = val.value.value;
16-
if (field !== 'limit') {
17-
wheres.push({ field, value, operator: '=' });
18-
}
15+
function getWheres(info): any[] {
16+
const wheres: { field: string; value: string; operator: string }[] = [];
17+
info.fieldNodes.map(fieldNode => {
18+
fieldNode.arguments.map(val => {
19+
const field = val.name.value;
20+
const value = val.value.value;
21+
if (field !== 'limit') {
22+
wheres.push({ field, value, operator: '=' });
23+
}
24+
});
1925
});
2026
return wheres;
2127
}
2228

23-
function getLimit(info: any): number | void {
29+
function getLimit(info): number | void {
2430
let limit;
25-
info.fieldNodes[0].arguments.map((value, key) => {
26-
if (value.name.value === 'limit') {
27-
limit = value.value.value;
28-
}
31+
info.fieldNodes.map(fieldNode => {
32+
fieldNode.arguments.map(value => {
33+
if (value.name.value === 'limit') {
34+
limit = value.value.value;
35+
}
36+
});
2937
});
3038
return limit;
3139
}
3240

33-
export class Salesforce {
34-
public conn;
41+
class Salesforce {
42+
conn: any;
3543

3644
constructor(props) {
45+
console.log('CONSTRUCTOR')
3746
this.conn = props.conn;
3847
}
3948

40-
query = (parent, info) => {
49+
public query = (parent: { key: string }, info) => {
50+
console.log('BEFORE')
4151
const queryBuilder = new Q(info.returnType.ofType || info.returnType).select(getFields(info));
4252
const limit = getLimit(info);
4353

@@ -54,3 +64,5 @@ export class Salesforce {
5464
return this.conn.query(query);
5565
};
5666
}
67+
68+
export { Salesforce };

src/q.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export default class Q {
22
fromText: string;
33
fieldList: string[];
4-
numberOfRows: number;
4+
numberOfRows: number | undefined;
55
conditions: { field: string; operator: string; value: string }[];
6-
numberOfRowsToSkip: number;
6+
numberOfRowsToSkip: number | undefined;
77

88
constructor(sobject: string) {
99
this.fromText = sobject;
@@ -48,7 +48,7 @@ export default class Q {
4848
if (this.conditions.length !== 0) {
4949
return 'WHERE ' + condList.join(' AND ');
5050
} else {
51-
return null;
51+
return '';
5252
}
5353
};
5454

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"module": "commonjs",
44
"outDir": "build",
55
"target": "es2016",
6-
"skipLibCheck": true
6+
"lib": ["esnext","dom"]
77
},
88
"include": ["src/**/*"],
99
"exclude": ["node_modules", "**/node_modules/*"]

0 commit comments

Comments
 (0)