簡體   English   中英

多邊形燈塔graphql類型

[英]polygon lighthouse graphql type

我在laravel 5.7應用程序中使用燈塔PHP。 我正在嘗試在架構中定義一個幾何字段(多邊形),但是效果不佳。

誰能幫助我如何在我的graphql模式中定義多邊形字段。 模式和查詢代碼

根據您的圖像,您尚未為Polygon創建類型。

由於您尚未真正指定要如何說明多邊形,所以我將僅顯示一個示例。 在此示例中,我將創建一個新的GraphQl標量類型。

指定多邊形的常用方法是使用一組坐標集,如下所示

((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))

為此,我將創建一個新的標量

"A string representation of a polygon e.g. `((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))`."
scalar Polygon @scalar(class: "Your\\Classname\\Polygon")

然后創建必須解析/驗證字符串的類

use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\ScalarType;

class Polygon extends ScalarType
{

    /**
     * Serializes an internal value to include in a response.
     *
     * @param mixed $value
     *
     * @return mixed
     *
     * @throws Error
     */
    public function serialize($value)
    {
        if ($value instanceof Geometry\Polygon) {
            $value->toString();
        }

        return (new Geometry\Polygon($value))->toString();
    }

    /**
     * Parses an externally provided value (query variable) to use as an input
     *
     * In the case of an invalid value this method must throw an Exception
     *
     * @param mixed $value
     *
     * @return mixed
     *
     * @throws Error
     */
    public function parseValue($value)
    {
        return new Geometry\Polygon($value);
    }

    /**
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input
     *
     * In the case of an invalid node or value this method must throw an Exception
     *
     * @param Node         $valueNode
     * @param mixed[]|null $variables
     *
     * @return mixed
     *
     * @throws Exception
     */
    public function parseLiteral($valueNode, ?array $variables = null)
    {
        if (! $valueNode instanceof StringValueNode) {
            throw new Error(
                "Query error: Can only parse strings, got {$valueNode->kind}",
                [$valueNode]
            );
        }

        return new Geometry\Polygon($valueNode->value);
    }
}

我尚未實現Geometry\\Polygon類的邏輯,並且對於這種類型的輸入和Scalar類型的所有驗證可能還需要進行一些調整。 但這基本上就是您在Ligthouse中創建Polygon Scalar類型的方式。

這樣,您就可以在areazone字段中輸入具有上面指定格式的字符串,並且在您的代碼中將其作為Geometry\\Polygon類獲得。

您可以在Lighthouse docs中閱讀有關標量的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM