|
7 | 7 | from graphql.language.parser import parse
|
8 | 8 | from graphql.type import (GraphQLArgument, GraphQLBoolean, GraphQLField,
|
9 | 9 | GraphQLInt, GraphQLList, GraphQLObjectType,
|
10 |
| - GraphQLSchema, GraphQLString) |
| 10 | + GraphQLSchema, GraphQLString, GraphQLNonNull) |
11 | 11 | from promise import Promise
|
12 | 12 |
|
13 | 13 |
|
@@ -668,3 +668,148 @@ def resolve(self, next, *args, **kwargs):
|
668 | 668 | middleware=middlewares_without_promise)
|
669 | 669 | assert result1.data == result2.data and result1.data == {
|
670 | 670 | 'ok': 'ok', 'not_ok': 'not_ok'}
|
| 671 | + |
| 672 | + |
| 673 | +def test_executor_properly_propogates_path_data(mocker): |
| 674 | + time_mock = mocker.patch('time.time') |
| 675 | + time_mock.side_effect = range(0, 10000) |
| 676 | + |
| 677 | + BlogImage = GraphQLObjectType('BlogImage', { |
| 678 | + 'url': GraphQLField(GraphQLString), |
| 679 | + 'width': GraphQLField(GraphQLInt), |
| 680 | + 'height': GraphQLField(GraphQLInt), |
| 681 | + }) |
| 682 | + |
| 683 | + BlogAuthor = GraphQLObjectType('Author', lambda: { |
| 684 | + 'id': GraphQLField(GraphQLString), |
| 685 | + 'name': GraphQLField(GraphQLString), |
| 686 | + 'pic': GraphQLField(BlogImage, |
| 687 | + args={ |
| 688 | + 'width': GraphQLArgument(GraphQLInt), |
| 689 | + 'height': GraphQLArgument(GraphQLInt), |
| 690 | + }, |
| 691 | + resolver=lambda obj, info, **args: |
| 692 | + obj.pic(args['width'], args['height']) |
| 693 | + ), |
| 694 | + 'recentArticle': GraphQLField(BlogArticle), |
| 695 | + }) |
| 696 | + |
| 697 | + BlogArticle = GraphQLObjectType('Article', { |
| 698 | + 'id': GraphQLField(GraphQLNonNull(GraphQLString)), |
| 699 | + 'isPublished': GraphQLField(GraphQLBoolean), |
| 700 | + 'author': GraphQLField(BlogAuthor), |
| 701 | + 'title': GraphQLField(GraphQLString), |
| 702 | + 'body': GraphQLField(GraphQLString), |
| 703 | + 'keywords': GraphQLField(GraphQLList(GraphQLString)), |
| 704 | + }) |
| 705 | + |
| 706 | + BlogQuery = GraphQLObjectType('Query', { |
| 707 | + 'article': GraphQLField( |
| 708 | + BlogArticle, |
| 709 | + args={'id': GraphQLArgument(GraphQLID)}, |
| 710 | + resolver=lambda obj, info, **args: Article(args['id'])), |
| 711 | + 'feed': GraphQLField( |
| 712 | + GraphQLList(BlogArticle), |
| 713 | + resolver=lambda *_: map(Article, range(1, 2 + 1))), |
| 714 | + }) |
| 715 | + |
| 716 | + BlogSchema = GraphQLSchema(BlogQuery) |
| 717 | + |
| 718 | + class Article(object): |
| 719 | + |
| 720 | + def __init__(self, id): |
| 721 | + self.id = id |
| 722 | + self.isPublished = True |
| 723 | + self.author = Author() |
| 724 | + self.title = 'My Article {}'.format(id) |
| 725 | + self.body = 'This is a post' |
| 726 | + self.hidden = 'This data is not exposed in the schema' |
| 727 | + self.keywords = ['foo', 'bar', 1, True, None] |
| 728 | + |
| 729 | + class Author(object): |
| 730 | + id = 123 |
| 731 | + name = 'John Smith' |
| 732 | + |
| 733 | + def pic(self, width, height): |
| 734 | + return Pic(123, width, height) |
| 735 | + |
| 736 | + @property |
| 737 | + def recentArticle(self): return Article(1) |
| 738 | + |
| 739 | + class Pic(object): |
| 740 | + def __init__(self, uid, width, height): |
| 741 | + self.url = 'cdn://{}'.format(uid) |
| 742 | + self.width = str(width) |
| 743 | + self.height = str(height) |
| 744 | + |
| 745 | + class PathCollectorMiddleware(object): |
| 746 | + def __init__(self): |
| 747 | + self.paths = [] |
| 748 | + |
| 749 | + def resolve(self, _next, root, info, *args, **kwargs): |
| 750 | + self.paths.append(info.path) |
| 751 | + return _next(root, info, *args, **kwargs) |
| 752 | + |
| 753 | + request = ''' |
| 754 | + { |
| 755 | + feed { |
| 756 | + id |
| 757 | + ...articleFields |
| 758 | + author { |
| 759 | + id |
| 760 | + name |
| 761 | + } |
| 762 | + }, |
| 763 | + } |
| 764 | + fragment articleFields on Article { |
| 765 | + title, |
| 766 | + body, |
| 767 | + hidden, |
| 768 | + } |
| 769 | + ''' |
| 770 | + |
| 771 | + paths_middleware = PathCollectorMiddleware() |
| 772 | + |
| 773 | + result = execute(BlogSchema, parse(request), middleware=(paths_middleware, )) |
| 774 | + assert not result.errors |
| 775 | + assert result.data == \ |
| 776 | + { |
| 777 | + "feed": [ |
| 778 | + { |
| 779 | + "id": "1", |
| 780 | + "title": "My Article 1", |
| 781 | + "body": "This is a post", |
| 782 | + "author": { |
| 783 | + "id": "123", |
| 784 | + "name": "John Smith" |
| 785 | + } |
| 786 | + }, |
| 787 | + { |
| 788 | + "id": "2", |
| 789 | + "title": "My Article 2", |
| 790 | + "body": "This is a post", |
| 791 | + "author": { |
| 792 | + "id": "123", |
| 793 | + "name": "John Smith" |
| 794 | + } |
| 795 | + }, |
| 796 | + ], |
| 797 | + } |
| 798 | + |
| 799 | + traversed_paths = paths_middleware.paths |
| 800 | + assert traversed_paths == [ |
| 801 | + ['feed'], |
| 802 | + ['feed', 0, 'id'], |
| 803 | + ['feed', 0, 'title'], |
| 804 | + ['feed', 0, 'body'], |
| 805 | + ['feed', 0, 'author'], |
| 806 | + ['feed', 1, 'id'], |
| 807 | + ['feed', 1, 'title'], |
| 808 | + ['feed', 1, 'body'], |
| 809 | + ['feed', 1, 'author'], |
| 810 | + ['feed', 0, 'author', 'id'], |
| 811 | + ['feed', 0, 'author', 'name'], |
| 812 | + ['feed', 1, 'author', 'id'], |
| 813 | + ['feed', 1, 'author', 'name'] |
| 814 | + ] |
| 815 | + |
0 commit comments