graphile-postgis 1.1.1 → 2.2.0

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 (76) hide show
  1. package/README.md +22 -45
  2. package/constants.d.ts +1 -0
  3. package/constants.js +10 -1
  4. package/esm/constants.d.ts +13 -0
  5. package/esm/constants.js +9 -0
  6. package/esm/index.d.ts +24 -0
  7. package/esm/index.js +25 -33
  8. package/esm/plugins/codec.d.ts +19 -0
  9. package/esm/plugins/codec.js +174 -0
  10. package/esm/plugins/detect-extension.d.ts +14 -0
  11. package/esm/plugins/detect-extension.js +57 -0
  12. package/esm/plugins/geometry-fields.d.ts +21 -0
  13. package/esm/plugins/geometry-fields.js +245 -0
  14. package/esm/plugins/inflection.d.ts +8 -0
  15. package/esm/plugins/inflection.js +52 -0
  16. package/esm/plugins/register-types.d.ts +22 -0
  17. package/esm/plugins/register-types.js +319 -0
  18. package/esm/preset.d.ts +18 -0
  19. package/esm/preset.js +30 -0
  20. package/esm/types.d.ts +84 -0
  21. package/esm/utils.d.ts +21 -0
  22. package/esm/utils.js +18 -7
  23. package/index.d.ts +24 -15
  24. package/index.js +39 -47
  25. package/package.json +23 -18
  26. package/plugins/codec.d.ts +19 -0
  27. package/plugins/codec.js +180 -0
  28. package/plugins/detect-extension.d.ts +14 -0
  29. package/plugins/detect-extension.js +60 -0
  30. package/plugins/geometry-fields.d.ts +21 -0
  31. package/plugins/geometry-fields.js +248 -0
  32. package/plugins/inflection.d.ts +8 -0
  33. package/plugins/inflection.js +55 -0
  34. package/plugins/register-types.d.ts +22 -0
  35. package/plugins/register-types.js +325 -0
  36. package/preset.d.ts +18 -0
  37. package/preset.js +33 -0
  38. package/types.d.ts +69 -44
  39. package/utils.d.ts +16 -0
  40. package/utils.js +17 -6
  41. package/PostgisExtensionDetectionPlugin.d.ts +0 -3
  42. package/PostgisExtensionDetectionPlugin.js +0 -28
  43. package/PostgisInflectionPlugin.d.ts +0 -3
  44. package/PostgisInflectionPlugin.js +0 -36
  45. package/PostgisRegisterTypesPlugin.d.ts +0 -3
  46. package/PostgisRegisterTypesPlugin.js +0 -234
  47. package/PostgisVersionPlugin.d.ts +0 -3
  48. package/PostgisVersionPlugin.js +0 -24
  49. package/Postgis_GeometryCollection_GeometriesPlugin.d.ts +0 -3
  50. package/Postgis_GeometryCollection_GeometriesPlugin.js +0 -43
  51. package/Postgis_LineString_PointsPlugin.d.ts +0 -3
  52. package/Postgis_LineString_PointsPlugin.js +0 -40
  53. package/Postgis_MultiLineString_LineStringsPlugin.d.ts +0 -3
  54. package/Postgis_MultiLineString_LineStringsPlugin.js +0 -38
  55. package/Postgis_MultiPoint_PointsPlugin.d.ts +0 -3
  56. package/Postgis_MultiPoint_PointsPlugin.js +0 -38
  57. package/Postgis_MultiPolygon_PolygonsPlugin.d.ts +0 -3
  58. package/Postgis_MultiPolygon_PolygonsPlugin.js +0 -38
  59. package/Postgis_Point_LatitudeLongitudePlugin.d.ts +0 -3
  60. package/Postgis_Point_LatitudeLongitudePlugin.js +0 -43
  61. package/Postgis_Polygon_RingsPlugin.d.ts +0 -3
  62. package/Postgis_Polygon_RingsPlugin.js +0 -49
  63. package/esm/PostgisExtensionDetectionPlugin.js +0 -26
  64. package/esm/PostgisInflectionPlugin.js +0 -34
  65. package/esm/PostgisRegisterTypesPlugin.js +0 -229
  66. package/esm/PostgisVersionPlugin.js +0 -22
  67. package/esm/Postgis_GeometryCollection_GeometriesPlugin.js +0 -41
  68. package/esm/Postgis_LineString_PointsPlugin.js +0 -38
  69. package/esm/Postgis_MultiLineString_LineStringsPlugin.js +0 -36
  70. package/esm/Postgis_MultiPoint_PointsPlugin.js +0 -36
  71. package/esm/Postgis_MultiPolygon_PolygonsPlugin.js +0 -36
  72. package/esm/Postgis_Point_LatitudeLongitudePlugin.js +0 -41
  73. package/esm/Postgis_Polygon_RingsPlugin.js +0 -47
  74. package/esm/makeGeoJSONType.js +0 -39
  75. package/makeGeoJSONType.d.ts +0 -1
  76. package/makeGeoJSONType.js +0 -42
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisRegisterTypesPlugin: Plugin;
3
- export default PostgisRegisterTypesPlugin;
@@ -1,234 +0,0 @@
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(
130
- // `Getting ${type.name} type ${type.id}|${typeModifier}|${subtype}|${hasZ}|${hasM}|${srid}`
131
- // );
132
- if (!constructedTypes[typeId]) {
133
- constructedTypes[typeId] = {};
134
- }
135
- const typeModifierKey = typeModifier ?? -1;
136
- if (!pgTweaksByTypeIdAndModifer[typeId]) {
137
- pgTweaksByTypeIdAndModifer[typeId] = {};
138
- }
139
- if (!pgTweaksByTypeIdAndModifer[typeId][typeModifierKey]) {
140
- pgTweaksByTypeIdAndModifer[typeId][typeModifierKey] = (fragment) => {
141
- const params = [
142
- sql.literal('__gisType'),
143
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'postgis_type_name' // MUST be lowercase!
144
- )}(
145
- ${sql.identifier(POSTGIS?.namespaceName || 'public', 'geometrytype' // MUST be lowercase!
146
- )}(${fragment}),
147
- ${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_coorddim' // MUST be lowercase!
148
- )}(${fragment}::text)
149
- )`,
150
- sql.literal('__srid'),
151
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_srid' // MUST be lowercase!
152
- )}(${fragment})`,
153
- sql.literal('__geojson'),
154
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_asgeojson' // MUST be lowercase!
155
- )}(${fragment})::JSON`
156
- ];
157
- return sql.fragment `(case when ${fragment} is null then null else json_build_object(
158
- ${sql.join(params, ', ')}
159
- ) end)`;
160
- };
161
- }
162
- const gisTypeKey = typeModifier != null ? (0, utils_1.getGISTypeName)(subtype, hasZ, hasM) : -1;
163
- if (!constructedTypes[typeId][gisTypeKey]) {
164
- if (typeModifierKey === -1) {
165
- constructedTypes[typeId][gisTypeKey] = getGisInterface(type);
166
- }
167
- else if (subtype === constants_1.GisSubtype.Geometry) {
168
- constructedTypes[typeId][gisTypeKey] = getGisDimensionInterface(type, hasZ, hasM);
169
- }
170
- else {
171
- const intType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'int4' && introspectionType.namespaceName === 'pg_catalog');
172
- const jsonType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'json' && introspectionType.namespaceName === 'pg_catalog');
173
- if (!intType || !jsonType) {
174
- throw new Error('Unable to locate built-in int4/json types');
175
- }
176
- constructedTypes[typeId][gisTypeKey] = newWithHooks(GraphQLObjectType, {
177
- name: inflection.gisType(type, subtype, hasZ, hasM, srid),
178
- interfaces: () => [
179
- getGisInterface(type),
180
- getGisDimensionInterface(type, hasZ, hasM)
181
- ],
182
- fields: {
183
- [geojsonFieldName]: {
184
- type: GeoJSON,
185
- resolve: (data) => {
186
- return pg2gql(data.__geojson, jsonType);
187
- }
188
- },
189
- srid: {
190
- type: new GraphQLNonNull(GraphQLInt),
191
- resolve: (data) => {
192
- return pg2gql(data.__srid, intType);
193
- }
194
- }
195
- }
196
- }, {
197
- isPgGISType: true,
198
- pgGISType: type,
199
- pgGISTypeDetails: typeDetails
200
- });
201
- }
202
- }
203
- return constructedTypes[typeId][gisTypeKey];
204
- };
205
- // console.warn(`Registering handler for ${GEOGRAPHY_TYPE.id}`);
206
- pgRegisterGqlInputTypeByTypeId(GEOGRAPHY_TYPE.id, () => GeoJSON);
207
- pg2GqlMapper[GEOGRAPHY_TYPE.id] = {
208
- map: identity,
209
- unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)::${sql.identifier(POSTGIS?.namespaceName || 'public', 'geography')}`
210
- };
211
- pgRegisterGqlTypeByTypeId(GEOGRAPHY_TYPE.id, (_set, typeModifier) => {
212
- return getGisType(GEOGRAPHY_TYPE, typeModifier);
213
- });
214
- // console.warn(`Registering handler for ${GEOMETRY_TYPE.id}`);
215
- pgRegisterGqlInputTypeByTypeId(GEOMETRY_TYPE.id, () => GeoJSON);
216
- pg2GqlMapper[GEOMETRY_TYPE.id] = {
217
- map: identity,
218
- unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)`
219
- };
220
- pgRegisterGqlTypeByTypeId(GEOMETRY_TYPE.id, (_set, typeModifier) => {
221
- return getGisType(GEOMETRY_TYPE, typeModifier);
222
- });
223
- return input;
224
- }, ['PostgisTypes'], ['PgTables'], ['PgTypes']);
225
- builder.hook('GraphQLSchema', (schemaConfig, build) => {
226
- const postgisBuild = build;
227
- const existingTypes = schemaConfig.types ?? [];
228
- return {
229
- ...schemaConfig,
230
- types: [...existingTypes, ...postgisBuild.pgGISIncludedTypes]
231
- };
232
- });
233
- };
234
- exports.default = PostgisRegisterTypesPlugin;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const plugin: Plugin;
3
- export default plugin;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const find_and_require_package_json_1 = require("find-and-require-package-json");
4
- const pkg = (0, find_and_require_package_json_1.findAndRequirePackageJson)(__dirname);
5
- const plugin = (builder) => {
6
- builder.hook('build', (build) => {
7
- // Check dependencies
8
- if (!build.versions) {
9
- throw new Error(`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`);
10
- }
11
- const depends = (name, range) => {
12
- if (!build.hasVersion(name, range)) {
13
- throw new Error(`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${build.versions[name]
14
- ? `current version: ${build.versions[name]}`
15
- : 'not found'})`);
16
- }
17
- };
18
- depends('graphile-build-pg', '^4.4.0');
19
- // Register this plugin
20
- build.versions = build.extend(build.versions, { [pkg.name]: pkg.version });
21
- return build;
22
- });
23
- };
24
- exports.default = plugin;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisGeometryCollectionGeometriesPlugin: Plugin;
3
- export default PostgisGeometryCollectionGeometriesPlugin;
@@ -1,43 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisLineStringPointsPlugin: Plugin;
3
- export default PostgisLineStringPointsPlugin;
@@ -1,40 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisMultiLineStringLineStringsPlugin: Plugin;
3
- export default PostgisMultiLineStringLineStringsPlugin;
@@ -1,38 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisMultiPointPointsPlugin: Plugin;
3
- export default PostgisMultiPointPointsPlugin;
@@ -1,38 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisMultiPolygonPolygonsPlugin: Plugin;
3
- export default PostgisMultiPolygonPolygonsPlugin;
@@ -1,38 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisPointLatitudeLongitudePlugin: Plugin;
3
- export default PostgisPointLatitudeLongitudePlugin;
@@ -1,43 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'graphile-build';
2
- declare const PostgisPolygonRingsPlugin: Plugin;
3
- export default PostgisPolygonRingsPlugin;
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const constants_1 = require("./constants");
4
- const utils_1 = require("./utils");
5
- const PostgisPolygonRingsPlugin = (builder) => {
6
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
7
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
8
- if (!isPgGISType || !pgGISType || !pgGISTypeDetails || pgGISTypeDetails.subtype !== constants_1.GisSubtype.Polygon) {
9
- return fields;
10
- }
11
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
12
- const { hasZ, hasM, srid } = pgGISTypeDetails;
13
- const LineStringType = getPostgisTypeByGeometryType(pgGISType, constants_1.GisSubtype.LineString, hasZ, hasM, srid);
14
- if (!LineStringType) {
15
- return fields;
16
- }
17
- return extend(fields, {
18
- exterior: {
19
- type: LineStringType,
20
- resolve(data) {
21
- const polygon = data.__geojson;
22
- return {
23
- __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.LineString, hasZ, hasM),
24
- __srid: data.__srid,
25
- __geojson: {
26
- type: 'LineString',
27
- coordinates: polygon.coordinates[0]
28
- }
29
- };
30
- }
31
- },
32
- interiors: {
33
- type: new GraphQLList(LineStringType),
34
- resolve(data) {
35
- const polygon = data.__geojson;
36
- return polygon.coordinates.slice(1).map((coord) => ({
37
- __gisType: (0, utils_1.getGISTypeName)(constants_1.GisSubtype.LineString, hasZ, hasM),
38
- __srid: data.__srid,
39
- __geojson: {
40
- type: 'LineString',
41
- coordinates: coord
42
- }
43
- }));
44
- }
45
- }
46
- });
47
- });
48
- };
49
- exports.default = PostgisPolygonRingsPlugin;
@@ -1,26 +0,0 @@
1
- const PostgisExtensionDetectionPlugin = (builder) => {
2
- builder.hook('build', (build) => {
3
- const postgisBuild = build;
4
- const { pgIntrospectionResultsByKind: introspectionResultsByKind } = postgisBuild;
5
- const pgGISExtension = introspectionResultsByKind.extension.find((extension) => extension.name === 'postgis');
6
- // Check we have the postgis extension
7
- if (!pgGISExtension) {
8
- console.warn('PostGIS extension not found in database; skipping');
9
- return postgisBuild;
10
- }
11
- // Extract the geography and geometry types
12
- const pgGISGeometryType = introspectionResultsByKind.type.find((type) => type.name === 'geometry' && type.namespaceId === pgGISExtension.namespaceId);
13
- const pgGISGeographyType = introspectionResultsByKind.type.find((type) => type.name === 'geography' && type.namespaceId === pgGISExtension.namespaceId);
14
- if (!pgGISGeographyType || !pgGISGeometryType) {
15
- throw new Error("PostGIS is installed, but we couldn't find the geometry/geography types!");
16
- }
17
- return postgisBuild.extend(postgisBuild, {
18
- pgGISGraphQLTypesByTypeAndSubtype: {},
19
- pgGISGraphQLInterfaceTypesByType: {},
20
- pgGISGeometryType,
21
- pgGISGeographyType,
22
- pgGISExtension
23
- });
24
- });
25
- };
26
- export default PostgisExtensionDetectionPlugin;
@@ -1,34 +0,0 @@
1
- import { GisSubtype, SUBTYPE_STRING_BY_SUBTYPE } from './constants';
2
- const PostgisInflectionPlugin = (builder) => {
3
- builder.hook('inflection', (inflection) => {
4
- return {
5
- ...inflection,
6
- gisType(type, subtype, hasZ, hasM, _srid) {
7
- return this.upperCamelCase([type.name, SUBTYPE_STRING_BY_SUBTYPE[subtype], hasZ ? 'z' : null, hasM ? 'm' : null]
8
- .filter(Boolean)
9
- .join('-'));
10
- },
11
- gisInterfaceName(type) {
12
- return this.upperCamelCase(`${type.name}-interface`);
13
- },
14
- gisDimensionInterfaceName(type, hasZ, hasM) {
15
- return this.upperCamelCase([type.name, SUBTYPE_STRING_BY_SUBTYPE[GisSubtype.Geometry], hasZ ? 'z' : null, hasM ? 'm' : null]
16
- .filter(Boolean)
17
- .join('-'));
18
- },
19
- geojsonFieldName() {
20
- return 'geojson';
21
- },
22
- gisXFieldName(_type) {
23
- return _type.name === 'geography' ? 'longitude' : 'x';
24
- },
25
- gisYFieldName(_type) {
26
- return _type.name === 'geography' ? 'latitude' : 'y';
27
- },
28
- gisZFieldName(_type) {
29
- return _type.name === 'geography' ? 'height' : 'z';
30
- }
31
- };
32
- });
33
- };
34
- export default PostgisInflectionPlugin;