graphql-python / graphql-python/graphene

[Documentation] Fix TypeError and make the examples in ObjectType > Resolver Parameters > Parent Value Object section self contained

Đang mở
#1,239 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
🐛 bug
Ngôn ngữ chính
Python
Star
8.2k
Fork
818
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

**Note: for support questions, please use stackoverflow**. This repository's issues are reserved for feature requests and bug reports.

* **What is the current behavior?**
You can't actually execute the example under the **Parent Value Object** section. There are mainly two issues. Let's have a look at the code snippet:

```python
from graphene import ObjectType, String, Field

class Person(ObjectType):
full_name = String()

def resolve_full_name(parent, info):
return f"{parent.first_name} {parent.last_name}"

class Query(ObjectType):
me = Field(Person)

def resolve_me(parent, info):
# returns an object that represents a Person
return get_human(name="Luke Skywalker")
```
* From a beginner's perspective, it's unclear how the `resolve_full_name` method has access to `first_name` and `last_name` when they aren't defined in the `Person` class.
* Seems like this `get_human` function(or method?) came out of nowhere.

Then it gets executed like this:

```python

schema = Schema(query=Query)

query_string = "{ me { fullName } }"
result = schema.execute(query_string)

assert result["data"]["me"] == {"fullName": "Luke Skywalker")
```

* The last line of the above snippet has a misplaced parenthesis
* Even if you fix that one, it raises `TypeError` saying `TypeError: 'ExecutionResult' object is not subscriptable`. `result` object has a `to_dict` method, maybe we should use that here.

* **If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem** via
a Github repo, https://repl.it or similar.

* **What is the expected behavior?**
At this stage of the documentation, the example should be self-contained, even if it gets a bit verbose.

* **What is the motivation / use case for changing the behavior?**

Maybe this is the expected behavior?

```python
from graphene import ObjectType, String, Field, Schema

class Person(ObjectType):
first_name = String()
last_name = String()
full_name = String()

def resolve_full_name(parent, info):
return f"{parent.first_name} {parent.last_name}"

def get_human(name):
first_name, last_name = name.split()
return Person(first_name=first_name, last_name=last_name)

class Query(ObjectType):
me = Field(Person)

def resolve_me(parent, info):
# returns an object that represents a Person
return get_human(name="Luke Skywalker")

schema = Schema(query=Query)

query_string = "{ me { fullName } }"
result = schema.execute(query_string)

assert result.to_dict()["data"]["me"] == {"fullName": "Luke Skywalker"}
```

* **Please tell us about your environment:**

- Version: graphql-core==2.3.2, graphql-relay==2.0.1
- Platform: Ubuntu 20.04

* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu từ phần tài liệu ObjectType > Resolver Parameters > Parent Value Object được nêu trong issue. Làm cho ví dụ tự chứa bằng cách định nghĩa các trường được tham chiếu và hàm get_human, sửa cú pháp assertion và sử dụng chuyển đổi ExecutionResult được tài liệu hóa để ví dụ thực thi thành công.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
graphql, python
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.