Skip to content

Commit a88e9f5

Browse files
LoicMahieumickhansen
authored andcommitted
simplifyAST: resolve array args (#256)
1 parent 230a585 commit a88e9f5

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/simplifyAST.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ module.exports = function simplifyAST(ast, info, parent) {
6464
}
6565

6666
simpleAST.fields[key].args = selection.arguments.reduce(function (args, arg) {
67-
args[arg.name.value] = arg.value.value;
67+
if (arg.value.values) {
68+
args[arg.name.value] = arg.value.values.map(value => value.value);
69+
} else {
70+
args[arg.name.value] = arg.value.value;
71+
}
6872
return args;
6973
}, {});
7074

test/integration/resolver.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ describe('resolver', function () {
171171
}
172172
}
173173

174+
return options;
175+
}
176+
})
177+
},
178+
tasksByIds: {
179+
type: new GraphQLList(taskType),
180+
args: {
181+
ids: {
182+
type: new GraphQLList(GraphQLInt)
183+
}
184+
},
185+
resolve: resolver(User.Tasks, {
186+
before: (options, args) => {
187+
options.where = options.where || {};
188+
options.where.id = { $in: args.ids };
174189
return options;
175190
}
176191
})
@@ -1091,6 +1106,24 @@ describe('resolver', function () {
10911106
});
10921107
});
10931108

1109+
it('should resolve args from array to before', function () {
1110+
var user = this.userB;
1111+
1112+
return graphql(schema, `
1113+
{
1114+
user(id: ${user.get('id')}) {
1115+
tasksByIds(ids: [${user.tasks[0].get('id')}]) {
1116+
id
1117+
}
1118+
}
1119+
}
1120+
`).then(function (result) {
1121+
if (result.errors) throw new Error(result.errors[0].stack);
1122+
1123+
expect(result.data.user.tasksByIds.length).to.equal(1);
1124+
});
1125+
});
1126+
10941127
describe('filterAttributes', function () {
10951128
beforeEach(function () {
10961129
this.defaultValue = resolver.filterAttributes;

test/unit/simplifyAST.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ describe('simplifyAST', function () {
7272
});
7373
});
7474

75+
it('should simplify a basic structure with array args', function () {
76+
expect(simplifyAST(parse(`
77+
{
78+
luke: human(id: ["1000", "1003"]) {
79+
name
80+
}
81+
}
82+
`))).to.deep.equal({
83+
args: {},
84+
fields: {
85+
luke: {
86+
key: "human",
87+
args: {
88+
id: ["1000", "1003"]
89+
},
90+
fields: {
91+
name: {
92+
args: {},
93+
fields: {}
94+
}
95+
}
96+
}
97+
}
98+
})
99+
});
100+
75101
it('should simplify a basic structure with an inline fragment', function () {
76102
expect(simplifyAST(parse(`
77103
{
@@ -258,4 +284,4 @@ describe('simplifyAST', function () {
258284
}
259285
})
260286
});
261-
});
287+
});

0 commit comments

Comments
 (0)