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,229 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import makeGeoJSONType from './makeGeoJSONType';
3
- import { getGISTypeDetails, getGISTypeModifier, getGISTypeName } from './utils';
4
- const SUBTYPES = [
5
- GisSubtype.Point,
6
- GisSubtype.LineString,
7
- GisSubtype.Polygon,
8
- GisSubtype.MultiPoint,
9
- GisSubtype.MultiLineString,
10
- GisSubtype.MultiPolygon,
11
- GisSubtype.GeometryCollection
12
- ];
13
- const identity = (input) => input;
14
- const PostgisRegisterTypesPlugin = (builder) => {
15
- builder.hook('build', (build) => {
16
- const rawBuild = build;
17
- const GeoJSON = makeGeoJSONType(rawBuild.graphql, rawBuild.inflection.builtin('GeoJSON'));
18
- rawBuild.addType(GeoJSON);
19
- return rawBuild.extend(rawBuild, {
20
- getPostgisTypeByGeometryType(pgGISType, subtype, hasZ = false, hasM = false, srid = 0) {
21
- const typeModifier = getGISTypeModifier(subtype, hasZ, hasM, srid);
22
- return this.pgGetGqlTypeByTypeIdAndModifier(pgGISType.id, typeModifier);
23
- },
24
- pgGISIncludedTypes: [],
25
- pgGISIncludeType(Type) {
26
- this.pgGISIncludedTypes.push(Type);
27
- }
28
- });
29
- });
30
- builder.hook('init', (input, build) => {
31
- const rawBuild = build;
32
- 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;
33
- if (!GEOMETRY_TYPE || !GEOGRAPHY_TYPE) {
34
- return input;
35
- }
36
- // console.warn('PostGIS plugin enabled');
37
- const GeoJSON = getTypeByName(inflection.builtin('GeoJSON'));
38
- if (!GeoJSON) {
39
- throw new Error('GeoJSON type was not registered on the build');
40
- }
41
- const geojsonFieldName = inflection.geojsonFieldName();
42
- const ensureInterfaceStore = (type) => {
43
- if (!interfacesMap[type.id]) {
44
- interfacesMap[type.id] = {};
45
- }
46
- };
47
- const getGisInterface = (type) => {
48
- const zmflag = -1; // no dimensional constraint; could be xy/xyz/xym/xyzm
49
- ensureInterfaceStore(type);
50
- if (!interfacesMap[type.id][zmflag]) {
51
- interfacesMap[type.id][zmflag] = newWithHooks(GraphQLInterfaceType, {
52
- name: inflection.gisInterfaceName(type),
53
- fields: {
54
- [geojsonFieldName]: {
55
- type: GeoJSON,
56
- description: 'Converts the object to GeoJSON'
57
- },
58
- srid: {
59
- type: new GraphQLNonNull(GraphQLInt),
60
- description: 'Spatial reference identifier (SRID)'
61
- }
62
- },
63
- resolveType(value) {
64
- const Type = constructedTypes[type.id]?.[value.__gisType];
65
- return Type instanceof GraphQLObjectType ? Type : undefined;
66
- },
67
- description: `All ${type.name} types implement this interface`
68
- }, {
69
- isPgGISInterface: true,
70
- pgGISType: type,
71
- pgGISZMFlag: zmflag
72
- });
73
- for (const subtype of SUBTYPES) {
74
- for (const hasZ of [false, true]) {
75
- for (const hasM of [false, true]) {
76
- const typeModifier = getGISTypeModifier(subtype, hasZ, hasM, 0);
77
- const Type = getGisType(type, typeModifier);
78
- includeType(Type);
79
- }
80
- }
81
- }
82
- }
83
- return interfacesMap[type.id][zmflag];
84
- };
85
- const getGisDimensionInterface = (type, hasZ, hasM) => {
86
- const zmflag = (hasZ ? 2 : 0) + (hasM ? 1 : 0); // Equivalent to ST_Zmflag: https://postgis.net/docs/ST_Zmflag.html
87
- const coords = { 0: 'XY', 1: 'XYM', 2: 'XYZ', 3: 'XYZM' };
88
- ensureInterfaceStore(type);
89
- if (!interfacesMap[type.id][zmflag]) {
90
- interfacesMap[type.id][zmflag] = newWithHooks(GraphQLInterfaceType, {
91
- name: inflection.gisDimensionInterfaceName(type, hasZ, hasM),
92
- fields: {
93
- [geojsonFieldName]: {
94
- type: GeoJSON,
95
- description: 'Converts the object to GeoJSON'
96
- },
97
- srid: {
98
- type: new GraphQLNonNull(GraphQLInt),
99
- description: 'Spatial reference identifier (SRID)'
100
- }
101
- },
102
- resolveType(value) {
103
- const Type = constructedTypes[type.id]?.[value.__gisType];
104
- return Type instanceof GraphQLObjectType ? Type : undefined;
105
- },
106
- description: `All ${type.name} ${coords[zmflag]} types implement this interface`
107
- }, {
108
- isPgGISDimensionInterface: true,
109
- pgGISType: type,
110
- pgGISZMFlag: zmflag
111
- });
112
- for (const subtype of SUBTYPES) {
113
- const typeModifier = getGISTypeModifier(subtype, hasZ, hasM, 0);
114
- const Type = getGisType(type, typeModifier);
115
- includeType(Type);
116
- }
117
- }
118
- return interfacesMap[type.id][zmflag];
119
- };
120
- const getGisType = (type, typeModifier) => {
121
- const typeId = type.id;
122
- const typeDetails = getGISTypeDetails(typeModifier);
123
- const { subtype, hasZ, hasM, srid } = typeDetails;
124
- // console.warn(
125
- // `Getting ${type.name} type ${type.id}|${typeModifier}|${subtype}|${hasZ}|${hasM}|${srid}`
126
- // );
127
- if (!constructedTypes[typeId]) {
128
- constructedTypes[typeId] = {};
129
- }
130
- const typeModifierKey = typeModifier ?? -1;
131
- if (!pgTweaksByTypeIdAndModifer[typeId]) {
132
- pgTweaksByTypeIdAndModifer[typeId] = {};
133
- }
134
- if (!pgTweaksByTypeIdAndModifer[typeId][typeModifierKey]) {
135
- pgTweaksByTypeIdAndModifer[typeId][typeModifierKey] = (fragment) => {
136
- const params = [
137
- sql.literal('__gisType'),
138
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'postgis_type_name' // MUST be lowercase!
139
- )}(
140
- ${sql.identifier(POSTGIS?.namespaceName || 'public', 'geometrytype' // MUST be lowercase!
141
- )}(${fragment}),
142
- ${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_coorddim' // MUST be lowercase!
143
- )}(${fragment}::text)
144
- )`,
145
- sql.literal('__srid'),
146
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_srid' // MUST be lowercase!
147
- )}(${fragment})`,
148
- sql.literal('__geojson'),
149
- sql.fragment `${sql.identifier(POSTGIS?.namespaceName || 'public', 'st_asgeojson' // MUST be lowercase!
150
- )}(${fragment})::JSON`
151
- ];
152
- return sql.fragment `(case when ${fragment} is null then null else json_build_object(
153
- ${sql.join(params, ', ')}
154
- ) end)`;
155
- };
156
- }
157
- const gisTypeKey = typeModifier != null ? getGISTypeName(subtype, hasZ, hasM) : -1;
158
- if (!constructedTypes[typeId][gisTypeKey]) {
159
- if (typeModifierKey === -1) {
160
- constructedTypes[typeId][gisTypeKey] = getGisInterface(type);
161
- }
162
- else if (subtype === GisSubtype.Geometry) {
163
- constructedTypes[typeId][gisTypeKey] = getGisDimensionInterface(type, hasZ, hasM);
164
- }
165
- else {
166
- const intType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'int4' && introspectionType.namespaceName === 'pg_catalog');
167
- const jsonType = introspectionResultsByKind.type.find((introspectionType) => introspectionType.name === 'json' && introspectionType.namespaceName === 'pg_catalog');
168
- if (!intType || !jsonType) {
169
- throw new Error('Unable to locate built-in int4/json types');
170
- }
171
- constructedTypes[typeId][gisTypeKey] = newWithHooks(GraphQLObjectType, {
172
- name: inflection.gisType(type, subtype, hasZ, hasM, srid),
173
- interfaces: () => [
174
- getGisInterface(type),
175
- getGisDimensionInterface(type, hasZ, hasM)
176
- ],
177
- fields: {
178
- [geojsonFieldName]: {
179
- type: GeoJSON,
180
- resolve: (data) => {
181
- return pg2gql(data.__geojson, jsonType);
182
- }
183
- },
184
- srid: {
185
- type: new GraphQLNonNull(GraphQLInt),
186
- resolve: (data) => {
187
- return pg2gql(data.__srid, intType);
188
- }
189
- }
190
- }
191
- }, {
192
- isPgGISType: true,
193
- pgGISType: type,
194
- pgGISTypeDetails: typeDetails
195
- });
196
- }
197
- }
198
- return constructedTypes[typeId][gisTypeKey];
199
- };
200
- // console.warn(`Registering handler for ${GEOGRAPHY_TYPE.id}`);
201
- pgRegisterGqlInputTypeByTypeId(GEOGRAPHY_TYPE.id, () => GeoJSON);
202
- pg2GqlMapper[GEOGRAPHY_TYPE.id] = {
203
- map: identity,
204
- unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)::${sql.identifier(POSTGIS?.namespaceName || 'public', 'geography')}`
205
- };
206
- pgRegisterGqlTypeByTypeId(GEOGRAPHY_TYPE.id, (_set, typeModifier) => {
207
- return getGisType(GEOGRAPHY_TYPE, typeModifier);
208
- });
209
- // console.warn(`Registering handler for ${GEOMETRY_TYPE.id}`);
210
- pgRegisterGqlInputTypeByTypeId(GEOMETRY_TYPE.id, () => GeoJSON);
211
- pg2GqlMapper[GEOMETRY_TYPE.id] = {
212
- map: identity,
213
- unmap: (o) => sql.fragment `st_geomfromgeojson(${sql.value(JSON.stringify(o))}::text)`
214
- };
215
- pgRegisterGqlTypeByTypeId(GEOMETRY_TYPE.id, (_set, typeModifier) => {
216
- return getGisType(GEOMETRY_TYPE, typeModifier);
217
- });
218
- return input;
219
- }, ['PostgisTypes'], ['PgTables'], ['PgTypes']);
220
- builder.hook('GraphQLSchema', (schemaConfig, build) => {
221
- const postgisBuild = build;
222
- const existingTypes = schemaConfig.types ?? [];
223
- return {
224
- ...schemaConfig,
225
- types: [...existingTypes, ...postgisBuild.pgGISIncludedTypes]
226
- };
227
- });
228
- };
229
- export default PostgisRegisterTypesPlugin;
@@ -1,22 +0,0 @@
1
- import { findAndRequirePackageJson } from 'find-and-require-package-json';
2
- const pkg = findAndRequirePackageJson(__dirname);
3
- const plugin = (builder) => {
4
- builder.hook('build', (build) => {
5
- // Check dependencies
6
- if (!build.versions) {
7
- throw new Error(`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`);
8
- }
9
- const depends = (name, range) => {
10
- if (!build.hasVersion(name, range)) {
11
- throw new Error(`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${build.versions[name]
12
- ? `current version: ${build.versions[name]}`
13
- : 'not found'})`);
14
- }
15
- };
16
- depends('graphile-build-pg', '^4.4.0');
17
- // Register this plugin
18
- build.versions = build.extend(build.versions, { [pkg.name]: pkg.version });
19
- return build;
20
- });
21
- };
22
- export default plugin;
@@ -1,41 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisGeometryCollectionGeometriesPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', function AddGeometriesToGeometryCollection(fields, build, context) {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType ||
7
- !pgGISType ||
8
- !pgGISTypeDetails ||
9
- pgGISTypeDetails.subtype !== GisSubtype.GeometryCollection) {
10
- return fields;
11
- }
12
- const { extend, pgGISGraphQLInterfaceTypesByType, graphql: { GraphQLList } } = build;
13
- const { hasZ, hasM } = pgGISTypeDetails;
14
- const zmflag = (hasZ ? 2 : 0) + (hasM ? 1 : 0); // Equivalent to ST_Zmflag: https://postgis.net/docs/ST_Zmflag.html
15
- const Interface = pgGISGraphQLInterfaceTypesByType[pgGISType.id][zmflag];
16
- if (!Interface) {
17
- console.warn("Unexpectedly couldn't find the interface");
18
- return fields;
19
- }
20
- return extend(fields, {
21
- geometries: {
22
- type: new GraphQLList(Interface),
23
- resolve(data) {
24
- const geometryCollection = data.__geojson;
25
- return geometryCollection.geometries.map((geom) => {
26
- const subtype = GisSubtype[geom.type];
27
- if (subtype === undefined) {
28
- throw new Error(`Unsupported geometry subtype ${geom.type}`);
29
- }
30
- return {
31
- __gisType: getGISTypeName(subtype, hasZ, hasM),
32
- __srid: data.__srid,
33
- __geojson: geom
34
- };
35
- });
36
- }
37
- }
38
- });
39
- });
40
- };
41
- export default PostgisGeometryCollectionGeometriesPlugin;
@@ -1,38 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisLineStringPointsPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType ||
7
- !pgGISType ||
8
- !pgGISTypeDetails ||
9
- pgGISTypeDetails.subtype !== GisSubtype.LineString) {
10
- return fields;
11
- }
12
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
13
- const { hasZ, hasM, srid } = pgGISTypeDetails;
14
- const Point = getPostgisTypeByGeometryType(pgGISType, GisSubtype.Point, hasZ, hasM, srid);
15
- if (!Point) {
16
- return fields;
17
- }
18
- return extend(fields, {
19
- points: {
20
- type: new GraphQLList(Point),
21
- resolve(data) {
22
- const lineString = data.__geojson;
23
- return lineString.coordinates.map((coord) => {
24
- return {
25
- __gisType: getGISTypeName(GisSubtype.Point, hasZ, hasM),
26
- __srid: data.__srid,
27
- __geojson: {
28
- type: 'Point',
29
- coordinates: coord
30
- }
31
- };
32
- });
33
- }
34
- }
35
- });
36
- });
37
- };
38
- export default PostgisLineStringPointsPlugin;
@@ -1,36 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisMultiLineStringLineStringsPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType ||
7
- !pgGISType ||
8
- !pgGISTypeDetails ||
9
- pgGISTypeDetails.subtype !== GisSubtype.MultiLineString) {
10
- return fields;
11
- }
12
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
13
- const { hasZ, hasM, srid } = pgGISTypeDetails;
14
- const LineString = getPostgisTypeByGeometryType(pgGISType, GisSubtype.LineString, hasZ, hasM, srid);
15
- if (!LineString) {
16
- return fields;
17
- }
18
- return extend(fields, {
19
- lines: {
20
- type: new GraphQLList(LineString),
21
- resolve(data) {
22
- const multiLineString = data.__geojson;
23
- return multiLineString.coordinates.map((coord) => ({
24
- __gisType: getGISTypeName(GisSubtype.LineString, hasZ, hasM),
25
- __srid: data.__srid,
26
- __geojson: {
27
- type: 'LineString',
28
- coordinates: coord
29
- }
30
- }));
31
- }
32
- }
33
- });
34
- });
35
- };
36
- export default PostgisMultiLineStringLineStringsPlugin;
@@ -1,36 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisMultiPointPointsPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType ||
7
- !pgGISType ||
8
- !pgGISTypeDetails ||
9
- pgGISTypeDetails.subtype !== GisSubtype.MultiPoint) {
10
- return fields;
11
- }
12
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
13
- const { hasZ, hasM, srid } = pgGISTypeDetails;
14
- const Point = getPostgisTypeByGeometryType(pgGISType, GisSubtype.Point, hasZ, hasM, srid);
15
- if (!Point) {
16
- return fields;
17
- }
18
- return extend(fields, {
19
- points: {
20
- type: new GraphQLList(Point),
21
- resolve(data) {
22
- const multiPoint = data.__geojson;
23
- return multiPoint.coordinates.map((coord) => ({
24
- __gisType: getGISTypeName(GisSubtype.Point, hasZ, hasM),
25
- __srid: data.__srid,
26
- __geojson: {
27
- type: 'Point',
28
- coordinates: coord
29
- }
30
- }));
31
- }
32
- }
33
- });
34
- });
35
- };
36
- export default PostgisMultiPointPointsPlugin;
@@ -1,36 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisMultiPolygonPolygonsPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType ||
7
- !pgGISType ||
8
- !pgGISTypeDetails ||
9
- pgGISTypeDetails.subtype !== GisSubtype.MultiPolygon) {
10
- return fields;
11
- }
12
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
13
- const { hasZ, hasM, srid } = pgGISTypeDetails;
14
- const PolygonType = getPostgisTypeByGeometryType(pgGISType, GisSubtype.Polygon, hasZ, hasM, srid);
15
- if (!PolygonType) {
16
- return fields;
17
- }
18
- return extend(fields, {
19
- polygons: {
20
- type: new GraphQLList(PolygonType),
21
- resolve(data) {
22
- const multiPolygon = data.__geojson;
23
- return multiPolygon.coordinates.map((coord) => ({
24
- __gisType: getGISTypeName(GisSubtype.Polygon, hasZ, hasM),
25
- __srid: data.__srid,
26
- __geojson: {
27
- type: 'Polygon',
28
- coordinates: coord
29
- }
30
- }));
31
- }
32
- }
33
- });
34
- });
35
- };
36
- export default PostgisMultiPolygonPolygonsPlugin;
@@ -1,41 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- const PostgisPointLatitudeLongitudePlugin = (builder) => {
3
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
4
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
5
- if (!isPgGISType || !pgGISType || !pgGISTypeDetails || pgGISTypeDetails.subtype !== GisSubtype.Point) {
6
- return fields;
7
- }
8
- const { extend, graphql: { GraphQLNonNull, GraphQLFloat }, inflection } = build;
9
- const xFieldName = inflection.gisXFieldName(pgGISType);
10
- const yFieldName = inflection.gisYFieldName(pgGISType);
11
- const zFieldName = inflection.gisZFieldName(pgGISType);
12
- return extend(fields, {
13
- [xFieldName]: {
14
- type: new GraphQLNonNull(GraphQLFloat),
15
- resolve(data) {
16
- const point = data.__geojson;
17
- return point.coordinates[0];
18
- }
19
- },
20
- [yFieldName]: {
21
- type: new GraphQLNonNull(GraphQLFloat),
22
- resolve(data) {
23
- const point = data.__geojson;
24
- return point.coordinates[1];
25
- }
26
- },
27
- ...(pgGISTypeDetails.hasZ
28
- ? {
29
- [zFieldName]: {
30
- type: new GraphQLNonNull(GraphQLFloat),
31
- resolve(data) {
32
- const point = data.__geojson;
33
- return point.coordinates[2];
34
- }
35
- }
36
- }
37
- : {})
38
- });
39
- });
40
- };
41
- export default PostgisPointLatitudeLongitudePlugin;
@@ -1,47 +0,0 @@
1
- import { GisSubtype } from './constants';
2
- import { getGISTypeName } from './utils';
3
- const PostgisPolygonRingsPlugin = (builder) => {
4
- builder.hook('GraphQLObjectType:fields', (fields, build, context) => {
5
- const { scope: { isPgGISType, pgGISType, pgGISTypeDetails } } = context;
6
- if (!isPgGISType || !pgGISType || !pgGISTypeDetails || pgGISTypeDetails.subtype !== GisSubtype.Polygon) {
7
- return fields;
8
- }
9
- const { extend, getPostgisTypeByGeometryType, graphql: { GraphQLList } } = build;
10
- const { hasZ, hasM, srid } = pgGISTypeDetails;
11
- const LineStringType = getPostgisTypeByGeometryType(pgGISType, GisSubtype.LineString, hasZ, hasM, srid);
12
- if (!LineStringType) {
13
- return fields;
14
- }
15
- return extend(fields, {
16
- exterior: {
17
- type: LineStringType,
18
- resolve(data) {
19
- const polygon = data.__geojson;
20
- return {
21
- __gisType: getGISTypeName(GisSubtype.LineString, hasZ, hasM),
22
- __srid: data.__srid,
23
- __geojson: {
24
- type: 'LineString',
25
- coordinates: polygon.coordinates[0]
26
- }
27
- };
28
- }
29
- },
30
- interiors: {
31
- type: new GraphQLList(LineStringType),
32
- resolve(data) {
33
- const polygon = data.__geojson;
34
- return polygon.coordinates.slice(1).map((coord) => ({
35
- __gisType: getGISTypeName(GisSubtype.LineString, hasZ, hasM),
36
- __srid: data.__srid,
37
- __geojson: {
38
- type: 'LineString',
39
- coordinates: coord
40
- }
41
- }));
42
- }
43
- }
44
- });
45
- });
46
- };
47
- export default PostgisPolygonRingsPlugin;
@@ -1,39 +0,0 @@
1
- export default function makeGeoJSONType(graphql, name = 'GeoJSON') {
2
- const { Kind, GraphQLScalarType } = graphql;
3
- const identity = (value) => value;
4
- const parseLiteral = (ast, variables) => {
5
- switch (ast.kind) {
6
- case Kind.STRING:
7
- case Kind.BOOLEAN:
8
- return ast.value;
9
- case Kind.INT:
10
- case Kind.FLOAT:
11
- return parseFloat(ast.value);
12
- case Kind.OBJECT: {
13
- const value = Object.create(null);
14
- ast.fields.forEach((field) => {
15
- value[field.name.value] = parseLiteral(field.value, variables);
16
- });
17
- return value;
18
- }
19
- case Kind.LIST:
20
- return ast.values.map((n) => parseLiteral(n, variables));
21
- case Kind.NULL:
22
- return null;
23
- case Kind.VARIABLE: {
24
- const variableName = ast.name.value;
25
- return variables ? variables[variableName] : undefined;
26
- }
27
- default:
28
- return undefined;
29
- }
30
- };
31
- return new GraphQLScalarType({
32
- name,
33
- description: `The \`${name}\` scalar type represents GeoJSON values as specified by` +
34
- '[RFC 7946](https://tools.ietf.org/html/rfc7946).',
35
- serialize: identity,
36
- parseValue: identity,
37
- parseLiteral
38
- });
39
- }
@@ -1 +0,0 @@
1
- export default function makeGeoJSONType(graphql: typeof import('graphql'), name?: string): import('graphql').GraphQLScalarType;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = makeGeoJSONType;
4
- function makeGeoJSONType(graphql, name = 'GeoJSON') {
5
- const { Kind, GraphQLScalarType } = graphql;
6
- const identity = (value) => value;
7
- const parseLiteral = (ast, variables) => {
8
- switch (ast.kind) {
9
- case Kind.STRING:
10
- case Kind.BOOLEAN:
11
- return ast.value;
12
- case Kind.INT:
13
- case Kind.FLOAT:
14
- return parseFloat(ast.value);
15
- case Kind.OBJECT: {
16
- const value = Object.create(null);
17
- ast.fields.forEach((field) => {
18
- value[field.name.value] = parseLiteral(field.value, variables);
19
- });
20
- return value;
21
- }
22
- case Kind.LIST:
23
- return ast.values.map((n) => parseLiteral(n, variables));
24
- case Kind.NULL:
25
- return null;
26
- case Kind.VARIABLE: {
27
- const variableName = ast.name.value;
28
- return variables ? variables[variableName] : undefined;
29
- }
30
- default:
31
- return undefined;
32
- }
33
- };
34
- return new GraphQLScalarType({
35
- name,
36
- description: `The \`${name}\` scalar type represents GeoJSON values as specified by` +
37
- '[RFC 7946](https://tools.ietf.org/html/rfc7946).',
38
- serialize: identity,
39
- parseValue: identity,
40
- parseLiteral
41
- });
42
- }