Recipe: translate range types to/from Postgres' format instead of the object version

未關閉
#341 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
38/100
Issue 類型
文件
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
graphql, postgresql, typescript
領域
documentation

研究方向

未指定目標文件。首先在 graphile.github.io repository 中找到現有的 recipe 文件,然後使用所提供的 TranslateRangesPlugin 範例及其 builder hook 和 pg2GqlMapper 入口點作為參考資料。完成的標準是:加入該 recipe,並清楚說明 range translation 行為與範例程式碼。

由索引模型根據 Issue 內容生成。

描述

Sometimes the old way is more convenient :)

import { GraphQLScalarType } from "graphql"
import type { SchemaBuilder, Build } from "graphile-build"
import type { PgType } from "graphile-build-pg"

/**
 * This plugin switches the expression of range types back to Postgres' native
 * `[lower,upper)` formats instead of Postgraphile's more verbose nested-object
 * `{start: {value, inclusive}, end: {...}` version.
 */
export default function TranslateRangesPlugin(builder: SchemaBuilder) {
    builder.hook("build", (build: Build) => {
        const {
            pgIntrospectionResultsByKind,
            pgRegisterGqlInputTypeByTypeId,
            pgRegisterGqlTypeByTypeId,
            pg2GqlMapper,
            pgSql: sql,
        } = build

        // find our target type oid. If you want to change the behavior for a
        // built-in range type, you can use one of the following default oids:
        //
        // 3904 int4range
        // 3906 numrange
        // 3908 tsrange
        // 3910 tstzrange
        // 3912 daterange
        // 3926 int8range
        const myRange = pgIntrospectionResultsByKind.type.find(
            (t: PgType) => t.namespaceName === "my_schema" && t.name === "my_range"
        )

        if (!myRange) {
            return build // something went horribly wrong, abort
        }

        // declare a new kind of scalar
        const MyRangeType = new GraphQLScalarType({
            name: "MyRange",
            description: "An int8range dedicated to a more specific purpose",
            serialize: (a: string) => a,
            parseValue: (a: string) => a,
        })

        // register input and output types for the oid
        pgRegisterGqlInputTypeByTypeId(myRange.id, () => MyRangeType)
        pgRegisterGqlTypeByTypeId(myRange.id, () => MyRangeType)

        // define mappings to and from
        pg2GqlMapper[myRange.id] = {
            // SQL -> GQL
            map: (output: string) => output,
            // GQL -> SQL; substitute your target range type for `int8range` as necessary
            unmap: (input: string) => sql.fragment`${sql.value(input)}::int8range`,
        }

        return build // pass the build object back for the next stage
    })
}
主要語言
SCSS
星號
27
分支
124
PR 合併指標
30 天內沒有已合併 PR

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

graphile/graphile.github.io 的其他 Issue

查看 graphile/graphile.github.io 的全部 Issue

相似的 Issue

更多 Documentation Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。