You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{GraphQLScalarType}from"graphql"importtype{SchemaBuilder,Build}from"graphile-build"importtype{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. */exportdefaultfunctionTranslateRangesPlugin(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 int8rangeconstmyRange=pgIntrospectionResultsByKind.type.find((t: PgType)=>t.namespaceName==="my_schema"&&t.name==="my_range")if(!myRange){returnbuild// something went horribly wrong, abort}// declare a new kind of scalarconstMyRangeType=newGraphQLScalarType({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 oidpgRegisterGqlInputTypeByTypeId(myRange.id,()=>MyRangeType)pgRegisterGqlTypeByTypeId(myRange.id,()=>MyRangeType)// define mappings to and frompg2GqlMapper[myRange.id]={// SQL -> GQLmap: (output: string)=>output,// GQL -> SQL; substitute your target range type for `int8range` as necessaryunmap: (input: string)=>sql.fragment`${sql.value(input)}::int8range`,}returnbuild// pass the build object back for the next stage})}
The text was updated successfully, but these errors were encountered:
Sometimes the old way is more convenient :)
The text was updated successfully, but these errors were encountered: