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

Đang mở
#341 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

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

Hướng nghiên cứu

Không có tệp tài liệu đích nào được nêu tên. Hãy bắt đầu bằng cách xác định tài liệu recipe hiện có trong repository graphile.github.io, sau đó sử dụng ví dụ TranslateRangesPlugin được cung cấp cùng với builder hook và các entry point pg2GqlMapper của nó làm tư liệu nguồn. Công việc được xem là hoàn tất khi recipe được thêm vào với hành vi dịch range và mã ví dụ được giải thích rõ ràng.

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

Mô tả

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
    })
}
Ngôn ngữ chính
SCSS
Star
27
Fork
124
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của graphile/graphile.github.io

Tất cả issue của graphile/graphile.github.io

Issue tương tự

Thêm issue về Documentation

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.