Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Postgraphile v5 Support (#58) #60

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import * as pg from "pg";
import PostgisPreset from "../src";
import { makeSchema } from "graphile-build";
import { makePgService } from "postgraphile/adaptors/pg";
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { makeV4Preset } from "postgraphile/presets/v4";

export async function withPgPool<T = any>(
cb: (pool: pg.Pool) => Promise<T>
Expand Down Expand Up @@ -39,3 +44,15 @@ export async function withTransaction<T = any>(
}
});
}

export async function makePostGraphileSchema(pool: pg.Pool, schemas: string[]) {
return await makeSchema({
extends: [PostGraphileAmberPreset, makeV4Preset({}), PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
});
}
22 changes: 14 additions & 8 deletions __tests__/integration/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import * as path from "path";
import * as pg from "pg";
import { promisify } from "util";
import { GraphQLSchema, graphql } from "graphql";
import { withPgClient } from "../helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../../src/index";
import { withPgClient, withPgPool } from "../helpers";
import PostgisPreset from "../../src/index";
import { makeSchema } from "graphile-build";
import { makePgService } from "postgraphile/adaptors/pg";

const readFile = promisify(fs.readFile);

const queriesDir = `${__dirname}/../fixtures/queries`;
const queryFileNames = fs.readdirSync(queriesDir);

const schemas = ["graphile_postgis"];
const options = {
appendPlugins: [PostgisPlugin],
};

let gqlSchema: GraphQLSchema;

beforeAll(async () => {
await withPgClient(async (client: pg.PoolClient) => {
gqlSchema = await createPostGraphileSchema(client, schemas, options);
await withPgPool(async (pool: pg.Pool) => {
gqlSchema = await makeSchema({
extends: [PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
}).then(it => it.schema);
});
});

Expand Down
13 changes: 4 additions & 9 deletions __tests__/schema.minimal_dimensional.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import * as pg from "pg";
import { withPgClient } from "./helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../src/index";
import { makePostGraphileSchema, withPgPool } from "./helpers.js";
import { lexicographicSortSchema } from "graphql";

const schemas = ["graphile_postgis_minimal_dimensional"];
const options = {
appendPlugins: [PostgisPlugin],
};

test("prints a schema with this plugin", () =>
withPgClient(async (client: pg.PoolClient) => {
const gqlSchema = await createPostGraphileSchema(client, schemas, options);
expect(lexicographicSortSchema(gqlSchema)).toMatchSnapshot();
withPgPool(async (pool: pg.Pool) => {
const { schema } = await makePostGraphileSchema(pool, schemas);
expect(lexicographicSortSchema(schema)).toMatchSnapshot();
}));
22 changes: 14 additions & 8 deletions __tests__/schema.minimal_type.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import * as pg from "pg";
import { withPgClient } from "./helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../src/index";
import { withPgPool } from "./helpers";
import PostgisPreset from "../src/index";
import { lexicographicSortSchema } from "graphql";
import { makePgService } from "postgraphile/adaptors/pg";
import { makeSchema } from "graphile-build";

const schemas = ["graphile_postgis_minimal_type"];
const options = {
appendPlugins: [PostgisPlugin],
};

test("prints a schema with this plugin", () =>
withPgClient(async (client: pg.PoolClient) => {
const gqlSchema = await createPostGraphileSchema(client, schemas, options);
withPgPool(async (pool: pg.Pool) => {
const gqlSchema = await makeSchema({
extends: [PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
}).then(it => it.schema);
expect(lexicographicSortSchema(gqlSchema)).toMatchSnapshot();
}));
22 changes: 14 additions & 8 deletions __tests__/schema.minimal_type_and_srid.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import * as pg from "pg";
import { withPgClient } from "./helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../src/index";
import { withPgPool } from "./helpers";
import PostgisPreset from "../src/index";
import { lexicographicSortSchema } from "graphql";
import { makeSchema } from "graphile-build";
import { makePgService } from "postgraphile/adaptors/pg";

const schemas = ["graphile_postgis_minimal_type_and_srid"];
const options = {
appendPlugins: [PostgisPlugin],
};

test("prints a schema with this plugin", () =>
withPgClient(async (client: pg.PoolClient) => {
const gqlSchema = await createPostGraphileSchema(client, schemas, options);
withPgPool(async (pool: pg.Pool) => {
const gqlSchema = await makeSchema({
extends: [PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
}).then(it => it.schema);
expect(lexicographicSortSchema(gqlSchema)).toMatchSnapshot();
}));
22 changes: 14 additions & 8 deletions __tests__/schema.minimal_unconstrained.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import * as pg from "pg";
import { withPgClient } from "./helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../src/index";
import { withPgPool } from "./helpers";
import PostgisPreset from "../src/index";
import { lexicographicSortSchema } from "graphql";
import { makeSchema } from "graphile-build";
import { makePgService } from "postgraphile/adaptors/pg";

const schemas = ["graphile_postgis_minimal_unconstrained"];
const options = {
appendPlugins: [PostgisPlugin],
};

test("prints a schema with this plugin", () =>
withPgClient(async (client: pg.PoolClient) => {
const gqlSchema = await createPostGraphileSchema(client, schemas, options);
withPgPool(async (pool: pg.Pool) => {
const gqlSchema = await makeSchema({
extends: [PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
}).then(it => it.schema);
expect(lexicographicSortSchema(gqlSchema)).toMatchSnapshot();
}));
22 changes: 14 additions & 8 deletions __tests__/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import * as pg from "pg";
import { withPgClient } from "./helpers";
import { createPostGraphileSchema } from "postgraphile-core";
import PostgisPlugin from "../src/index";
import { withPgPool } from "./helpers";
import PostgisPreset from "../src/index";
import { lexicographicSortSchema } from "graphql";
import { makeSchema } from "graphile-build";
import { makePgService } from "postgraphile/adaptors/pg";

const schemas = ["graphile_postgis"];
const options = {
appendPlugins: [PostgisPlugin],
};

test("prints a schema with this plugin", () =>
withPgClient(async (client: pg.PoolClient) => {
const gqlSchema = await createPostGraphileSchema(client, schemas, options);
withPgPool(async (pool: pg.Pool) => {
const gqlSchema = await makeSchema({
extends: [PostgisPreset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
}).then(it => it.schema);
expect(lexicographicSortSchema(gqlSchema)).toMatchSnapshot();
}));
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
testRegex: "(/__tests__/.*\\.(test|spec))\\.[tj]sx?$",
moduleFileExtensions: ["ts", "js", "json"],
snapshotSerializers: ["jest-serializer-graphql-schema"],
testEnvironment: 'jest-environment-node'
};
28 changes: 13 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "PostGIS support for PostGraphile",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"build": "tsc -p tsconfig.build.json",
"watch": "tsc -p tsconfig.build.json --watch",
"test": "scripts/test",
"lint": "prettier --list-different 'src/**/*' && tslint --config tslint.json --project tsconfig.json",
"lint": "prettier --list-different 'src/**/*'",
"postgraphile": "nodemon --watch dist -x 'postgraphile --append-plugins `pwd`/dist/index.js -c graphile_test -s graphile_postgis -p 5123 --enhance-graphiql --watch --dynamic-json --show-error-stack --extended-errors severity,code,detail,hint,position,internalPosition,internalQuery,where,schema,table,column,dataType,constraint,file,line,routine'",
"dev": "psql -f __tests__/schema.sql graphile_test && concurrently --kill-others 'npm run watch' 'npm run postgraphile'",
"prepack": "rm -Rf dist && npm run build"
Expand All @@ -32,33 +32,31 @@
},
"homepage": "https://github.com/graphile/postgis#readme",
"peerDependencies": {
"graphile-build": "^4.4.0",
"graphile-build-pg": "^4.4.0",
"graphile-build": "^5.0.0-beta.21",
"graphile-build-pg": "^5.0.0-beta.25",
"graphql": ">=0.6 <16",
"pg-sql2": ">=2.2.1 <5"
"pg-sql2": "^5.0.0-beta.6"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.22",
"concurrently": "^6.0.2",
"graphql": "^15",
"graphql": "16.1.0-experimental-stream-defer.6",
"jest": "^26.6.3",
"nodemon": "^1.19.1",
"postgraphile": "^4.9.0",
"postgraphile-core": "^4.9.0",
"prettier": "1.18.2",
"postgraphile": "^5.0.0-beta.26",
"prettier": "^3.3.2",
"ts-jest": "^26.5.4",
"tslint": "^5.19.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.5.3"
"typescript": "^5.4.5"
},
"dependencies": {
"debug": "^4.1.1",
"jest-serializer-graphql-schema": "^5.0.0-1.1",
"tslib": "^1.10.0"
"tslib": "^2.6.3"
},
"resolutions": {
"graphql": "^15"
"graphql": "16.1.0-experimental-stream-defer.6"
},
"files": [
"dist"
Expand Down
58 changes: 47 additions & 11 deletions src/PostgisRegisterTypesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import { Plugin } from "graphile-build";
import debug from "./debug";
import { PgType } from "graphile-build-pg";
import { GraphQLResolveInfo, GraphQLType, GraphQLNamedType } from "graphql";
import { Subtype } from "./interfaces";
import { getGISTypeDetails, getGISTypeModifier, getGISTypeName } from "./utils";
import { Subtype } from "./interfaces.js";
import {
getGISTypeDetails,
getGISTypeModifier,
getGISTypeName,
} from "./utils.js";
import { SQL } from "pg-sql2";
import makeGeoJSONType from "./makeGeoJSONType";
import makeGeoJSONType from "./makeGeoJSONType.js";
import { version } from "./version.js";

function identity<T>(input: T): T {
return input;
}

const plugin: Plugin = builder => {
builder.hook("build", build => {
const GeoJSON = makeGeoJSONType(
build.graphql,
build.inflection.builtin("GeoJSON")
);
build.addType(GeoJSON);
declare global {
namespace GraphileBuild {
interface ScopeScalar {
/**
* Set to true for the GeoJSON type.
*/
isGeoJSONType?: boolean;
}
}
}

export const PostgisRegisterTypesPlugin: GraphileConfig.Plugin = {
name: "PostgisRegisterTypesPlugin",
version,

schema: {
hooks: {
init(_, build, _context) {
const { graphql } = build;
const name = build.inflection.builtin("GeoJSON");
build.registerScalarType(
name,
{
isGeoJSONType: true,
},
() => {
return makeGeoJSONType(graphql, name);
},
"Adding GeoJSON type"
);
return _;
},
},
},
};

/*


return build.extend(build, {
getPostgisTypeByGeometryType(
Expand Down Expand Up @@ -339,3 +374,4 @@ const plugin: Plugin = builder => {
};

export default plugin;
*/
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Plugin } from "graphile-build";
import PostgisVersionPlugin from "./PostgisVersionPlugin";
import PostgisInflectionPlugin from "./PostgisInflectionPlugin";
import PostgisExtensionDetectionPlugin from "./PostgisExtensionDetectionPlugin";
Expand All @@ -11,7 +10,7 @@ import Postgis_MultiPoint_PointsPlugin from "./Postgis_MultiPoint_PointsPlugin";
import Postgis_MultiLineString_LineStringsPlugin from "./Postgis_MultiLineString_LineStringsPlugin";
import Postgis_MultiPolygon_PolygonsPlugin from "./Postgis_MultiPolygon_PolygonsPlugin";

const PostgisPlugin: Plugin = async (builder, options) => {
const PostgisPreset: GraphileConfig.Preset = async (builder, options) => {
await PostgisVersionPlugin(builder, options);
await PostgisInflectionPlugin(builder, options);
await PostgisExtensionDetectionPlugin(builder, options);
Expand All @@ -38,4 +37,4 @@ const PostgisPlugin: Plugin = async (builder, options) => {
// Enhancing the `GeometryCollection` type:
await Postgis_GeometryCollection_GeometriesPlugin(builder, options);
};
export default PostgisPlugin;
export default PostgisPreset;
Loading