graphile-schema 1.1.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.
- package/LICENSE +23 -0
- package/README.md +155 -0
- package/build-schema.d.ts +7 -0
- package/build-schema.js +32 -0
- package/esm/build-schema.d.ts +7 -0
- package/esm/build-schema.js +26 -0
- package/esm/fetch-endpoint-schema.d.ts +6 -0
- package/esm/fetch-endpoint-schema.js +67 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +2 -0
- package/fetch-endpoint-schema.d.ts +6 -0
- package/fetch-endpoint-schema.js +103 -0
- package/index.d.ts +4 -0
- package/index.js +7 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# graphile-schema
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/constructive/blob/main/LICENSE">
|
|
12
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/graphile-schema">
|
|
15
|
+
<img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=graphile%2Fgraphile-schema%2Fpackage.json"/>
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
Lightweight GraphQL SDL builder for PostgreSQL using PostGraphile v5. Build schemas directly from a database or fetch them from a running GraphQL endpoint — no server dependencies required.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install graphile-schema
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Build SDL from a PostgreSQL Database
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { buildSchemaSDL } from 'graphile-schema';
|
|
33
|
+
|
|
34
|
+
const sdl = await buildSchemaSDL({
|
|
35
|
+
database: 'mydb',
|
|
36
|
+
schemas: ['app_public'],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
console.log(sdl);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Fetch SDL from a GraphQL Endpoint
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { fetchEndpointSchemaSDL } from 'graphile-schema';
|
|
46
|
+
|
|
47
|
+
const sdl = await fetchEndpointSchemaSDL('https://api.example.com/graphql', {
|
|
48
|
+
auth: 'Bearer my-token',
|
|
49
|
+
headers: { 'X-Custom-Header': 'value' },
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
console.log(sdl);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### With Custom Graphile Presets
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { buildSchemaSDL } from 'graphile-schema';
|
|
59
|
+
|
|
60
|
+
const sdl = await buildSchemaSDL({
|
|
61
|
+
database: 'mydb',
|
|
62
|
+
schemas: ['app_public', 'app_private'],
|
|
63
|
+
graphile: {
|
|
64
|
+
extends: [MyCustomPreset],
|
|
65
|
+
schema: { pgSimplifyPatch: false },
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## API
|
|
71
|
+
|
|
72
|
+
### `buildSchemaSDL(opts)`
|
|
73
|
+
|
|
74
|
+
Builds a GraphQL SDL string directly from a PostgreSQL database using PostGraphile v5 introspection.
|
|
75
|
+
|
|
76
|
+
| Option | Type | Description |
|
|
77
|
+
|--------|------|-------------|
|
|
78
|
+
| `database` | `string` | Database name (default: `'constructive'`) |
|
|
79
|
+
| `schemas` | `string[]` | PostgreSQL schemas to introspect |
|
|
80
|
+
| `graphile` | `Partial<GraphileConfig.Preset>` | Optional Graphile preset overrides |
|
|
81
|
+
|
|
82
|
+
### `fetchEndpointSchemaSDL(endpoint, opts?)`
|
|
83
|
+
|
|
84
|
+
Fetches a GraphQL SDL string from a running GraphQL endpoint via introspection query.
|
|
85
|
+
|
|
86
|
+
| Option | Type | Description |
|
|
87
|
+
|--------|------|-------------|
|
|
88
|
+
| `endpoint` | `string` | GraphQL endpoint URL |
|
|
89
|
+
| `opts.headerHost` | `string` | Override the `Host` header |
|
|
90
|
+
| `opts.auth` | `string` | `Authorization` header value |
|
|
91
|
+
| `opts.headers` | `Record<string, string>` | Additional request headers |
|
|
92
|
+
|
|
93
|
+
## Disclaimer
|
|
94
|
+
|
|
95
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
96
|
+
|
|
97
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Education and Tutorials
|
|
102
|
+
|
|
103
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
104
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
105
|
+
|
|
106
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
107
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
108
|
+
|
|
109
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
110
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
111
|
+
|
|
112
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
113
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
114
|
+
|
|
115
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
116
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
117
|
+
|
|
118
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
119
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
120
|
+
|
|
121
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
122
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
123
|
+
|
|
124
|
+
## Related Constructive Tooling
|
|
125
|
+
|
|
126
|
+
### 📦 Package Management
|
|
127
|
+
|
|
128
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
129
|
+
|
|
130
|
+
### 🧪 Testing
|
|
131
|
+
|
|
132
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
133
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
134
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
135
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
136
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
137
|
+
|
|
138
|
+
### 🧠 Parsing & AST
|
|
139
|
+
|
|
140
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
141
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
142
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
143
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
144
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
145
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
146
|
+
|
|
147
|
+
## Credits
|
|
148
|
+
|
|
149
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
150
|
+
|
|
151
|
+
## Disclaimer
|
|
152
|
+
|
|
153
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
154
|
+
|
|
155
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
2
|
+
export type BuildSchemaOptions = {
|
|
3
|
+
database?: string;
|
|
4
|
+
schemas: string[];
|
|
5
|
+
graphile?: Partial<GraphileConfig.Preset>;
|
|
6
|
+
};
|
|
7
|
+
export declare function buildSchemaSDL(opts: BuildSchemaOptions): Promise<string>;
|
package/build-schema.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
exports.buildSchemaSDL = buildSchemaSDL;
|
|
7
|
+
const deepmerge_1 = __importDefault(require("deepmerge"));
|
|
8
|
+
const graphql_1 = require("graphql");
|
|
9
|
+
const graphile_settings_1 = require("graphile-settings");
|
|
10
|
+
const graphile_build_1 = require("graphile-build");
|
|
11
|
+
const pg_cache_1 = require("pg-cache");
|
|
12
|
+
const pg_env_1 = require("pg-env");
|
|
13
|
+
async function buildSchemaSDL(opts) {
|
|
14
|
+
const database = opts.database ?? 'constructive';
|
|
15
|
+
const schemas = Array.isArray(opts.schemas) ? opts.schemas : [];
|
|
16
|
+
const config = (0, pg_env_1.getPgEnvOptions)({ database });
|
|
17
|
+
const connectionString = (0, pg_cache_1.buildConnectionString)(config.user, config.password, config.host, config.port, config.database);
|
|
18
|
+
const basePreset = {
|
|
19
|
+
extends: [graphile_settings_1.ConstructivePreset],
|
|
20
|
+
pgServices: [
|
|
21
|
+
(0, graphile_settings_1.makePgService)({
|
|
22
|
+
connectionString,
|
|
23
|
+
schemas,
|
|
24
|
+
}),
|
|
25
|
+
],
|
|
26
|
+
};
|
|
27
|
+
const preset = opts.graphile
|
|
28
|
+
? (0, deepmerge_1.default)(basePreset, opts.graphile)
|
|
29
|
+
: basePreset;
|
|
30
|
+
const { schema } = await (0, graphile_build_1.makeSchema)(preset);
|
|
31
|
+
return (0, graphql_1.printSchema)(schema);
|
|
32
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
2
|
+
export type BuildSchemaOptions = {
|
|
3
|
+
database?: string;
|
|
4
|
+
schemas: string[];
|
|
5
|
+
graphile?: Partial<GraphileConfig.Preset>;
|
|
6
|
+
};
|
|
7
|
+
export declare function buildSchemaSDL(opts: BuildSchemaOptions): Promise<string>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import deepmerge from 'deepmerge';
|
|
2
|
+
import { printSchema } from 'graphql';
|
|
3
|
+
import { ConstructivePreset, makePgService } from 'graphile-settings';
|
|
4
|
+
import { makeSchema } from 'graphile-build';
|
|
5
|
+
import { buildConnectionString } from 'pg-cache';
|
|
6
|
+
import { getPgEnvOptions } from 'pg-env';
|
|
7
|
+
export async function buildSchemaSDL(opts) {
|
|
8
|
+
const database = opts.database ?? 'constructive';
|
|
9
|
+
const schemas = Array.isArray(opts.schemas) ? opts.schemas : [];
|
|
10
|
+
const config = getPgEnvOptions({ database });
|
|
11
|
+
const connectionString = buildConnectionString(config.user, config.password, config.host, config.port, config.database);
|
|
12
|
+
const basePreset = {
|
|
13
|
+
extends: [ConstructivePreset],
|
|
14
|
+
pgServices: [
|
|
15
|
+
makePgService({
|
|
16
|
+
connectionString,
|
|
17
|
+
schemas,
|
|
18
|
+
}),
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
const preset = opts.graphile
|
|
22
|
+
? deepmerge(basePreset, opts.graphile)
|
|
23
|
+
: basePreset;
|
|
24
|
+
const { schema } = await makeSchema(preset);
|
|
25
|
+
return printSchema(schema);
|
|
26
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { getIntrospectionQuery, buildClientSchema, printSchema } from 'graphql';
|
|
2
|
+
import * as http from 'node:http';
|
|
3
|
+
import * as https from 'node:https';
|
|
4
|
+
export async function fetchEndpointSchemaSDL(endpoint, opts) {
|
|
5
|
+
const url = new URL(endpoint);
|
|
6
|
+
const requestUrl = url;
|
|
7
|
+
const introspectionQuery = getIntrospectionQuery({ descriptions: true });
|
|
8
|
+
const postData = JSON.stringify({
|
|
9
|
+
query: introspectionQuery,
|
|
10
|
+
variables: null,
|
|
11
|
+
operationName: 'IntrospectionQuery',
|
|
12
|
+
});
|
|
13
|
+
const headers = {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
'Content-Length': String(Buffer.byteLength(postData)),
|
|
16
|
+
};
|
|
17
|
+
if (opts?.headerHost) {
|
|
18
|
+
headers['Host'] = opts.headerHost;
|
|
19
|
+
}
|
|
20
|
+
if (opts?.auth) {
|
|
21
|
+
headers['Authorization'] = opts.auth;
|
|
22
|
+
}
|
|
23
|
+
if (opts?.headers) {
|
|
24
|
+
for (const [key, value] of Object.entries(opts.headers)) {
|
|
25
|
+
headers[key] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const isHttps = requestUrl.protocol === 'https:';
|
|
29
|
+
const lib = isHttps ? https : http;
|
|
30
|
+
const responseData = await new Promise((resolve, reject) => {
|
|
31
|
+
const req = lib.request({
|
|
32
|
+
hostname: requestUrl.hostname,
|
|
33
|
+
port: (requestUrl.port ? Number(requestUrl.port) : (isHttps ? 443 : 80)),
|
|
34
|
+
path: requestUrl.pathname,
|
|
35
|
+
method: 'POST',
|
|
36
|
+
headers,
|
|
37
|
+
}, (res) => {
|
|
38
|
+
let data = '';
|
|
39
|
+
res.on('data', (chunk) => { data += chunk; });
|
|
40
|
+
res.on('end', () => {
|
|
41
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
42
|
+
reject(new Error(`HTTP ${res.statusCode} – ${data}`));
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
resolve(data);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
req.on('error', (err) => reject(err));
|
|
49
|
+
req.write(postData);
|
|
50
|
+
req.end();
|
|
51
|
+
});
|
|
52
|
+
let json;
|
|
53
|
+
try {
|
|
54
|
+
json = JSON.parse(responseData);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
throw new Error(`Failed to parse response: ${responseData}`);
|
|
58
|
+
}
|
|
59
|
+
if (json.errors) {
|
|
60
|
+
throw new Error('Introspection returned errors');
|
|
61
|
+
}
|
|
62
|
+
if (!json.data) {
|
|
63
|
+
throw new Error('No data in introspection response');
|
|
64
|
+
}
|
|
65
|
+
const schema = buildClientSchema(json.data);
|
|
66
|
+
return printSchema(schema);
|
|
67
|
+
}
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.fetchEndpointSchemaSDL = fetchEndpointSchemaSDL;
|
|
37
|
+
const graphql_1 = require("graphql");
|
|
38
|
+
const http = __importStar(require("node:http"));
|
|
39
|
+
const https = __importStar(require("node:https"));
|
|
40
|
+
async function fetchEndpointSchemaSDL(endpoint, opts) {
|
|
41
|
+
const url = new URL(endpoint);
|
|
42
|
+
const requestUrl = url;
|
|
43
|
+
const introspectionQuery = (0, graphql_1.getIntrospectionQuery)({ descriptions: true });
|
|
44
|
+
const postData = JSON.stringify({
|
|
45
|
+
query: introspectionQuery,
|
|
46
|
+
variables: null,
|
|
47
|
+
operationName: 'IntrospectionQuery',
|
|
48
|
+
});
|
|
49
|
+
const headers = {
|
|
50
|
+
'Content-Type': 'application/json',
|
|
51
|
+
'Content-Length': String(Buffer.byteLength(postData)),
|
|
52
|
+
};
|
|
53
|
+
if (opts?.headerHost) {
|
|
54
|
+
headers['Host'] = opts.headerHost;
|
|
55
|
+
}
|
|
56
|
+
if (opts?.auth) {
|
|
57
|
+
headers['Authorization'] = opts.auth;
|
|
58
|
+
}
|
|
59
|
+
if (opts?.headers) {
|
|
60
|
+
for (const [key, value] of Object.entries(opts.headers)) {
|
|
61
|
+
headers[key] = value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const isHttps = requestUrl.protocol === 'https:';
|
|
65
|
+
const lib = isHttps ? https : http;
|
|
66
|
+
const responseData = await new Promise((resolve, reject) => {
|
|
67
|
+
const req = lib.request({
|
|
68
|
+
hostname: requestUrl.hostname,
|
|
69
|
+
port: (requestUrl.port ? Number(requestUrl.port) : (isHttps ? 443 : 80)),
|
|
70
|
+
path: requestUrl.pathname,
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers,
|
|
73
|
+
}, (res) => {
|
|
74
|
+
let data = '';
|
|
75
|
+
res.on('data', (chunk) => { data += chunk; });
|
|
76
|
+
res.on('end', () => {
|
|
77
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
78
|
+
reject(new Error(`HTTP ${res.statusCode} – ${data}`));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
resolve(data);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
req.on('error', (err) => reject(err));
|
|
85
|
+
req.write(postData);
|
|
86
|
+
req.end();
|
|
87
|
+
});
|
|
88
|
+
let json;
|
|
89
|
+
try {
|
|
90
|
+
json = JSON.parse(responseData);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
throw new Error(`Failed to parse response: ${responseData}`);
|
|
94
|
+
}
|
|
95
|
+
if (json.errors) {
|
|
96
|
+
throw new Error('Introspection returned errors');
|
|
97
|
+
}
|
|
98
|
+
if (!json.data) {
|
|
99
|
+
throw new Error('No data in introspection response');
|
|
100
|
+
}
|
|
101
|
+
const schema = (0, graphql_1.buildClientSchema)(json.data);
|
|
102
|
+
return (0, graphql_1.printSchema)(schema);
|
|
103
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchEndpointSchemaSDL = exports.buildSchemaSDL = void 0;
|
|
4
|
+
var build_schema_1 = require("./build-schema");
|
|
5
|
+
Object.defineProperty(exports, "buildSchemaSDL", { enumerable: true, get: function () { return build_schema_1.buildSchemaSDL; } });
|
|
6
|
+
var fetch_endpoint_schema_1 = require("./fetch-endpoint-schema");
|
|
7
|
+
Object.defineProperty(exports, "fetchEndpointSchemaSDL", { enumerable: true, get: function () { return fetch_endpoint_schema_1.fetchEndpointSchemaSDL; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphile-schema",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Build GraphQL SDL from PostgreSQL databases using PostGraphile v5",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"deepmerge": "^4.3.1",
|
|
33
|
+
"graphile-build": "^5.0.0-rc.3",
|
|
34
|
+
"graphile-config": "1.0.0-rc.3",
|
|
35
|
+
"graphile-settings": "^4.1.0",
|
|
36
|
+
"graphql": "^16.9.0",
|
|
37
|
+
"pg-cache": "^3.0.0",
|
|
38
|
+
"pg-env": "^1.4.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"makage": "^0.1.10",
|
|
42
|
+
"ts-node": "^10.9.2"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"graphile",
|
|
46
|
+
"schema",
|
|
47
|
+
"graphql",
|
|
48
|
+
"sdl",
|
|
49
|
+
"postgraphile",
|
|
50
|
+
"introspection",
|
|
51
|
+
"constructive"
|
|
52
|
+
],
|
|
53
|
+
"gitHead": "6dac0247c675de94b41038ac1e5095dc5df0c753"
|
|
54
|
+
}
|