Skip to content

Commit 942f84e

Browse files
committed
Merge pull request #326 from graphql/context
[RFC] Add explicit context arg to graphql execution
2 parents 576b6a1 + d7cc6f9 commit 942f84e

File tree

10 files changed

+97
-62
lines changed

10 files changed

+97
-62
lines changed

src/__tests__/starWarsQueryTests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Star Wars Query Tests', () => {
181181
name: 'Luke Skywalker'
182182
}
183183
};
184-
const result = await graphql(StarWarsSchema, query, null, params);
184+
const result = await graphql(StarWarsSchema, query, null, null, params);
185185
expect(result).to.deep.equal({ data: expected });
186186
});
187187

@@ -201,7 +201,7 @@ describe('Star Wars Query Tests', () => {
201201
name: 'Han Solo'
202202
}
203203
};
204-
const result = await graphql(StarWarsSchema, query, null, params);
204+
const result = await graphql(StarWarsSchema, query, null, null, params);
205205
expect(result).to.deep.equal({ data: expected });
206206
});
207207

@@ -219,7 +219,7 @@ describe('Star Wars Query Tests', () => {
219219
const expected = {
220220
human: null
221221
};
222-
const result = await graphql(StarWarsSchema, query, null, params);
222+
const result = await graphql(StarWarsSchema, query, null, null, params);
223223
expect(result).to.deep.equal({ data: expected });
224224
});
225225
});

src/execution/__tests__/executor.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('Execute: Handles basic execution tasks', () => {
137137
});
138138

139139
expect(
140-
await execute(schema, ast, data, { size: 100 }, 'Example')
140+
await execute(schema, ast, data, null, { size: 100 }, 'Example')
141141
).to.deep.equal(expected);
142142
});
143143

@@ -427,7 +427,7 @@ describe('Execute: Handles basic execution tasks', () => {
427427
})
428428
});
429429

430-
const result = await execute(schema, ast, data, null, 'OtherExample');
430+
const result = await execute(schema, ast, data, null, null, 'OtherExample');
431431

432432
expect(result).to.deep.equal({ data: { second: 'b' } });
433433
});
@@ -481,7 +481,9 @@ describe('Execute: Handles basic execution tasks', () => {
481481
})
482482
});
483483

484-
expect(() => execute(schema, ast, data, null, 'UnknownExample')).to.throw(
484+
expect(() =>
485+
execute(schema, ast, data, null, null, 'UnknownExample')
486+
).to.throw(
485487
'Unknown operation named "UnknownExample".'
486488
);
487489
});
@@ -511,7 +513,7 @@ describe('Execute: Handles basic execution tasks', () => {
511513
})
512514
});
513515

514-
const queryResult = await execute(schema, ast, data, {}, 'Q');
516+
const queryResult = await execute(schema, ast, data, null, {}, 'Q');
515517

516518
expect(queryResult).to.deep.equal({ data: { a: 'b' } });
517519
});
@@ -535,7 +537,7 @@ describe('Execute: Handles basic execution tasks', () => {
535537
})
536538
});
537539

538-
const mutationResult = await execute(schema, ast, data, {}, 'M');
540+
const mutationResult = await execute(schema, ast, data, null, {}, 'M');
539541

540542
expect(mutationResult).to.deep.equal({ data: { c: 'd' } });
541543
});
@@ -559,7 +561,7 @@ describe('Execute: Handles basic execution tasks', () => {
559561
})
560562
});
561563

562-
const subscriptionResult = await execute(schema, ast, data, {}, 'S');
564+
const subscriptionResult = await execute(schema, ast, data, null, {}, 'S');
563565

564566
expect(subscriptionResult).to.deep.equal({ data: { a: 'b' } });
565567
});
@@ -644,7 +646,7 @@ describe('Execute: Handles basic execution tasks', () => {
644646
}),
645647
});
646648

647-
const queryResult = await execute(schema, ast, data, {}, 'Q');
649+
const queryResult = await execute(schema, ast, data, null, {}, 'Q');
648650

649651
expect(queryResult).to.deep.equal({ data: { a: 'b' } });
650652
});

src/execution/__tests__/union-interface.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ describe('Execute: Union and intersection types', () => {
349349
});
350350

351351
it('gets execution info in resolver', async () => {
352+
let encounteredContext;
352353
let encounteredSchema;
353354
let encounteredRootValue;
354355

@@ -357,9 +358,10 @@ describe('Execute: Union and intersection types', () => {
357358
fields: {
358359
name: { type: GraphQLString }
359360
},
360-
resolveType(obj, { schema: infoSchema, rootValue: infoRootValue }) {
361-
encounteredSchema = infoSchema;
362-
encounteredRootValue = infoRootValue;
361+
resolveType(obj, context, { schema: _schema, rootValue }) {
362+
encounteredContext = context;
363+
encounteredSchema = _schema;
364+
encounteredRootValue = rootValue;
363365
return PersonType2;
364366
}
365367
});
@@ -379,14 +381,17 @@ describe('Execute: Union and intersection types', () => {
379381

380382
const john2 = new Person('John', [], [ liz ]);
381383

384+
const context = { authToken: '123abc' };
385+
382386
const ast = parse('{ name, friends { name } }');
383387

384388
expect(
385-
await execute(schema2, ast, john2)
389+
await execute(schema2, ast, john2, context)
386390
).to.deep.equal({
387391
data: { name: 'John', friends: [ { name: 'Liz' } ] }
388392
});
389393

394+
expect(encounteredContext).to.equal(context);
390395
expect(encounteredSchema).to.equal(schema2);
391396
expect(encounteredRootValue).to.equal(john2);
392397
});

src/execution/__tests__/variables.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('Execute: Handles inputs', () => {
204204

205205
it('executes with complex input', async () => {
206206
const params = { input: { a: 'foo', b: [ 'bar' ], c: 'baz' } };
207-
const result = await execute(schema, ast, null, params);
207+
const result = await execute(schema, ast, null, null, params);
208208

209209
return expect(result).to.deep.equal({
210210
data: {
@@ -231,7 +231,7 @@ describe('Execute: Handles inputs', () => {
231231

232232
it('properly parses single value to list', async () => {
233233
const params = { input: { a: 'foo', b: 'bar', c: 'baz' } };
234-
const result = await execute(schema, ast, null, params);
234+
const result = await execute(schema, ast, null, null, params);
235235

236236
return expect(result).to.deep.equal({
237237
data: {
@@ -242,7 +242,7 @@ describe('Execute: Handles inputs', () => {
242242

243243
it('executes with complex scalar input', async () => {
244244
const params = { input: { c: 'foo', d: 'SerializedValue' } };
245-
const result = await execute(schema, ast, null, params);
245+
const result = await execute(schema, ast, null, null, params);
246246

247247
return expect(result).to.deep.equal({
248248
data: {
@@ -256,7 +256,7 @@ describe('Execute: Handles inputs', () => {
256256

257257
let caughtError;
258258
try {
259-
execute(schema, ast, null, params);
259+
execute(schema, ast, null, null, params);
260260
} catch (error) {
261261
caughtError = error;
262262
}
@@ -275,7 +275,7 @@ describe('Execute: Handles inputs', () => {
275275

276276
let caughtError;
277277
try {
278-
execute(schema, ast, null, params);
278+
execute(schema, ast, null, null, params);
279279
} catch (error) {
280280
caughtError = error;
281281
}
@@ -293,7 +293,7 @@ describe('Execute: Handles inputs', () => {
293293

294294
let caughtError;
295295
try {
296-
execute(schema, ast, null, params);
296+
execute(schema, ast, null, null, params);
297297
} catch (error) {
298298
caughtError = error;
299299
}
@@ -317,7 +317,7 @@ describe('Execute: Handles inputs', () => {
317317

318318
let caughtError;
319319
try {
320-
execute(schema, nestedAst, null, params);
320+
execute(schema, nestedAst, null, null, params);
321321
} catch (error) {
322322
caughtError = error;
323323
}
@@ -339,7 +339,7 @@ describe('Execute: Handles inputs', () => {
339339

340340
let caughtError;
341341
try {
342-
execute(schema, ast, null, params);
342+
execute(schema, ast, null, null, params);
343343
} catch (error) {
344344
caughtError = error;
345345
}
@@ -411,7 +411,7 @@ describe('Execute: Handles inputs', () => {
411411
const ast = parse(doc);
412412

413413
return expect(
414-
await execute(schema, ast, null, { value: null })
414+
await execute(schema, ast, null, null, { value: null })
415415
).to.deep.equal({
416416
data: {
417417
fieldWithNullableStringInput: null
@@ -428,7 +428,7 @@ describe('Execute: Handles inputs', () => {
428428
const ast = parse(doc);
429429

430430
return expect(
431-
await execute(schema, ast, null, { value: 'a' })
431+
await execute(schema, ast, null, null, { value: 'a' })
432432
).to.deep.equal({
433433
data: {
434434
fieldWithNullableStringInput: '"a"'
@@ -484,7 +484,7 @@ describe('Execute: Handles inputs', () => {
484484

485485
let caughtError;
486486
try {
487-
execute(schema, ast, null, { value: null });
487+
execute(schema, ast, null, null, { value: null });
488488
} catch (error) {
489489
caughtError = error;
490490
}
@@ -505,7 +505,7 @@ describe('Execute: Handles inputs', () => {
505505
const ast = parse(doc);
506506

507507
return expect(
508-
await execute(schema, ast, null, { value: 'a' })
508+
await execute(schema, ast, null, null, { value: 'a' })
509509
).to.deep.equal({
510510
data: {
511511
fieldWithNonNullableStringInput: '"a"'
@@ -554,7 +554,7 @@ describe('Execute: Handles inputs', () => {
554554
const ast = parse(doc);
555555

556556
return expect(
557-
await execute(schema, ast, null, { input: null })
557+
await execute(schema, ast, null, null, { input: null })
558558
).to.deep.equal({
559559
data: {
560560
list: null
@@ -571,7 +571,7 @@ describe('Execute: Handles inputs', () => {
571571
const ast = parse(doc);
572572

573573
return expect(
574-
await execute(schema, ast, null, { input: [ 'A' ] })
574+
await execute(schema, ast, null, null, { input: [ 'A' ] })
575575
).to.deep.equal({
576576
data: {
577577
list: '["A"]'
@@ -588,7 +588,7 @@ describe('Execute: Handles inputs', () => {
588588
const ast = parse(doc);
589589

590590
return expect(
591-
await execute(schema, ast, null, { input: [ 'A', null, 'B' ] })
591+
await execute(schema, ast, null, null, { input: [ 'A', null, 'B' ] })
592592
).to.deep.equal({
593593
data: {
594594
list: '["A",null,"B"]'
@@ -606,7 +606,7 @@ describe('Execute: Handles inputs', () => {
606606

607607
let caughtError;
608608
try {
609-
execute(schema, ast, null, { input: null });
609+
execute(schema, ast, null, null, { input: null });
610610
} catch (error) {
611611
caughtError = error;
612612
}
@@ -627,7 +627,7 @@ describe('Execute: Handles inputs', () => {
627627
const ast = parse(doc);
628628

629629
return expect(
630-
await execute(schema, ast, null, { input: [ 'A' ] })
630+
await execute(schema, ast, null, null, { input: [ 'A' ] })
631631
).to.deep.equal({
632632
data: {
633633
nnList: '["A"]'
@@ -644,7 +644,7 @@ describe('Execute: Handles inputs', () => {
644644
const ast = parse(doc);
645645

646646
return expect(
647-
await execute(schema, ast, null, { input: [ 'A', null, 'B' ] })
647+
await execute(schema, ast, null, null, { input: [ 'A', null, 'B' ] })
648648
).to.deep.equal({
649649
data: {
650650
nnList: '["A",null,"B"]'
@@ -661,7 +661,7 @@ describe('Execute: Handles inputs', () => {
661661
const ast = parse(doc);
662662

663663
return expect(
664-
await execute(schema, ast, null, { input: null })
664+
await execute(schema, ast, null, null, { input: null })
665665
).to.deep.equal({
666666
data: {
667667
listNN: null
@@ -678,7 +678,7 @@ describe('Execute: Handles inputs', () => {
678678
const ast = parse(doc);
679679

680680
return expect(
681-
await execute(schema, ast, null, { input: [ 'A' ] })
681+
await execute(schema, ast, null, null, { input: [ 'A' ] })
682682
).to.deep.equal({
683683
data: {
684684
listNN: '["A"]'
@@ -697,7 +697,7 @@ describe('Execute: Handles inputs', () => {
697697

698698
let caughtError;
699699
try {
700-
execute(schema, ast, null, vars);
700+
execute(schema, ast, null, null, vars);
701701
} catch (error) {
702702
caughtError = error;
703703
}
@@ -720,7 +720,7 @@ describe('Execute: Handles inputs', () => {
720720

721721
let caughtError;
722722
try {
723-
execute(schema, ast, null, { input: null });
723+
execute(schema, ast, null, null, { input: null });
724724
} catch (error) {
725725
caughtError = error;
726726
}
@@ -741,7 +741,7 @@ describe('Execute: Handles inputs', () => {
741741
const ast = parse(doc);
742742

743743
return expect(
744-
await execute(schema, ast, null, { input: [ 'A' ] })
744+
await execute(schema, ast, null, null, { input: [ 'A' ] })
745745
).to.deep.equal({
746746
data: {
747747
nnListNN: '["A"]'
@@ -760,7 +760,7 @@ describe('Execute: Handles inputs', () => {
760760

761761
let caughtError;
762762
try {
763-
execute(schema, ast, null, vars);
763+
execute(schema, ast, null, null, vars);
764764
} catch (error) {
765765
caughtError = error;
766766
}
@@ -784,7 +784,7 @@ describe('Execute: Handles inputs', () => {
784784

785785
let caughtError;
786786
try {
787-
execute(schema, ast, null, vars);
787+
execute(schema, ast, null, null, vars);
788788
} catch (error) {
789789
caughtError = error;
790790
}
@@ -808,7 +808,7 @@ describe('Execute: Handles inputs', () => {
808808

809809
let caughtError;
810810
try {
811-
execute(schema, ast, null, vars);
811+
execute(schema, ast, null, null, vars);
812812
} catch (error) {
813813
caughtError = error;
814814
}

0 commit comments

Comments
 (0)