graphile-postgis 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE +3 -1
  2. package/PostgisExtensionDetectionPlugin.d.ts +3 -0
  3. package/PostgisExtensionDetectionPlugin.js +28 -0
  4. package/PostgisInflectionPlugin.d.ts +3 -0
  5. package/PostgisInflectionPlugin.js +36 -0
  6. package/PostgisRegisterTypesPlugin.d.ts +3 -0
  7. package/PostgisRegisterTypesPlugin.js +232 -0
  8. package/PostgisVersionPlugin.d.ts +3 -0
  9. package/PostgisVersionPlugin.js +23 -0
  10. package/Postgis_GeometryCollection_GeometriesPlugin.d.ts +3 -0
  11. package/Postgis_GeometryCollection_GeometriesPlugin.js +43 -0
  12. package/Postgis_LineString_PointsPlugin.d.ts +3 -0
  13. package/Postgis_LineString_PointsPlugin.js +40 -0
  14. package/Postgis_MultiLineString_LineStringsPlugin.d.ts +3 -0
  15. package/Postgis_MultiLineString_LineStringsPlugin.js +38 -0
  16. package/Postgis_MultiPoint_PointsPlugin.d.ts +3 -0
  17. package/Postgis_MultiPoint_PointsPlugin.js +38 -0
  18. package/Postgis_MultiPolygon_PolygonsPlugin.d.ts +3 -0
  19. package/Postgis_MultiPolygon_PolygonsPlugin.js +38 -0
  20. package/Postgis_Point_LatitudeLongitudePlugin.d.ts +3 -0
  21. package/Postgis_Point_LatitudeLongitudePlugin.js +43 -0
  22. package/Postgis_Polygon_RingsPlugin.d.ts +3 -0
  23. package/Postgis_Polygon_RingsPlugin.js +49 -0
  24. package/README.md +129 -5
  25. package/constants.d.ts +12 -0
  26. package/constants.js +34 -0
  27. package/esm/PostgisExtensionDetectionPlugin.js +26 -0
  28. package/esm/PostgisInflectionPlugin.js +34 -0
  29. package/esm/PostgisRegisterTypesPlugin.js +227 -0
  30. package/esm/PostgisVersionPlugin.js +21 -0
  31. package/esm/Postgis_GeometryCollection_GeometriesPlugin.js +41 -0
  32. package/esm/Postgis_LineString_PointsPlugin.js +38 -0
  33. package/esm/Postgis_MultiLineString_LineStringsPlugin.js +36 -0
  34. package/esm/Postgis_MultiPoint_PointsPlugin.js +36 -0
  35. package/esm/Postgis_MultiPolygon_PolygonsPlugin.js +36 -0
  36. package/esm/Postgis_Point_LatitudeLongitudePlugin.js +41 -0
  37. package/esm/Postgis_Polygon_RingsPlugin.js +47 -0
  38. package/esm/constants.js +31 -0
  39. package/esm/index.js +33 -0
  40. package/esm/makeGeoJSONType.js +39 -0
  41. package/esm/types.js +1 -0
  42. package/esm/utils.js +47 -0
  43. package/index.d.ts +15 -0
  44. package/index.js +49 -0
  45. package/makeGeoJSONType.d.ts +1 -0
  46. package/makeGeoJSONType.js +42 -0
  47. package/package.json +37 -52
  48. package/types.d.ts +59 -0
  49. package/types.js +2 -0
  50. package/utils.d.ts +5 -0
  51. package/utils.js +53 -0
  52. package/main/PostgisExtensionDetectionPlugin.js +0 -43
  53. package/main/PostgisInflectionPlugin.js +0 -51
  54. package/main/PostgisRegisterTypesPlugin.js +0 -347
  55. package/main/Postgis_GeometryCollection_GeometriesPlugin.js +0 -55
  56. package/main/Postgis_LineString_PointsPlugin.js +0 -51
  57. package/main/Postgis_MultiLineString_LineStringsPlugin.js +0 -51
  58. package/main/Postgis_MultiPoint_PointsPlugin.js +0 -51
  59. package/main/Postgis_MultiPolygon_PolygonsPlugin.js +0 -51
  60. package/main/Postgis_Point_LatitudeLongitudePlugin.js +0 -59
  61. package/main/Postgis_Polygon_RingsPlugin.js +0 -64
  62. package/main/constants.js +0 -39
  63. package/main/index.js +0 -93
  64. package/main/makeGeoJSONType.js +0 -89
  65. package/main/utils.js +0 -56
  66. package/module/PostgisExtensionDetectionPlugin.js +0 -31
  67. package/module/PostgisInflectionPlugin.js +0 -44
  68. package/module/PostgisRegisterTypesPlugin.js +0 -267
  69. package/module/Postgis_GeometryCollection_GeometriesPlugin.js +0 -57
  70. package/module/Postgis_LineString_PointsPlugin.js +0 -53
  71. package/module/Postgis_MultiLineString_LineStringsPlugin.js +0 -51
  72. package/module/Postgis_MultiPoint_PointsPlugin.js +0 -51
  73. package/module/Postgis_MultiPolygon_PolygonsPlugin.js +0 -51
  74. package/module/Postgis_Point_LatitudeLongitudePlugin.js +0 -64
  75. package/module/Postgis_Polygon_RingsPlugin.js +0 -66
  76. package/module/constants.js +0 -30
  77. package/module/index.js +0 -32
  78. package/module/makeGeoJSONType.js +0 -79
  79. package/module/utils.js +0 -40
package/LICENSE CHANGED
@@ -1,6 +1,8 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020 Dan Lynch <pyramation@gmail.com>
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Hyperweb <developers@hyperweb.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisExtensionDetectionPlugin: Plugin;
3
+ export default PostgisExtensionDetectionPlugin;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const PostgisExtensionDetectionPlugin = (builder) => {
4
+ builder.hook('build', (build) => {
5
+ const postgisBuild = build;
6
+ const { pgIntrospectionResultsByKind: introspectionResultsByKind } = postgisBuild;
7
+ const pgGISExtension = introspectionResultsByKind.extension.find((extension) => extension.name === 'postgis');
8
+ // Check we have the postgis extension
9
+ if (!pgGISExtension) {
10
+ console.warn('PostGIS extension not found in database; skipping');
11
+ return postgisBuild;
12
+ }
13
+ // Extract the geography and geometry types
14
+ const pgGISGeometryType = introspectionResultsByKind.type.find((type) => type.name === 'geometry' && type.namespaceId === pgGISExtension.namespaceId);
15
+ const pgGISGeographyType = introspectionResultsByKind.type.find((type) => type.name === 'geography' && type.namespaceId === pgGISExtension.namespaceId);
16
+ if (!pgGISGeographyType || !pgGISGeometryType) {
17
+ throw new Error("PostGIS is installed, but we couldn't find the geometry/geography types!");
18
+ }
19
+ return postgisBuild.extend(postgisBuild, {
20
+ pgGISGraphQLTypesByTypeAndSubtype: {},
21
+ pgGISGraphQLInterfaceTypesByType: {},
22
+ pgGISGeometryType,
23
+ pgGISGeographyType,
24
+ pgGISExtension
25
+ });
26
+ });
27
+ };
28
+ exports.default = PostgisExtensionDetectionPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisInflectionPlugin: Plugin;
3
+ export default PostgisInflectionPlugin;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const PostgisInflectionPlugin = (builder) => {
5
+ builder.hook('inflection', (inflection) => {
6
+ return {
7
+ ...inflection,
8
+ gisType(type, subtype, hasZ, hasM, _srid) {
9
+ return this.upperCamelCase([type.name, constants_1.SUBTYPE_STRING_BY_SUBTYPE[subtype], hasZ ? 'z' : null, hasM ? 'm' : null]
10
+ .filter(Boolean)
11
+ .join('-'));
12
+ },
13
+ gisInterfaceName(type) {
14
+ return this.upperCamelCase(`${type.name}-interface`);
15
+ },
16
+ gisDimensionInterfaceName(type, hasZ, hasM) {
17
+ return this.upperCamelCase([type.name, constants_1.SUBTYPE_STRING_BY_SUBTYPE[constants_1.GisSubtype.Geometry], hasZ ? 'z' : null, hasM ? 'm' : null]
18
+ .filter(Boolean)
19
+ .join('-'));
20
+ },
21
+ geojsonFieldName() {
22
+ return 'geojson';
23
+ },
24
+ gisXFieldName(_type) {
25
+ return _type.name === 'geography' ? 'longitude' : 'x';
26
+ },
27
+ gisYFieldName(_type) {
28
+ return _type.name === 'geography' ? 'latitude' : 'y';
29
+ },
30
+ gisZFieldName(_type) {
31
+ return _type.name === 'geography' ? 'height' : 'z';
32
+ }
33
+ };
34
+ });
35
+ };
36
+ exports.default = PostgisInflectionPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisRegisterTypesPlugin: Plugin;
3
+ export default PostgisRegisterTypesPlugin;
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const constants_1 = require("./constants");
7
+ const makeGeoJSONType_1 = __importDefault(require("./makeGeoJSONType"));
8
+ const utils_1 = require("./utils");
9
+ const SUBTYPES = [
10
+ constants_1.GisSubtype.Point,
11
+ constants_1.GisSubtype.LineString,
12
+ constants_1.GisSubtype.Polygon,
13
+ constants_1.GisSubtype.MultiPoint,
14
+ constants_1.GisSubtype.MultiLineString,
15
+ constants_1.GisSubtype.MultiPolygon,
16
+ constants_1.GisSubtype.GeometryCollection
17
+ ];
18
+ const identity = (input) => input;
19
+ const PostgisRegisterTypesPlugin = (builder) => {
20
+ builder.hook('build', (build) => {
21
+ const rawBuild = build;
22
+ const GeoJSON = (0, makeGeoJSONType_1.default)(rawBuild.graphql, rawBuild.inflection.builtin('GeoJSON'));
23
+ rawBuild.addType(GeoJSON);
24
+ return rawBuild.extend(rawBuild, {
25
+ getPostgisTypeByGeometryType(pgGISType, subtype, hasZ = false, hasM = false, srid = 0) {
26
+ const typeModifier = (0, utils_1.getGISTypeModifier)(subtype, hasZ, hasM, srid);
27
+ return this.pgGetGqlTypeByTypeIdAndModifier(pgGISType.id, typeModifier);
28
+ },
29
+ pgGISIncludedTypes: [],
30
+ pgGISIncludeType(Type) {
31
+ this.pgGISIncludedTypes.push(Type);
32
+ }
33
+ });
34
+ });
35
+ builder.hook('init', (input, build) => {
36
+ const rawBuild = build;
37
+ const { newWithHooks, pgIntrospectionResultsByKind: introspectionResultsByKind, graphql: { GraphQLInt, GraphQLNonNull, GraphQLInterfaceType, GraphQLObjectType }, pgRegisterGqlTypeByTypeId, pgRegisterGqlInputTypeByTypeId, pgTweaksByTypeIdAndModifer, getTypeByName, pgSql: sql, pg2gql, pg2GqlMapper, inflection, pgGISGraphQLTypesByTypeAndSubtype: constructedTypes, pgGISGraphQLInterfaceTypesByType: interfacesMap, pgGISGeometryType: GEOMETRY_TYPE, pgGISGeographyType: GEOGRAPHY_TYPE, pgGISExtension: POSTGIS, pgGISIncludeType: includeType } = rawBuild;
38
+ if (!GEOMETRY_TYPE || !GEOGRAPHY_TYPE) {
39
+ return input;
40
+ }
41
+ console.warn('PostGIS plugin enabled');
42
+ const GeoJSON = getTypeByName(inflection.builtin('GeoJSON'));
43
+ if (!GeoJSON) {
44
+ throw new Error('GeoJSON type was not registered on the build');
45
+ }
46
+ const geojsonFieldName = inflection.geojsonFieldName();
47
+ const ensureInterfaceStore = (type) => {
48
+ if (!interfacesMap[type.id]) {
49
+ interfacesMap[type.id] = {};
50
+ }
51
+ };
52
+ const getGisInterface = (type) => {
53
+ const zmflag = -1; // no dimensional constraint; could be xy/xyz/xym/xyzm
54
+ ensureInterfaceStore(type);
55
+ if (!interfacesMap[type.id][zmflag]) {
56
+ interfacesMap[type.id][zmflag] = newWithHooks(GraphQLInterfaceType, {
57
+ name: inflection.gisInterfaceName(type),
58
+ fields: {
59
+ [geojsonFieldName]: {
60
+ type: GeoJSON,
61
+ description: 'Converts the object to GeoJSON'
62
+ },
63
+ srid: {
64
+ type: new GraphQLNonNull(GraphQLInt),
65
+ description: 'Spatial reference identifier (SRID)'
66
+ }
67
+ },
68
+ resolveType(value) {
69
+ const Type = constructedTypes[type.id]?.[value.__gisType];
70
+ return Type instanceof GraphQLObjectType ? Type : undefined;
71
+ },
72
+ description: `All ${type.name} types implement this interface`
73
+ }, {
74
+ isPgGISInterface: true,
75
+ pgGISType: type,
76
+ pgGISZMFlag: zmflag
77
+ });
78
+ for (const subtype of SUBTYPES) {
79
+ for (const hasZ of [false, true]) {
80
+ for (const hasM of [false, true]) {
81
+ const typeModifier = (0, utils_1.getGISTypeModifier)(subtype, hasZ, hasM, 0);
82
+ const Type = getGisType(type, typeModifier);
83
+ includeType(Type);
84
+ }
85
+ }
86
+ }
87
+ }
88
+ return interfacesMap[type.id][zmflag];
89
+ };
90
+ const getGisDimensionInterface = (type, hasZ, hasM) => {
91
+ const zmflag = (hasZ ? 2 : 0) + (hasM ? 1 : 0); // Equivalent to ST_Zmflag: https://postgis.net/docs/ST_Zmflag.html
92
+ const coords = { 0: 'XY', 1: 'XYM', 2: 'XYZ', 3: 'XYZM' };
93
+ ensureInterfaceStore(type);
94
+ if (!interfacesMap[type.id][zmflag]) {
95
+ interfacesMap[type.id][zmflag] = newWithHooks(GraphQLInterfaceType, {
96
+ name: inflection.gisDimensionInterfaceName(type, hasZ, hasM),
97
+ fields: {
98
+ [geojsonFieldName]: {
99
+ type: GeoJSON,
100
+ description: 'Converts the object to GeoJSON'
101
+ },
102
+ srid: {
103
+ type: new GraphQLNonNull(GraphQLInt),
104
+ description: 'Spatial reference identifier (SRID)'
105
+ }
106
+ },
107
+ resolveType(value) {
108
+ const Type = constructedTypes[type.id]?.[value.__gisType];
109
+ return Type instanceof GraphQLObjectType ? Type : undefined;
110
+ },
111
+ description: `All ${type.name} ${coords[zmflag]} types implement this interface`
112
+ }, {
113
+ isPgGISDimensionInterface: true,
114
+ pgGISType: type,
115
+ pgGISZMFlag: zmflag
116
+ });
117
+ for (const subtype of SUBTYPES) {
118
+ const typeModifier = (0, utils_1.getGISTypeModifier)(subtype, hasZ, hasM, 0);
119
+ const Type = getGisType(type, typeModifier);
120
+ includeType(Type);
121
+ }
122
+ }
123
+ return interfacesMap[type.id][zmflag];
124
+ };
125
+ const getGisType = (type, typeModifier) => {
126
+ const typeId = type.id;
127
+ const typeDetails = (0, utils_1.getGISTypeDetails)(typeModifier);
128
+ const { subtype, hasZ, hasM, srid } = typeDetails;
129
+ console.warn(`Getting ${type.name} type ${type.id}|${typeModifier}|${subtype}|${hasZ}|${hasM}|${srid}`);
130
+ if (!constructedTypes[typeId]) {
131
+ constructedTypes[typeId] = {};
132
+ }
133
+ const typeModifierKey = typeModifier ?? -1;
134
+ if (!pgTweaksByTypeIdAndModifer[typeId]) {
135
+ pgTweaksByTypeIdAndModifer[typeId] = {};
136
+ }
137
+ if (!pgTweaksByTypeIdAndModifer[typeId][typeModifierKey]) {
138
+ pgTweaksByTypeIdAndModifer[typeId][typeModifierKey] = (fragment) => {
139
+ const params = [
140
+ sql.literal('__gisType'),
141
+ sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'postgis_type_name' // MUST be lowercase!
142
+ )}(
143
+ ${sql.identifier(POSTGIS?.namespaceName || 'public', 'geometrytype' // MUST be lowercase!
144
+ )}(${fragment}),
145
+ ${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_coorddim' // MUST be lowercase!
146
+ )}(${fragment}::text)
147
+ )`,
148
+ sql.literal('__srid'),
149
+ sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_srid' // MUST be lowercase!
150
+ )}(${fragment})`,
151
+ sql.literal('__geojson'),
152
+ sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_asgeojson' // MUST be lowercase!
153
+ )}(${fragment})::JSON`
154
+ ];
155
+ return sql.fragment `(case when ${fragment} is null then null else json_build_object(
156
+ ${sql.join(params, ', ')}
157
+ ) end)`;
158
+ };
159
+ }
160
+ const gisTypeKey = typeModifier != null ? (0, utils_1.getGISTypeName)(subtype, hasZ, hasM) : -1;
161
+ if (!constructedTypes[typeId][gisTypeKey]) {
162
+ if (typeModifierKey === -1) {
163
+ constructedTypes[typeId][gisTypeKey] = getGisInterface(type);
164
+ }
165
+ else if (subtype === constants_1.GisSubtype.Geometry) {
166
+ constructedTypes[typeId][gisTypeKey] = getGisDimensionInterface(type, hasZ, hasM);
167
+ }
168
+ else {
169
+ const intType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'int4' && introspectionType.namespaceName === 'pg_catalog');
170
+ const jsonType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'json' && introspectionType.namespaceName === 'pg_catalog');
171
+ if (!intType || !jsonType) {
172
+ throw new Error('Unable to locate built-in int4/json types');
173
+ }
174
+ constructedTypes[typeId][gisTypeKey] = newWithHooks(GraphQLObjectType, {
175
+ name: inflection.gisType(type, subtype, hasZ, hasM, srid),
176
+ interfaces: () => [
177
+ getGisInterface(type),
178
+ getGisDimensionInterface(type, hasZ, hasM)
179
+ ],
180
+ fields: {
181
+ [geojsonFieldName]: {
182
+ type: GeoJSON,
183
+ resolve: (data) => {
184
+ return pg2gql(data.__geojson, jsonType);
185
+ }
186
+ },
187
+ srid: {
188
+ type: new GraphQLNonNull(GraphQLInt),
189
+ resolve: (data) => {
190
+ return pg2gql(data.__srid, intType);
191
+ }
192
+ }
193
+ }
194
+ }, {
195
+ isPgGISType: true,
196
+ pgGISType: type,
197
+ pgGISTypeDetails: typeDetails
198
+ });
199
+ }
200
+ }
201
+ return constructedTypes[typeId][gisTypeKey];
202
+ };
203
+ console.warn(`Registering handler for ${GEOGRAPHY_TYPE.id}`);
204
+ pgRegisterGqlInputTypeByTypeId(GEOGRAPHY_TYPE.id, () => GeoJSON);
205
+ pg2GqlMapper[GEOGRAPHY_TYPE.id] = {
206
+ map: identity,
207
+ unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)::${sql.identifier(POSTGIS?.namespaceName || 'public', 'geography')}`
208
+ };
209
+ pgRegisterGqlTypeByTypeId(GEOGRAPHY_TYPE.id, (_set, typeModifier) => {
210
+ return getGisType(GEOGRAPHY_TYPE, typeModifier);
211
+ });
212
+ console.warn(`Registering handler for ${GEOMETRY_TYPE.id}`);
213
+ pgRegisterGqlInputTypeByTypeId(GEOMETRY_TYPE.id, () => GeoJSON);
214
+ pg2GqlMapper[GEOMETRY_TYPE.id] = {
215
+ map: identity,
216
+ unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)`
217
+ };
218
+ pgRegisterGqlTypeByTypeId(GEOMETRY_TYPE.id, (_set, typeModifier) => {
219
+ return getGisType(GEOMETRY_TYPE, typeModifier);
220
+ });
221
+ return input;
222
+ }, ['PostgisTypes'], ['PgTables'], ['PgTypes']);
223
+ builder.hook('GraphQLSchema', (schemaConfig, build) => {
224
+ const postgisBuild = build;
225
+ const existingTypes = schemaConfig.types ?? [];
226
+ return {
227
+ ...schemaConfig,
228
+ types: [...existingTypes, ...postgisBuild.pgGISIncludedTypes]
229
+ };
230
+ });
231
+ };
232
+ exports.default = PostgisRegisterTypesPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const plugin: Plugin;
3
+ export default plugin;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const plugin = (builder) => {
4
+ builder.hook('build', (build) => {
5
+ const pkg = require('../package.json');
6
+ // Check dependencies
7
+ if (!build.versions) {
8
+ throw new Error(`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`);
9
+ }
10
+ const depends = (name, range) => {
11
+ if (!build.hasVersion(name, range)) {
12
+ throw new Error(`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${build.versions[name]
13
+ ? `current version: ${build.versions[name]}`
14
+ : 'not found'})`);
15
+ }
16
+ };
17
+ depends('graphile-build-pg', '^4.4.0');
18
+ // Register this plugin
19
+ build.versions = build.extend(build.versions, { [pkg.name]: pkg.version });
20
+ return build;
21
+ });
22
+ };
23
+ exports.default = plugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisGeometryCollectionGeometriesPlugin: Plugin;
3
+ export default PostgisGeometryCollectionGeometriesPlugin;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const PostgisGeometryCollectionGeometriesPlugin = (builder) => {
6
+ builder.hook('GraphQLObjectType:fields', function AddGeometriesToGeometryCollection(fields, build, context) {
7
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
+ if (!isPgGISType ||
9
+ !pgGISType ||
10
+ !pgGISTypeDetails ||
11
+ pgGISTypeDetails.subtype !== constants_1.GisSubtype.GeometryCollection) {
12
+ return fields;
13
+ }
14
+ const { extend, pgGISGraphQLInterfaceTypesByType, graphql: { GraphQLList } } = build;
15
+ const { hasZ, hasM } = pgGISTypeDetails;
16
+ const zmflag = (hasZ ? 2 : 0) + (hasM ? 1 : 0); // Equivalent to ST_Zmflag: https://postgis.net/docs/ST_Zmflag.html
17
+ const Interface = pgGISGraphQLInterfaceTypesByType[pgGISType.id][zmflag];
18
+ if (!Interface) {
19
+ console.warn("Unexpectedly couldn't find the interface");
20
+ return fields;
21
+ }
22
+ return extend(fields, {
23
+ geometries: {
24
+ type: new GraphQLList(Interface),
25
+ resolve(data) {
26
+ const geometryCollection = data.__geojson;
27
+ return geometryCollection.geometries.map((geom) => {
28
+ const subtype = constants_1.GisSubtype[geom.type];
29
+ if (subtype === undefined) {
30
+ throw new Error(`Unsupported geometry subtype ${geom.type}`);
31
+ }
32
+ return {
33
+ __gisType: (0, utils_1.getGISTypeName)(subtype, hasZ, hasM),
34
+ __srid: data.__srid,
35
+ __geojson: geom
36
+ };
37
+ });
38
+ }
39
+ }
40
+ });
41
+ });
42
+ };
43
+ exports.default = PostgisGeometryCollectionGeometriesPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisLineStringPointsPlugin: Plugin;
3
+ export default PostgisLineStringPointsPlugin;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const PostgisLineStringPointsPlugin = (builder) => {
6
+ builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
7
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
+ if (!isPgGISType ||
9
+ !pgGISType ||
10
+ !pgGISTypeDetails ||
11
+ pgGISTypeDetails.subtype !== constants_1.GisSubtype.LineString) {
12
+ return fields;
13
+ }
14
+ const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
15
+ const { hasZ, hasM, srid } = pgGISTypeDetails;
16
+ const Point = getPostgisTypeByGeometryType(pgGISType, constants_1.GisSubtype.Point, hasZ, hasM, srid);
17
+ if (!Point) {
18
+ return fields;
19
+ }
20
+ return extend(fields, {
21
+ points: {
22
+ type: new GraphQLList(Point),
23
+ resolve(data) {
24
+ const lineString = data.__geojson;
25
+ return lineString.coordinates.map((coord) => {
26
+ return {
27
+ __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.Point, hasZ, hasM),
28
+ __srid: data.__srid,
29
+ __geojson: {
30
+ type: 'Point',
31
+ coordinates: coord
32
+ }
33
+ };
34
+ });
35
+ }
36
+ }
37
+ });
38
+ });
39
+ };
40
+ exports.default = PostgisLineStringPointsPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisMultiLineStringLineStringsPlugin: Plugin;
3
+ export default PostgisMultiLineStringLineStringsPlugin;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const PostgisMultiLineStringLineStringsPlugin = (builder) => {
6
+ builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
7
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
+ if (!isPgGISType ||
9
+ !pgGISType ||
10
+ !pgGISTypeDetails ||
11
+ pgGISTypeDetails.subtype !== constants_1.GisSubtype.MultiLineString) {
12
+ return fields;
13
+ }
14
+ const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
15
+ const { hasZ, hasM, srid } = pgGISTypeDetails;
16
+ const LineString = getPostgisTypeByGeometryType(pgGISType, constants_1.GisSubtype.LineString, hasZ, hasM, srid);
17
+ if (!LineString) {
18
+ return fields;
19
+ }
20
+ return extend(fields, {
21
+ lines: {
22
+ type: new GraphQLList(LineString),
23
+ resolve(data) {
24
+ const multiLineString = data.__geojson;
25
+ return multiLineString.coordinates.map((coord) => ({
26
+ __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.LineString, hasZ, hasM),
27
+ __srid: data.__srid,
28
+ __geojson: {
29
+ type: 'LineString',
30
+ coordinates: coord
31
+ }
32
+ }));
33
+ }
34
+ }
35
+ });
36
+ });
37
+ };
38
+ exports.default = PostgisMultiLineStringLineStringsPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisMultiPointPointsPlugin: Plugin;
3
+ export default PostgisMultiPointPointsPlugin;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const PostgisMultiPointPointsPlugin = (builder) => {
6
+ builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
7
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
+ if (!isPgGISType ||
9
+ !pgGISType ||
10
+ !pgGISTypeDetails ||
11
+ pgGISTypeDetails.subtype !== constants_1.GisSubtype.MultiPoint) {
12
+ return fields;
13
+ }
14
+ const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
15
+ const { hasZ, hasM, srid } = pgGISTypeDetails;
16
+ const Point = getPostgisTypeByGeometryType(pgGISType, constants_1.GisSubtype.Point, hasZ, hasM, srid);
17
+ if (!Point) {
18
+ return fields;
19
+ }
20
+ return extend(fields, {
21
+ points: {
22
+ type: new GraphQLList(Point),
23
+ resolve(data) {
24
+ const multiPoint = data.__geojson;
25
+ return multiPoint.coordinates.map((coord) => ({
26
+ __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.Point, hasZ, hasM),
27
+ __srid: data.__srid,
28
+ __geojson: {
29
+ type: 'Point',
30
+ coordinates: coord
31
+ }
32
+ }));
33
+ }
34
+ }
35
+ });
36
+ });
37
+ };
38
+ exports.default = PostgisMultiPointPointsPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisMultiPolygonPolygonsPlugin: Plugin;
3
+ export default PostgisMultiPolygonPolygonsPlugin;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const PostgisMultiPolygonPolygonsPlugin = (builder) => {
6
+ builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
7
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
+ if (!isPgGISType ||
9
+ !pgGISType ||
10
+ !pgGISTypeDetails ||
11
+ pgGISTypeDetails.subtype !== constants_1.GisSubtype.MultiPolygon) {
12
+ return fields;
13
+ }
14
+ const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
15
+ const { hasZ, hasM, srid } = pgGISTypeDetails;
16
+ const PolygonType = getPostgisTypeByGeometryType(pgGISType, constants_1.GisSubtype.Polygon, hasZ, hasM, srid);
17
+ if (!PolygonType) {
18
+ return fields;
19
+ }
20
+ return extend(fields, {
21
+ polygons: {
22
+ type: new GraphQLList(PolygonType),
23
+ resolve(data) {
24
+ const multiPolygon = data.__geojson;
25
+ return multiPolygon.coordinates.map((coord) => ({
26
+ __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.Polygon, hasZ, hasM),
27
+ __srid: data.__srid,
28
+ __geojson: {
29
+ type: 'Polygon',
30
+ coordinates: coord
31
+ }
32
+ }));
33
+ }
34
+ }
35
+ });
36
+ });
37
+ };
38
+ exports.default = PostgisMultiPolygonPolygonsPlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisPointLatitudeLongitudePlugin: Plugin;
3
+ export default PostgisPointLatitudeLongitudePlugin;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const PostgisPointLatitudeLongitudePlugin = (builder) => {
5
+ builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
6
+ const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
7
+ if (!isPgGISType || !pgGISType || !pgGISTypeDetails || pgGISTypeDetails.subtype !== constants_1.GisSubtype.Point) {
8
+ return fields;
9
+ }
10
+ const { extend, graphql: { GraphQLNonNull, GraphQLFloat }, inflection } = build;
11
+ const xFieldName = inflection.gisXFieldName(pgGISType);
12
+ const yFieldName = inflection.gisYFieldName(pgGISType);
13
+ const zFieldName = inflection.gisZFieldName(pgGISType);
14
+ return extend(fields, {
15
+ [xFieldName]: {
16
+ type: new GraphQLNonNull(GraphQLFloat),
17
+ resolve(data) {
18
+ const point = data.__geojson;
19
+ return point.coordinates[0];
20
+ }
21
+ },
22
+ [yFieldName]: {
23
+ type: new GraphQLNonNull(GraphQLFloat),
24
+ resolve(data) {
25
+ const point = data.__geojson;
26
+ return point.coordinates[1];
27
+ }
28
+ },
29
+ ...(pgGISTypeDetails.hasZ
30
+ ? {
31
+ [zFieldName]: {
32
+ type: new GraphQLNonNull(GraphQLFloat),
33
+ resolve(data) {
34
+ const point = data.__geojson;
35
+ return point.coordinates[2];
36
+ }
37
+ }
38
+ }
39
+ : {})
40
+ });
41
+ });
42
+ };
43
+ exports.default = PostgisPointLatitudeLongitudePlugin;
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'graphile-build';
2
+ declare const PostgisPolygonRingsPlugin: Plugin;
3
+ export default PostgisPolygonRingsPlugin;