graphile / graphile/graphile.github.io

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

オープン
#341 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
SCSS
スター
27
フォーク
126
PR マージ指標
30日以内にマージされた PR はありません

説明

Sometimes the old way is more convenient :)

```typescript
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
})
}
```

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

対象となるドキュメントファイルは指定されていません。まず、graphile.github.io リポジトリにある既存のレシピドキュメントを見つけ、次に、提供されている TranslateRangesPlugin の例と、その builder hook および pg2GqlMapper エントリポイントを資料として使用してください。範囲の変換動作を含むレシピが追加され、サンプルコードが明確に説明されていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
graphql, postgresql, typescript
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。