用于从strawberry graphql提取数据的检查工具
项目描述
strawberry-resources
Introspection utilities to extract data from the schema to use as helpers in the client, like building an automatic form for input types.
安装
只需使用pip或您首选的包管理器安装此包
pip install strawberry-resources
如何使用
查询中的使用
This lib provides a Query
type that has two queries
resources
: Returns a list of all available resources in the schemaresource
: Returns an specific resource given its name
You can use merge_type to merge it with your own Query
type.
然后,给定以下示例
@strawberry.enum
class Color(enum.Enum):
YELLOW = strawberry.enum_value("yellow", description="Color Yellow")
RED = "red"
ORANGE = "orange"
@strawberry.type
class Fruit:
name: str
color: Annotated[Color, config(label="Color")]
weight: Annotate[float, strawberry_resource.config(label="Weight")]
@strawberry.type
class Market:
name: Annotate[str, strawberry_resource.config(label="Market Name")]
fruits: Annotate[List[Fruit], strawberry_resource.config(label="Fruits")]
@strawberry.type
class Query:
market: Market
You can query resource(name: "Market")
which would return
{
"resource": {
"name": "Market"
"fields": [
{
"__typename": "Field",
"choices": null,
"defaultValue": null,
"filterable": false,
"helpText": null,
"kind": "STRING",
"label": "Market Name",
"multiple": false,
"name": "name",
"orderable": false,
"resource": null,
"validation": {
"__typename": "BaseFieldValidation",
"required": true
}
},
{
"__typename": "FieldObject",
"label": "Fruits",
"name": "fruits",
"objKind": "OBJECT_LIST"
"fields": [
{
"__typename": "Field",
"choices": null,
"defaultValue": null,
"filterable": false,
"helpText": null,
"kind": "STRING",
"label": "name",
"multiple": false,
"name": "name",
"orderable": false,
"resource": null,
"validation": {
"__typename": "BaseFieldValidation",
"required": true
}
},
{
"__typename": "Field",
"choices": [
{
"group": null,
"label": "Color Yellow",
"value": "YELLOW"
},
{
"group": null,
"label": "RED",
"value": "RED"
},
{
"group": null,
"label": "ORANGE",
"value": "ORANGE"
}
],
"defaultValue": null,
"filterable": false,
"helpText": null,
"kind": "STRING",
"label": "Color",
"multiple": false,
"name": "color",
"orderable": false,
"resource": null,
"validation": {
"__typename": "BaseFieldValidation",
"required": true
}
},
{
"__typename": "Field",
"choices": null,
"defaultValue": null,
"filterable": false,
"helpText": null,
"kind": "FLOAT",
"label": "Weight",
"multiple": false,
"name": "weight",
"orderable": false,
"resource": null,
"validation": {
"__typename": "BaseFieldValidation",
"required": true
}
}
],
}
],
}
}
导出资源
You can also use the resources statically by exporting them by using the command
strawberry_resources export --app-dir <schema>
The export functions are also exposed in strawberry_resources.exporter
. There are 2 functions there
to_dict
: Will export the resources to a dictionaryto_json
:将资源导出为JSON字符串(用于上述命令)
自定义资源
Strawberry资源将自动分析模式以填充有关字段的某些信息。但是,您可以通过对字段进行注释来自定义它们。
在上面的示例中,我们自定义了大多数属性的label
,除了Fruit.name
。所有可能的配置选项如下
kind
(FieldKind
):字段的类型multiple
(bool
):字段是否为多值(即列表)orderable
(bool
):字段是否可排序filterable
(bool
):字段是否可过滤label
(str | None
):字段的可选友好标签help_text
(str | FieldChoice
):字段的可用选项的可选列表default_value
(JSON | None
):字段的默认值validation
(BaseFieldValidation
):字段的验证选项
有关更多详细信息,请参阅types.py模块。
集成
Django
如果您正在使用Django,并通过扩展strawberry-graphql-django或strawberry-django-plus,则集成将自动使用来配置一些选项,通过分析您的模型。
以下信息将从其中的字段中检索,特别是在使用strawberry.auto
进行类型定义时
kind
:字段类型将自动根据模型字段类型设置。例如,一个CharField
将生成一个类型为STRING
的字段,一个DateTimeField
将生成一个类型为DATETIME
的字段,依此类推。orderable
:如果Django类型上设置了排序,并且字段本身存在,则会自动填充filterable
:如果Django类型上设置了过滤器,并且字段本身存在,则会自动填充label
:将自动使用字段的verbose_name
值进行填充help_text
:将自动使用字段的help_text
值进行填充choices
:将自动使用字段的choices
值进行填充default_value
:将自动使用字段的default
值进行填充
创建自己的集成
您可以通过创建strawberry_resources.integrations.StrawberryResourceIntegration
的实例来创建自己的扩展。它期望4个属性
name
:集成的名称get_extra_mappings
:一个可调用的函数,应返回一个将类型映射到FieldKind
的字典get_field_options
:一个映射,接收包含字段的类型,字段本身,字段解析的类型以及它是否是一个列表。它应返回一个包含上述部分中提到的选项的字典。order
:用于运行集成时的可选顺序。
集成将按其定义的顺序运行。此存储库中的官方集成都有一致的顺序0
,因此您可以通过传递一个负值来定义在它们之前运行,或者通过传递一个大于0
的值来定义在它们之后运行。
注意:strawberry-resources渴望拥有更多的集成,所以请随意为我们打开一个PR发送您的! :)
选项如何解析
所有选项都将递归合并以生成最终资源选项。这意味着稍后定义的选项将覆盖先前定义的选项。顺序如下
- 选项将使用从类型映射中检索的其
kind
创建,并且其label
将默认设置为与其名称相同的值。 - 集成将按照它们定义的顺序运行,每个返回的选项将与当前选项递归合并。
- 最后,将通过字段的注解检索选项,在与其他选项合并时具有最高优先级。
许可
本项目中的代码采用MIT许可证。有关更多信息,请参阅LICENSE。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
strawberry_resources-0.10.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6267d5e1b68d2e667fafc3456d07d5ac85eb7376d457462d8076825270de749c |
|
MD5 | 8a9f33bdfe8755c4fbac9bcf9073546f |
|
BLAKE2b-256 | 34acc0dc4f8a06fe32c5e06c893a26b53e15f36ec245087b90c3acc517889f25 |
strawberry_resources-0.10.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 116c576a1e3ae64a64f586fccea79c9bed8823842128c3b4feadca58f1c2b5fd |
|
MD5 | c3f2407e7954e576b4ec11f6d81839e9 |
|
BLAKE2b-256 | 77769d5e39cb37184d206b3790f1ccbf0271eb23653225876fec319052d1f77a |