Skip to content

Commit 521383c

Browse files
committed
🐛 Added ListValue for editor puproses
1 parent 832772b commit 521383c

File tree

5 files changed

+240
-39
lines changed

5 files changed

+240
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",

src/Parser/typeResolver.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,23 @@ export class TypeResolver {
111111
*/
112112
static resolveValue(value: ValueNode): ParserField[] {
113113
if (value.kind === 'ListValue') {
114-
return value.values.map(TypeResolver.resolveValue).reduce((a, b) => [...(a || []), ...(b || [])], []);
114+
return [
115+
{
116+
name: value.kind,
117+
directives: [],
118+
interfaces: [],
119+
args: value.values.map((f) => TypeResolver.resolveValue(f)).reduce((a, b) => [...a, ...b], []),
120+
data: {
121+
type: value.kind as AllTypes,
122+
},
123+
type: {
124+
fieldType: {
125+
name: value.kind as AllTypes,
126+
type: Options.name,
127+
},
128+
},
129+
},
130+
];
115131
}
116132
if (value.kind === 'ObjectValue') {
117133
return [

src/TreeToGraphQL/templates/ValueTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ValueTemplate {
1616
returnedValue = `"${f.name}"`;
1717
}
1818
if (f.data.type === Value.ListValue) {
19-
returnedValue = `[${(f.args || []).map((a) => TemplateUtils.resolverForConnection(a))}]`;
19+
returnedValue = `[${(f.args || []).map((a) => TemplateUtils.resolverForConnection(a)).join(', ')}]`;
2020
}
2121
if (f.data.type === Value.ObjectValue) {
2222
returnedValue = `{ ${(f.args || []).map((a) => TemplateUtils.resolverForConnection(a))}}`;

src/__tests__/Parser/InputValue.spec.ts

Lines changed: 117 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ describe('Input Values tests on parser', () => {
317317
id: ${ScalarTypes.ID} = "abcdef"
318318
name: ${ScalarTypes.String} = "Artur"
319319
emptyName: ${ScalarTypes.String} = ""
320-
noName: ${ScalarTypes.String}
321320
emptyArray: [${ScalarTypes.String}] = []
322-
noArray: [${ScalarTypes.String}]
323321
age: ${ScalarTypes.Int} = 28
324322
weight: ${ScalarTypes.Float} = 73.0
325323
verified: ${ScalarTypes.Boolean} = true
@@ -434,21 +432,6 @@ describe('Input Values tests on parser', () => {
434432
},
435433
],
436434
},
437-
{
438-
name: 'noName',
439-
type: {
440-
fieldType: {
441-
name: ScalarTypes.String,
442-
type: Options.name,
443-
},
444-
},
445-
data: {
446-
type: ValueDefinition.InputValueDefinition,
447-
},
448-
directives: [],
449-
interfaces: [],
450-
args: [],
451-
},
452435
{
453436
name: 'emptyArray',
454437
type: {
@@ -464,27 +447,19 @@ describe('Input Values tests on parser', () => {
464447
type: ValueDefinition.InputValueDefinition,
465448
},
466449
directives: [],
467-
args: [],
468-
interfaces: [],
469-
},
470-
{
471-
name: 'noArray',
472-
type: {
473-
fieldType: {
474-
type: Options.array,
475-
nest: {
476-
type: Options.name,
477-
name: ScalarTypes.String,
478-
},
450+
args: [
451+
{
452+
args: [],
453+
data: { type: Value.ListValue },
454+
directives: [],
455+
interfaces: [],
456+
name: Value.ListValue,
457+
type: { fieldType: { name: Value.ListValue, type: Options.name } },
479458
},
480-
},
481-
data: {
482-
type: ValueDefinition.InputValueDefinition,
483-
},
484-
directives: [],
459+
],
485460
interfaces: [],
486-
args: [],
487461
},
462+
488463
{
489464
name: 'age',
490465
type: {
@@ -584,6 +559,113 @@ describe('Input Values tests on parser', () => {
584559
};
585560
expect(tree.nodes).toEqual(expect.arrayContaining(treeMock.nodes));
586561
});
562+
test(`Default ScalarTypes values - ${Object.keys(ScalarTypes).join(', ')}`, () => {
563+
const schema = `input Person{
564+
names: [${ScalarTypes.String}] = ["Artur","A","B"]
565+
}`;
566+
const tree = Parser.parse(schema);
567+
const treeMock: ParserTree = {
568+
nodes: [
569+
{
570+
name: 'Person',
571+
interfaces: [],
572+
type: {
573+
fieldType: {
574+
name: TypeDefinitionDisplayStrings.input,
575+
type: Options.name,
576+
},
577+
},
578+
data: {
579+
type: TypeDefinition.InputObjectTypeDefinition,
580+
},
581+
directives: [],
582+
args: [
583+
{
584+
name: 'names',
585+
type: {
586+
fieldType: {
587+
type: Options.array,
588+
nest: {
589+
name: ScalarTypes.String,
590+
type: Options.name,
591+
},
592+
},
593+
},
594+
data: {
595+
type: ValueDefinition.InputValueDefinition,
596+
},
597+
directives: [],
598+
interfaces: [],
599+
args: [
600+
{
601+
name: Value.ListValue,
602+
type: {
603+
fieldType: {
604+
name: Value.ListValue,
605+
type: Options.name,
606+
},
607+
},
608+
data: {
609+
type: Value.ListValue,
610+
},
611+
directives: [],
612+
interfaces: [],
613+
args: [
614+
{
615+
name: 'Artur',
616+
interfaces: [],
617+
args: [],
618+
directives: [],
619+
type: {
620+
fieldType: {
621+
name: Value.StringValue,
622+
type: Options.name,
623+
},
624+
},
625+
data: {
626+
type: Value.StringValue,
627+
},
628+
},
629+
{
630+
name: 'A',
631+
interfaces: [],
632+
args: [],
633+
directives: [],
634+
type: {
635+
fieldType: {
636+
name: Value.StringValue,
637+
type: Options.name,
638+
},
639+
},
640+
data: {
641+
type: Value.StringValue,
642+
},
643+
},
644+
{
645+
name: 'B',
646+
interfaces: [],
647+
args: [],
648+
directives: [],
649+
type: {
650+
fieldType: {
651+
name: Value.StringValue,
652+
type: Options.name,
653+
},
654+
},
655+
data: {
656+
type: Value.StringValue,
657+
},
658+
},
659+
],
660+
},
661+
],
662+
},
663+
],
664+
},
665+
],
666+
};
667+
expect(tree.nodes).toEqual(expect.arrayContaining(treeMock.nodes));
668+
});
587669

588670
test('Default Input objects', () => {
589671
const schema = `

src/__tests__/TreeToGraphQL/InputValue.spec.ts

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,110 @@ describe('Input Values tests on TreeToGraphQL', () => {
479479
expect(graphql).toContain(`weight: ${ScalarTypes.Float} = 73.0`);
480480
expect(graphql).toContain(`verified: ${ScalarTypes.Boolean} = true`);
481481
});
482-
482+
test('Default Input Arrays', () => {
483+
const treeMock: ParserTree = {
484+
nodes: [
485+
{
486+
name: 'Person',
487+
interfaces: [],
488+
type: {
489+
fieldType: {
490+
name: TypeDefinitionDisplayStrings.input,
491+
type: Options.name,
492+
},
493+
},
494+
data: {
495+
type: TypeDefinition.InputObjectTypeDefinition,
496+
},
497+
directives: [],
498+
args: [
499+
{
500+
name: 'names',
501+
type: {
502+
fieldType: {
503+
type: Options.array,
504+
nest: {
505+
name: ScalarTypes.String,
506+
type: Options.name,
507+
},
508+
},
509+
},
510+
data: {
511+
type: ValueDefinition.InputValueDefinition,
512+
},
513+
directives: [],
514+
interfaces: [],
515+
args: [
516+
{
517+
name: Value.ListValue,
518+
interfaces: [],
519+
args: [
520+
{
521+
name: 'Artur',
522+
interfaces: [],
523+
args: [],
524+
directives: [],
525+
type: {
526+
fieldType: {
527+
name: Value.StringValue,
528+
type: Options.name,
529+
},
530+
},
531+
data: {
532+
type: Value.StringValue,
533+
},
534+
},
535+
{
536+
name: 'A',
537+
interfaces: [],
538+
args: [],
539+
directives: [],
540+
type: {
541+
fieldType: {
542+
name: Value.StringValue,
543+
type: Options.name,
544+
},
545+
},
546+
data: {
547+
type: Value.StringValue,
548+
},
549+
},
550+
{
551+
name: 'B',
552+
interfaces: [],
553+
args: [],
554+
directives: [],
555+
type: {
556+
fieldType: {
557+
name: Value.StringValue,
558+
type: Options.name,
559+
},
560+
},
561+
data: {
562+
type: Value.StringValue,
563+
},
564+
},
565+
],
566+
directives: [],
567+
type: {
568+
fieldType: {
569+
name: Value.ListValue,
570+
type: Options.name,
571+
},
572+
},
573+
data: {
574+
type: Value.ListValue,
575+
},
576+
},
577+
],
578+
},
579+
],
580+
},
581+
],
582+
};
583+
const graphql = TreeToGraphQL.parse(treeMock);
584+
expect(graphql).toContain(`names: [String] = [`);
585+
});
483586
test('Default Input objects', () => {
484587
const treeMock: ParserTree = {
485588
nodes: [

0 commit comments

Comments
 (0)