Skip to content

Commit 759d64b

Browse files
authored
support operation name (#25)
* support named queries/mutations
1 parent 5c7e3da commit 759d64b

File tree

5 files changed

+234
-1
lines changed

5 files changed

+234
-1
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ query {
417417
```
418418

419419
### Query with multiple Inline Fragments
420+
420421
```typescript
421422
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
422423

@@ -453,6 +454,42 @@ query {
453454
}
454455
```
455456

457+
### Query with name
458+
459+
```typescript
460+
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
461+
462+
const query = {
463+
query: {
464+
__name: 'NewName',
465+
__variables: {
466+
variable1: 'String!',
467+
variableWithDefault: 'String = "default_value"'
468+
},
469+
Posts: {
470+
__args: {
471+
arg1: 20,
472+
arg2: new VariableType('variable1')
473+
},
474+
id: true,
475+
title: true
476+
}
477+
}
478+
};
479+
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
480+
```
481+
482+
Resulting `graphql_query`
483+
484+
```graphql
485+
query NewName ($variable1: String!, $variableWithDefault: String = "default_value") {
486+
Posts (arg1: 20, arg2: $variable1) {
487+
id
488+
title
489+
}
490+
}
491+
```
492+
456493
## TO-DO List
457494

458495
* Support Named Queries / Mutations

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"devDependencies": {
1818
"@types/chai": "4.2.8",
1919
"@types/mocha": "7.0.1",
20+
"@types/node": "14.0.27",
2021
"chai": "4.2.0",
2122
"mocha": "7.2.0",
2223
"sinon": "8.1.1",

src/__tests__/name.tests.ts

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import { expect } from 'chai';
2+
import { jsonToGraphQLQuery, EnumType, VariableType } from '../';
3+
4+
describe('jsonToGraphQLQuery() - name', () => {
5+
it('supports Named Queries', () => {
6+
const query = {
7+
query: {
8+
__name: 'NewName',
9+
lorem: {
10+
__aliasFor: 'Posts',
11+
__args: {
12+
arg1: 20,
13+
},
14+
id: true,
15+
},
16+
larem: {
17+
__aliasFor: 'Posts',
18+
__args: {
19+
arg2: 10,
20+
},
21+
id: true,
22+
},
23+
},
24+
};
25+
expect(jsonToGraphQLQuery(query)).to.equal(
26+
'query NewName { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
27+
);
28+
});
29+
30+
it('supports Named Mutations', () => {
31+
const query = {
32+
mutation: {
33+
__name: 'NewName',
34+
one: {
35+
__aliasFor: 'Posts',
36+
__args: {
37+
arg1: 20,
38+
},
39+
id: true,
40+
},
41+
two: {
42+
__aliasFor: 'Posts',
43+
__args: {
44+
arg2: 10,
45+
},
46+
id: true,
47+
},
48+
},
49+
};
50+
expect(jsonToGraphQLQuery(query)).to.equal(
51+
'mutation NewName { one: Posts (arg1: 20) { id } two: Posts (arg2: 10) { id } }'
52+
);
53+
});
54+
});
55+
56+
describe('jsonToGraphQLQuery() - combinations', () => {
57+
it('correctly converts query with name/variables', () => {
58+
const query = {
59+
query: {
60+
__name: 'NewName',
61+
__variables: {
62+
variable1: 'String!',
63+
variableWithDefault: 'String = "default_value"',
64+
},
65+
Posts: {
66+
__args: {
67+
arg1: 20,
68+
arg2: new VariableType('variable1'),
69+
},
70+
id: true,
71+
title: true,
72+
},
73+
},
74+
};
75+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
76+
`query NewName ($variable1: String!, $variableWithDefault: String = "default_value") {
77+
Posts (arg1: 20, arg2: $variable1) {
78+
id
79+
title
80+
}
81+
}`
82+
);
83+
});
84+
85+
it('correctly converts query with variables/name/alias/args/variable/fragments', () => {
86+
const query = {
87+
query: {
88+
__name: 'NewName',
89+
__variables: {
90+
someString: 'String!',
91+
varWithDefault: 'String = "default_value"',
92+
},
93+
one: {
94+
__aliasFor: 'Posts',
95+
__args: {
96+
arg1: 20,
97+
arg2: new VariableType('someString'),
98+
status: new EnumType('PUBLISHED'),
99+
},
100+
name: false,
101+
id: true,
102+
title: true,
103+
comments: {
104+
__args: {
105+
offensiveOnly: true,
106+
},
107+
id: true,
108+
comment: true,
109+
user: true,
110+
},
111+
},
112+
Post: {
113+
__args: {
114+
arg1: 20,
115+
arg2: new VariableType('someString'),
116+
},
117+
__on: {
118+
__typeName: 'ConfigurablePost',
119+
id: true,
120+
},
121+
name: false,
122+
title: true,
123+
comments: {
124+
__args: {
125+
offensiveOnly: true,
126+
},
127+
id: true,
128+
comment: true,
129+
user: true,
130+
},
131+
},
132+
Posts: {
133+
__args: {
134+
arg1: 20,
135+
arg2: new VariableType('someString'),
136+
},
137+
name: false,
138+
id: true,
139+
title: true,
140+
comments: {
141+
__args: {
142+
offensiveOnly: true,
143+
},
144+
id: true,
145+
comment: true,
146+
user: true,
147+
},
148+
},
149+
},
150+
};
151+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
152+
`query NewName ($someString: String!, $varWithDefault: String = "default_value") {
153+
one: Posts (arg1: 20, arg2: $someString, status: PUBLISHED) {
154+
id
155+
title
156+
comments (offensiveOnly: true) {
157+
id
158+
comment
159+
user
160+
}
161+
}
162+
Post (arg1: 20, arg2: $someString) {
163+
title
164+
comments (offensiveOnly: true) {
165+
id
166+
comment
167+
user
168+
}
169+
... on ConfigurablePost {
170+
id
171+
}
172+
}
173+
Posts (arg1: 20, arg2: $someString) {
174+
id
175+
title
176+
comments (offensiveOnly: true) {
177+
id
178+
comment
179+
user
180+
}
181+
}
182+
}`
183+
);
184+
});
185+
});

src/jsonToGraphQLQuery.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EnumType } from './types/EnumType';
22
import { VariableType } from './types/VariableType';
33

44
export const configFields = [
5-
'__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__all_on', '__typeName'
5+
'__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__all_on', '__typeName', '__name'
66
];
77

88
function stringify(obj_from_json: any): string {
@@ -100,6 +100,10 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
100100

101101
let token = `${key}`;
102102

103+
if (typeof value.__name === 'string') {
104+
token = `${token} ${value.__name}`;
105+
}
106+
103107
if (typeof value.__aliasFor === 'string') {
104108
token = `${token}: ${value.__aliasFor}`;
105109
}

0 commit comments

Comments
 (0)