Skip to content

Commit

Permalink
Fixed #11 and improved computeed props
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jul 2, 2019
1 parent 6ff98c5 commit 41564b0
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 148 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# gqlx for JavaScript Applications Changelog

## 0.2.6

- Updated dependencies
- Improved inbuilt function generation
- Fixed bug with `cq` to normalize entries (#11)
- Improved calculation of computed props of objects

## 0.2.5

- Updated dependencies
Expand Down Expand Up @@ -29,12 +36,12 @@
## 0.2.0

- Fixed bug in parenthesis evaluation
- Emit consistent error object incl. diagonstic information
- Emit consistent error object incl. diagnostic information

## 0.1.6

- Updated dependencies due to CVE-2018-16469
- Fixed bug related to object with parantheses (#2)
- Fixed bug related to object with parentheses (#2)
- Fixed bug related to spread return (#3)
- Fixed bug related to assign property name (#4)
- Fixed bug related to nested `Promise.all` (#5)
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gqlx-js",
"description": "GraphQL eXtended language and tools library for Node.js / JS applications.",
"version": "0.2.5",
"version": "0.2.6",
"main": "dist",
"types": "dist",
"license": "MIT",
Expand All @@ -28,22 +28,22 @@
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.4.5",
"@types/acorn": "^4.0.5",
"@types/graphql": "^14.2.0",
"@types/jest": "^24.0.13",
"@types/node": "^11.13.12",
"@types/graphql": "^14.2.2",
"@types/jest": "^24.0.15",
"@types/node": "^11.13.15",
"babelify": "^10.0.0",
"browserify": "^16.2.3",
"graphql": "^14.3.1",
"graphql": "^14.4.1",
"jest": "^24.8.0",
"prettier": "^1.17.1",
"prettier": "^1.18.2",
"regenerator-runtime": "^0.13.2",
"ts-jest": "^24.0.2",
"ts-node": "^8.2.0",
"tslint": "^5.16.0",
"ts-node": "^8.3.0",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^2.9.2",
"uglify-js": "^3.5.15"
"uglify-js": "^3.6.0"
},
"peerDependencies": {
"graphql": "14.x || 0.13.x || 0.12.x || 0.11.x || 0.10.x"
Expand Down
93 changes: 93 additions & 0 deletions src/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,97 @@ describe('compile', () => {
const resultWithoutArgument = await svc('Query', 'test', {});
expect(resultWithoutArgument).toBe(12);
});

it('either uses either the left or right side', async () => {
const source = `
type Query {
test(name: String): String {
either(name, 'Fallback')
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result1 = await svc('Query', 'test', { name: 'Tester' });
const result2 = await svc('Query', 'test', {});
expect(result1).toBe('Tester');
expect(result2).toBe('Fallback');
});

it('use uses the given type in the callback', async () => {
const source = `
type Query {
test(name: String): Int {
use(name, x => x.length)
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'Tester' });
expect(result).toBe(6);
});

it('cq adds the provided values', async () => {
const source = `
type Query {
test(name: String): String {
cq('/myurl', { name })
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'tester' });
expect(result).toBe('/myurl?name=tester');
});

it('cq ignores empty objects', async () => {
const source = `
type Query {
test(name: String): String {
cq('/myurl', { })
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'tester' });
expect(result).toBe('/myurl');
});

it('cq concats multiple values', async () => {
const source = `
type Query {
test(name: String): String {
cq('/myurl', { a: 'b', name, c: 'd' })
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'tester' });
expect(result).toBe('/myurl?a=b&name=tester&c=d');
});

it('cq properly encodes the values', async () => {
const source = `
type Query {
test(name: String, value: String): String {
cq('/myurl', { name, value })
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'a&b', value: 'you&me' });
expect(result).toBe('/myurl?name=a%26b&value=you%26me');
});

it('cq supports computed objects', async () => {
const source = `
type Query {
test(name: String, value: String): String {
cq('/myurl', { [name]: value })
}
}`;
const mod = compile('test', source, {});
const svc = mod.createService({});
const result = await svc('Query', 'test', { name: 'a&b', value: 'you&me' });
expect(result).toBe('/myurl?a%26b=you%26me');
});
});
11 changes: 11 additions & 0 deletions src/helpers/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ export function wrapInLambda(argument: any, statements: Array<Statement> = [], a
};
}

export function callFunction(name: string, argument: Expression): CallExpression {
return {
type: 'CallExpression',
callee: {
type: 'Identifier',
name,
},
arguments: [argument],
};
}

export function isNotIdentifier(name: string): BinaryExpression {
return {
type: 'BinaryExpression',
Expand Down
Loading

0 comments on commit 41564b0

Please sign in to comment.