跳转到主要内容

用于从strawberry graphql提取数据的检查工具

项目描述

strawberry-resources

build status coverage downloads PyPI version python version

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 schema
  • resource: 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 dictionary
  • to_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-djangostrawberry-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 (17.3 kB 查看哈希值)

上传时间

构建分布

strawberry_resources-0.10.0-py3-none-any.whl (18.6 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面