Skip to content

Commit 168587a

Browse files
committed
Update documentation for v1.7.0
1 parent f512027 commit 168587a

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
# json-to-graphql-query Changelog
33

4+
## 1.7.0
5+
6+
* Added `__aliasFor` option. The old `__alias` syntax did not support more than one alias. Thanks @jeniffer9
7+
* IMPORTANT: `__alias` is now deprecated and will be removed in version 2.0.0
8+
49
## 1.6.0
510

611
* Added support for `@client` directives (and other directives that don't need arguments). Thanks @joeflack4!

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Supported Options:
2727
* Full support for nested query / mutation nodes and arguments
2828
* Optionally strip specific object keys using the `ignoreFields` option
2929
* Support for input arguments via [`__args`](#query-with-arguments)
30-
* Support for query aliases via [`__alias`](#using-aliases)
30+
* Support for query aliases via [`__aliasFor`](#using-aliases)
3131
* Support for Enum values via [`EnumType`](#query-with-enum-values)
3232
* Support for variables via [`__variables`](#query-with-variables)
3333
* Support for simple directives (such as `@client`) via [`__directives`](#query-with-directives)
@@ -178,8 +178,8 @@ import { jsonToGraphQLQuery } from 'json-to-graphql-query';
178178

179179
const query = {
180180
query: {
181-
Posts: {
182-
__alias: 'allPosts',
181+
allPosts: {
182+
__aliasFor: 'Posts',
183183
id: true,
184184
comments: {
185185
id: true,

src/jsonToGraphQLQuery.ts

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,54 +78,59 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
7878
Object.keys(node)
7979
.filter((key) => filterNonConfigFields(key, options.ignoreFields))
8080
.forEach((key) => {
81-
if (typeof node[key] === 'object') {
82-
const fieldCount = Object.keys(node[key])
83-
.filter((keyCount) => filterNonConfigFields(keyCount, options.ignoreFields)).length;
84-
const subFields = fieldCount > 0;
85-
const argsExist = typeof node[key].__args === 'object';
86-
const directivesExist = typeof node[key].__directives === 'object';
87-
let token = `${key}`;
88-
if (typeof node[key].__aliasFor === 'string') {
89-
token = `${token}: ${node[key].__aliasFor}`;
90-
}
91-
if (typeof node[key].__variables === 'object') {
92-
token = `${token} (${buildVariables(node[key].__variables)})`;
93-
}
94-
else if (argsExist || directivesExist) {
95-
let argsStr: string;
96-
let dirsStr: string;
97-
if (directivesExist) {
98-
// TODO: Add support for multiple directives on one node. Then condense blocks into terniary lines.
99-
// const directives = Object.keys(node[key].__directives);
100-
const numDirectives = Object.keys(node[key].__directives).length;
101-
if (numDirectives > 1) {
102-
throw new Error(`Too many directives. The object/key ` +
103-
`'${Object.keys(node[key])[0]}' had ${numDirectives} directives, ` +
104-
`but only 1 directive per object/key is supported at this time.`);
81+
82+
if (typeof node[key] === 'object') {
83+
84+
const fieldCount = Object.keys(node[key])
85+
.filter((keyCount) => filterNonConfigFields(keyCount, options.ignoreFields)).length;
86+
const subFields = fieldCount > 0;
87+
const argsExist = typeof node[key].__args === 'object';
88+
const directivesExist = typeof node[key].__directives === 'object';
89+
90+
let token = `${key}`;
91+
92+
if (typeof node[key].__aliasFor === 'string') {
93+
token = `${token}: ${node[key].__aliasFor}`;
94+
}
95+
96+
if (typeof node[key].__variables === 'object') {
97+
token = `${token} (${buildVariables(node[key].__variables)})`;
98+
}
99+
else if (argsExist || directivesExist) {
100+
let argsStr: string;
101+
let dirsStr: string;
102+
if (directivesExist) {
103+
// TODO: Add support for multiple directives on one node.
104+
const numDirectives = Object.keys(node[key].__directives).length;
105+
if (numDirectives > 1) {
106+
throw new Error(`Too many directives. The object/key ` +
107+
`'${Object.keys(node[key])[0]}' had ${numDirectives} directives, ` +
108+
`but only 1 directive per object/key is supported at this time.`);
109+
}
110+
dirsStr = `@${buildDirectives(node[key].__directives)}`;
111+
}
112+
if (argsExist) {
113+
argsStr = `(${buildArgs(node[key].__args)})`;
105114
}
106-
// directives.map(((x) => { dirsStr += ` @${buildDirectives(node[key].__directives)}`; }));
107-
dirsStr = `@${buildDirectives(node[key].__directives)}`;
115+
const spacer = directivesExist && argsExist ? ' ' : '';
116+
token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
108117
}
109-
if (argsExist) {
110-
argsStr = `(${buildArgs(node[key].__args)})`;
118+
119+
// DEPRECATED: Should be removed in version 2.0.0
120+
if (typeof node[key].__alias === 'string') {
121+
token = `${node[key].__alias}: ${token}`;
111122
}
112-
const spacer = directivesExist && argsExist ? ' ' : '';
113-
token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
114-
}
115-
//Should be removed in version 2.0.0
116-
if (typeof node[key].__alias === 'string') {
117-
token = `${node[key].__alias}: ${token}`;
118-
}
119123

120-
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
121-
convertQuery(node[key], level + 1, output, options);
124+
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
125+
convertQuery(node[key], level + 1, output, options);
126+
127+
if (subFields) {
128+
output.push([ '}', level ]);
129+
}
122130

123-
if (subFields) {
124-
output.push([ '}', level ]);
131+
} else if (node[key]) {
132+
output.push([ `${key}`, level ]);
125133
}
126-
} else if (node[key]) {
127-
output.push([ `${key}`, level ]);
128-
}
129134
});
130135
}
131136

0 commit comments

Comments
 (0)