graphile-query 2.6.0 → 4.0.1
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/esm/index.js +161 -31
- package/index.d.ts +101 -15
- package/index.js +163 -31
- package/package.json +14 -6
package/esm/index.js
CHANGED
|
@@ -1,64 +1,194 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { parse } from 'graphql';
|
|
2
|
+
import { makeSchema } from 'graphile-build';
|
|
3
|
+
import { defaultPreset as graphileBuildDefaultPreset } from 'graphile-build';
|
|
4
|
+
import { defaultPreset as graphileBuildPgDefaultPreset, withPgClientFromPgService } from 'graphile-build-pg';
|
|
5
|
+
import { makePgService } from 'graphile-settings';
|
|
6
|
+
import { execute } from 'grafast';
|
|
7
|
+
/**
|
|
8
|
+
* Minimal preset that provides core functionality without Node/Relay.
|
|
9
|
+
* This matches the pattern from graphile-settings.
|
|
10
|
+
*/
|
|
11
|
+
const MinimalPreset = {
|
|
12
|
+
extends: [graphileBuildDefaultPreset, graphileBuildPgDefaultPreset],
|
|
13
|
+
disablePlugins: ['NodePlugin'],
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Create a GraphQL schema using PostGraphile v5's preset-based API.
|
|
17
|
+
*
|
|
18
|
+
* @param pool - PostgreSQL connection pool
|
|
19
|
+
* @param settings - Schema configuration including database schemas and optional preset
|
|
20
|
+
* @returns Promise resolving to schema, resolved preset, and pgService
|
|
21
|
+
*/
|
|
22
|
+
export const getSchema = async (pool, settings) => {
|
|
23
|
+
const schemas = Array.isArray(settings.schema) ? settings.schema : [settings.schema];
|
|
24
|
+
// Create the pgService for database access
|
|
25
|
+
const pgService = makePgService({
|
|
26
|
+
pool,
|
|
27
|
+
schemas,
|
|
28
|
+
});
|
|
29
|
+
// Build the complete preset
|
|
30
|
+
const completePreset = {
|
|
31
|
+
extends: [
|
|
32
|
+
MinimalPreset,
|
|
33
|
+
...(settings.preset?.extends ?? []),
|
|
34
|
+
],
|
|
35
|
+
...(settings.preset?.disablePlugins && { disablePlugins: settings.preset.disablePlugins }),
|
|
36
|
+
...(settings.preset?.plugins && { plugins: settings.preset.plugins }),
|
|
37
|
+
...(settings.preset?.schema && { schema: settings.preset.schema }),
|
|
38
|
+
...(settings.preset?.grafast && { grafast: settings.preset.grafast }),
|
|
39
|
+
pgServices: [pgService],
|
|
40
|
+
};
|
|
41
|
+
// Use makeSchema from graphile-build to create the schema
|
|
42
|
+
const result = await makeSchema(completePreset);
|
|
43
|
+
return {
|
|
44
|
+
schema: result.schema,
|
|
45
|
+
resolvedPreset: result.resolvedPreset,
|
|
46
|
+
pgService,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* GraphileQuery provides a way to execute GraphQL queries against a PostGraphile v5 schema.
|
|
51
|
+
*
|
|
52
|
+
* This is the full-featured version that supports pgSettings generation from requests.
|
|
53
|
+
*/
|
|
5
54
|
export class GraphileQuery {
|
|
6
55
|
pool;
|
|
7
56
|
schema;
|
|
8
57
|
settings;
|
|
9
|
-
|
|
58
|
+
resolvedPreset;
|
|
59
|
+
pgService;
|
|
60
|
+
constructor({ schema, pool, settings, resolvedPreset, pgService }) {
|
|
10
61
|
if (!schema)
|
|
11
62
|
throw new Error('requires a schema');
|
|
12
63
|
if (!pool)
|
|
13
64
|
throw new Error('requires a pool');
|
|
14
65
|
if (!settings)
|
|
15
66
|
throw new Error('requires graphile settings');
|
|
67
|
+
if (!resolvedPreset)
|
|
68
|
+
throw new Error('requires resolvedPreset');
|
|
69
|
+
if (!pgService)
|
|
70
|
+
throw new Error('requires pgService');
|
|
16
71
|
this.pool = pool;
|
|
17
72
|
this.schema = schema;
|
|
18
73
|
this.settings = settings;
|
|
74
|
+
this.resolvedPreset = resolvedPreset;
|
|
75
|
+
this.pgService = pgService;
|
|
19
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Execute a GraphQL query.
|
|
79
|
+
*
|
|
80
|
+
* @param options - Query options including query string, variables, and optional role
|
|
81
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
82
|
+
*/
|
|
20
83
|
async query({ req = {}, query, variables, role }) {
|
|
21
|
-
|
|
84
|
+
// Parse the query if it's a string
|
|
85
|
+
const document = typeof query === 'string' ? parse(query) : query;
|
|
86
|
+
// Generate pgSettings
|
|
22
87
|
const { pgSettings: pgSettingsGenerator } = this.settings;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
88
|
+
let pgSettings = {};
|
|
89
|
+
if (role != null) {
|
|
90
|
+
pgSettings = { role };
|
|
91
|
+
}
|
|
92
|
+
else if (typeof pgSettingsGenerator === 'function') {
|
|
93
|
+
pgSettings = await pgSettingsGenerator(req);
|
|
94
|
+
}
|
|
95
|
+
// Build context with withPgClient using withPgClientFromPgService
|
|
96
|
+
const withPgClientKey = this.pgService.withPgClientKey ?? 'withPgClient';
|
|
97
|
+
const contextValue = {
|
|
98
|
+
pgSettings,
|
|
99
|
+
[withPgClientKey]: withPgClientFromPgService.bind(null, this.pgService),
|
|
100
|
+
};
|
|
101
|
+
// Execute using grafast
|
|
102
|
+
const result = await execute({
|
|
103
|
+
schema: this.schema,
|
|
104
|
+
document,
|
|
105
|
+
variableValues: variables ?? undefined,
|
|
106
|
+
contextValue,
|
|
107
|
+
resolvedPreset: this.resolvedPreset,
|
|
39
108
|
});
|
|
109
|
+
// Handle streaming results
|
|
110
|
+
if (Symbol.asyncIterator in result) {
|
|
111
|
+
throw new Error('Streaming results (subscriptions) are not supported');
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
40
114
|
}
|
|
41
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* GraphileQuerySimple provides a simplified way to execute GraphQL queries.
|
|
118
|
+
*
|
|
119
|
+
* This version doesn't support pgSettings generation from requests - it's meant
|
|
120
|
+
* for simple use cases where you just need to run queries.
|
|
121
|
+
*/
|
|
42
122
|
export class GraphileQuerySimple {
|
|
43
123
|
pool;
|
|
44
124
|
schema;
|
|
45
|
-
|
|
125
|
+
resolvedPreset;
|
|
126
|
+
pgService;
|
|
127
|
+
constructor({ schema, pool, resolvedPreset, pgService }) {
|
|
46
128
|
if (!schema)
|
|
47
129
|
throw new Error('requires a schema');
|
|
48
130
|
if (!pool)
|
|
49
131
|
throw new Error('requires a pool');
|
|
132
|
+
if (!resolvedPreset)
|
|
133
|
+
throw new Error('requires resolvedPreset');
|
|
134
|
+
if (!pgService)
|
|
135
|
+
throw new Error('requires pgService');
|
|
50
136
|
this.pool = pool;
|
|
51
137
|
this.schema = schema;
|
|
138
|
+
this.resolvedPreset = resolvedPreset;
|
|
139
|
+
this.pgService = pgService;
|
|
52
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Execute a GraphQL query.
|
|
143
|
+
*
|
|
144
|
+
* @param query - GraphQL query string or DocumentNode
|
|
145
|
+
* @param variables - Optional query variables
|
|
146
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
147
|
+
*/
|
|
53
148
|
async query(query, variables) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
149
|
+
// Parse the query if it's a string
|
|
150
|
+
const document = typeof query === 'string' ? parse(query) : query;
|
|
151
|
+
// Build context with withPgClient using withPgClientFromPgService
|
|
152
|
+
const withPgClientKey = this.pgService.withPgClientKey ?? 'withPgClient';
|
|
153
|
+
const contextValue = {
|
|
154
|
+
[withPgClientKey]: withPgClientFromPgService.bind(null, this.pgService),
|
|
155
|
+
};
|
|
156
|
+
// Execute using grafast
|
|
157
|
+
const result = await execute({
|
|
158
|
+
schema: this.schema,
|
|
159
|
+
document,
|
|
160
|
+
variableValues: variables ?? undefined,
|
|
161
|
+
contextValue,
|
|
162
|
+
resolvedPreset: this.resolvedPreset,
|
|
62
163
|
});
|
|
164
|
+
// Handle streaming results
|
|
165
|
+
if (Symbol.asyncIterator in result) {
|
|
166
|
+
throw new Error('Streaming results (subscriptions) are not supported');
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
63
169
|
}
|
|
64
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Helper function to create a GraphileQuery instance with schema creation.
|
|
173
|
+
*
|
|
174
|
+
* This is a convenience function that combines getSchema and GraphileQuery creation.
|
|
175
|
+
*
|
|
176
|
+
* @param pool - PostgreSQL connection pool
|
|
177
|
+
* @param settings - Schema configuration
|
|
178
|
+
* @returns Promise resolving to a GraphileQuery instance
|
|
179
|
+
*/
|
|
180
|
+
export const createGraphileQuery = async (pool, settings) => {
|
|
181
|
+
const { schema, resolvedPreset, pgService } = await getSchema(pool, settings);
|
|
182
|
+
return new GraphileQuery({ schema, pool, settings, resolvedPreset, pgService });
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Helper function to create a GraphileQuerySimple instance with schema creation.
|
|
186
|
+
*
|
|
187
|
+
* @param pool - PostgreSQL connection pool
|
|
188
|
+
* @param settings - Schema configuration
|
|
189
|
+
* @returns Promise resolving to a GraphileQuerySimple instance
|
|
190
|
+
*/
|
|
191
|
+
export const createGraphileQuerySimple = async (pool, settings) => {
|
|
192
|
+
const { schema, resolvedPreset, pgService } = await getSchema(pool, settings);
|
|
193
|
+
return new GraphileQuerySimple({ schema, pool, resolvedPreset, pgService });
|
|
194
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,36 +1,122 @@
|
|
|
1
|
-
import { ExecutionResult, GraphQLSchema } from 'graphql';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import type { ExecutionResult, GraphQLSchema, DocumentNode } from 'graphql';
|
|
2
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
3
|
+
import { makePgService } from 'graphile-settings';
|
|
4
|
+
import type { Pool } from 'pg';
|
|
5
|
+
/**
|
|
6
|
+
* Settings for creating a GraphQL schema with PostGraphile v5.
|
|
7
|
+
*/
|
|
8
|
+
export interface GraphileSettings {
|
|
9
|
+
/** Database schema(s) to expose */
|
|
5
10
|
schema: string | string[];
|
|
11
|
+
/** Optional preset to extend the minimal preset */
|
|
12
|
+
preset?: GraphileConfig.Preset;
|
|
13
|
+
/** Function to generate pgSettings from a request object */
|
|
14
|
+
pgSettings?: (req: unknown) => Record<string, string> | Promise<Record<string, string>>;
|
|
6
15
|
}
|
|
7
|
-
|
|
8
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Result from getSchema containing the schema and resolved preset.
|
|
18
|
+
*/
|
|
19
|
+
export interface SchemaResult {
|
|
20
|
+
schema: GraphQLSchema;
|
|
21
|
+
resolvedPreset: GraphileConfig.ResolvedPreset;
|
|
22
|
+
pgService: ReturnType<typeof makePgService>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a GraphQL schema using PostGraphile v5's preset-based API.
|
|
26
|
+
*
|
|
27
|
+
* @param pool - PostgreSQL connection pool
|
|
28
|
+
* @param settings - Schema configuration including database schemas and optional preset
|
|
29
|
+
* @returns Promise resolving to schema, resolved preset, and pgService
|
|
30
|
+
*/
|
|
31
|
+
export declare const getSchema: (pool: Pool, settings: GraphileSettings) => Promise<SchemaResult>;
|
|
32
|
+
/**
|
|
33
|
+
* Parameters for creating a GraphileQuery instance.
|
|
34
|
+
*/
|
|
35
|
+
export interface GraphileQueryParams {
|
|
9
36
|
schema: GraphQLSchema;
|
|
10
37
|
pool: Pool;
|
|
11
38
|
settings: GraphileSettings;
|
|
39
|
+
resolvedPreset: GraphileConfig.ResolvedPreset;
|
|
40
|
+
pgService: ReturnType<typeof makePgService>;
|
|
12
41
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Options for executing a GraphQL query.
|
|
44
|
+
*/
|
|
45
|
+
export interface QueryOptions {
|
|
46
|
+
/** Optional request object for pgSettings generation */
|
|
47
|
+
req?: unknown;
|
|
48
|
+
/** GraphQL query string or DocumentNode */
|
|
49
|
+
query: string | DocumentNode;
|
|
50
|
+
/** Query variables */
|
|
51
|
+
variables?: Record<string, unknown>;
|
|
52
|
+
/** PostgreSQL role to use for the query */
|
|
17
53
|
role?: string;
|
|
18
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* GraphileQuery provides a way to execute GraphQL queries against a PostGraphile v5 schema.
|
|
57
|
+
*
|
|
58
|
+
* This is the full-featured version that supports pgSettings generation from requests.
|
|
59
|
+
*/
|
|
19
60
|
export declare class GraphileQuery {
|
|
20
61
|
private pool;
|
|
21
62
|
private schema;
|
|
22
63
|
private settings;
|
|
23
|
-
|
|
64
|
+
private resolvedPreset;
|
|
65
|
+
private pgService;
|
|
66
|
+
constructor({ schema, pool, settings, resolvedPreset, pgService }: GraphileQueryParams);
|
|
67
|
+
/**
|
|
68
|
+
* Execute a GraphQL query.
|
|
69
|
+
*
|
|
70
|
+
* @param options - Query options including query string, variables, and optional role
|
|
71
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
72
|
+
*/
|
|
24
73
|
query({ req, query, variables, role }: QueryOptions): Promise<ExecutionResult>;
|
|
25
74
|
}
|
|
26
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Parameters for creating a GraphileQuerySimple instance.
|
|
77
|
+
*/
|
|
78
|
+
export interface GraphileQuerySimpleParams {
|
|
27
79
|
schema: GraphQLSchema;
|
|
28
80
|
pool: Pool;
|
|
81
|
+
resolvedPreset: GraphileConfig.ResolvedPreset;
|
|
82
|
+
pgService: ReturnType<typeof makePgService>;
|
|
29
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* GraphileQuerySimple provides a simplified way to execute GraphQL queries.
|
|
86
|
+
*
|
|
87
|
+
* This version doesn't support pgSettings generation from requests - it's meant
|
|
88
|
+
* for simple use cases where you just need to run queries.
|
|
89
|
+
*/
|
|
30
90
|
export declare class GraphileQuerySimple {
|
|
31
91
|
private pool;
|
|
32
92
|
private schema;
|
|
33
|
-
|
|
34
|
-
|
|
93
|
+
private resolvedPreset;
|
|
94
|
+
private pgService;
|
|
95
|
+
constructor({ schema, pool, resolvedPreset, pgService }: GraphileQuerySimpleParams);
|
|
96
|
+
/**
|
|
97
|
+
* Execute a GraphQL query.
|
|
98
|
+
*
|
|
99
|
+
* @param query - GraphQL query string or DocumentNode
|
|
100
|
+
* @param variables - Optional query variables
|
|
101
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
102
|
+
*/
|
|
103
|
+
query(query: string | DocumentNode, variables?: Record<string, unknown>): Promise<ExecutionResult>;
|
|
35
104
|
}
|
|
36
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Helper function to create a GraphileQuery instance with schema creation.
|
|
107
|
+
*
|
|
108
|
+
* This is a convenience function that combines getSchema and GraphileQuery creation.
|
|
109
|
+
*
|
|
110
|
+
* @param pool - PostgreSQL connection pool
|
|
111
|
+
* @param settings - Schema configuration
|
|
112
|
+
* @returns Promise resolving to a GraphileQuery instance
|
|
113
|
+
*/
|
|
114
|
+
export declare const createGraphileQuery: (pool: Pool, settings: GraphileSettings) => Promise<GraphileQuery>;
|
|
115
|
+
/**
|
|
116
|
+
* Helper function to create a GraphileQuerySimple instance with schema creation.
|
|
117
|
+
*
|
|
118
|
+
* @param pool - PostgreSQL connection pool
|
|
119
|
+
* @param settings - Schema configuration
|
|
120
|
+
* @returns Promise resolving to a GraphileQuerySimple instance
|
|
121
|
+
*/
|
|
122
|
+
export declare const createGraphileQuerySimple: (pool: Pool, settings: GraphileSettings) => Promise<GraphileQuerySimple>;
|
package/index.js
CHANGED
|
@@ -1,70 +1,202 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GraphileQuerySimple = exports.GraphileQuery = exports.getSchema = void 0;
|
|
3
|
+
exports.createGraphileQuerySimple = exports.createGraphileQuery = exports.GraphileQuerySimple = exports.GraphileQuery = exports.getSchema = void 0;
|
|
4
4
|
const graphql_1 = require("graphql");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
5
|
+
const graphile_build_1 = require("graphile-build");
|
|
6
|
+
const graphile_build_2 = require("graphile-build");
|
|
7
|
+
const graphile_build_pg_1 = require("graphile-build-pg");
|
|
8
|
+
const graphile_settings_1 = require("graphile-settings");
|
|
9
|
+
const grafast_1 = require("grafast");
|
|
10
|
+
/**
|
|
11
|
+
* Minimal preset that provides core functionality without Node/Relay.
|
|
12
|
+
* This matches the pattern from graphile-settings.
|
|
13
|
+
*/
|
|
14
|
+
const MinimalPreset = {
|
|
15
|
+
extends: [graphile_build_2.defaultPreset, graphile_build_pg_1.defaultPreset],
|
|
16
|
+
disablePlugins: ['NodePlugin'],
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Create a GraphQL schema using PostGraphile v5's preset-based API.
|
|
20
|
+
*
|
|
21
|
+
* @param pool - PostgreSQL connection pool
|
|
22
|
+
* @param settings - Schema configuration including database schemas and optional preset
|
|
23
|
+
* @returns Promise resolving to schema, resolved preset, and pgService
|
|
24
|
+
*/
|
|
25
|
+
const getSchema = async (pool, settings) => {
|
|
26
|
+
const schemas = Array.isArray(settings.schema) ? settings.schema : [settings.schema];
|
|
27
|
+
// Create the pgService for database access
|
|
28
|
+
const pgService = (0, graphile_settings_1.makePgService)({
|
|
29
|
+
pool,
|
|
30
|
+
schemas,
|
|
31
|
+
});
|
|
32
|
+
// Build the complete preset
|
|
33
|
+
const completePreset = {
|
|
34
|
+
extends: [
|
|
35
|
+
MinimalPreset,
|
|
36
|
+
...(settings.preset?.extends ?? []),
|
|
37
|
+
],
|
|
38
|
+
...(settings.preset?.disablePlugins && { disablePlugins: settings.preset.disablePlugins }),
|
|
39
|
+
...(settings.preset?.plugins && { plugins: settings.preset.plugins }),
|
|
40
|
+
...(settings.preset?.schema && { schema: settings.preset.schema }),
|
|
41
|
+
...(settings.preset?.grafast && { grafast: settings.preset.grafast }),
|
|
42
|
+
pgServices: [pgService],
|
|
43
|
+
};
|
|
44
|
+
// Use makeSchema from graphile-build to create the schema
|
|
45
|
+
const result = await (0, graphile_build_1.makeSchema)(completePreset);
|
|
46
|
+
return {
|
|
47
|
+
schema: result.schema,
|
|
48
|
+
resolvedPreset: result.resolvedPreset,
|
|
49
|
+
pgService,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
8
52
|
exports.getSchema = getSchema;
|
|
53
|
+
/**
|
|
54
|
+
* GraphileQuery provides a way to execute GraphQL queries against a PostGraphile v5 schema.
|
|
55
|
+
*
|
|
56
|
+
* This is the full-featured version that supports pgSettings generation from requests.
|
|
57
|
+
*/
|
|
9
58
|
class GraphileQuery {
|
|
10
59
|
pool;
|
|
11
60
|
schema;
|
|
12
61
|
settings;
|
|
13
|
-
|
|
62
|
+
resolvedPreset;
|
|
63
|
+
pgService;
|
|
64
|
+
constructor({ schema, pool, settings, resolvedPreset, pgService }) {
|
|
14
65
|
if (!schema)
|
|
15
66
|
throw new Error('requires a schema');
|
|
16
67
|
if (!pool)
|
|
17
68
|
throw new Error('requires a pool');
|
|
18
69
|
if (!settings)
|
|
19
70
|
throw new Error('requires graphile settings');
|
|
71
|
+
if (!resolvedPreset)
|
|
72
|
+
throw new Error('requires resolvedPreset');
|
|
73
|
+
if (!pgService)
|
|
74
|
+
throw new Error('requires pgService');
|
|
20
75
|
this.pool = pool;
|
|
21
76
|
this.schema = schema;
|
|
22
77
|
this.settings = settings;
|
|
78
|
+
this.resolvedPreset = resolvedPreset;
|
|
79
|
+
this.pgService = pgService;
|
|
23
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Execute a GraphQL query.
|
|
83
|
+
*
|
|
84
|
+
* @param options - Query options including query string, variables, and optional role
|
|
85
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
86
|
+
*/
|
|
24
87
|
async query({ req = {}, query, variables, role }) {
|
|
25
|
-
|
|
88
|
+
// Parse the query if it's a string
|
|
89
|
+
const document = typeof query === 'string' ? (0, graphql_1.parse)(query) : query;
|
|
90
|
+
// Generate pgSettings
|
|
26
91
|
const { pgSettings: pgSettingsGenerator } = this.settings;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
92
|
+
let pgSettings = {};
|
|
93
|
+
if (role != null) {
|
|
94
|
+
pgSettings = { role };
|
|
95
|
+
}
|
|
96
|
+
else if (typeof pgSettingsGenerator === 'function') {
|
|
97
|
+
pgSettings = await pgSettingsGenerator(req);
|
|
98
|
+
}
|
|
99
|
+
// Build context with withPgClient using withPgClientFromPgService
|
|
100
|
+
const withPgClientKey = this.pgService.withPgClientKey ?? 'withPgClient';
|
|
101
|
+
const contextValue = {
|
|
102
|
+
pgSettings,
|
|
103
|
+
[withPgClientKey]: graphile_build_pg_1.withPgClientFromPgService.bind(null, this.pgService),
|
|
104
|
+
};
|
|
105
|
+
// Execute using grafast
|
|
106
|
+
const result = await (0, grafast_1.execute)({
|
|
107
|
+
schema: this.schema,
|
|
108
|
+
document,
|
|
109
|
+
variableValues: variables ?? undefined,
|
|
110
|
+
contextValue,
|
|
111
|
+
resolvedPreset: this.resolvedPreset,
|
|
43
112
|
});
|
|
113
|
+
// Handle streaming results
|
|
114
|
+
if (Symbol.asyncIterator in result) {
|
|
115
|
+
throw new Error('Streaming results (subscriptions) are not supported');
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
44
118
|
}
|
|
45
119
|
}
|
|
46
120
|
exports.GraphileQuery = GraphileQuery;
|
|
121
|
+
/**
|
|
122
|
+
* GraphileQuerySimple provides a simplified way to execute GraphQL queries.
|
|
123
|
+
*
|
|
124
|
+
* This version doesn't support pgSettings generation from requests - it's meant
|
|
125
|
+
* for simple use cases where you just need to run queries.
|
|
126
|
+
*/
|
|
47
127
|
class GraphileQuerySimple {
|
|
48
128
|
pool;
|
|
49
129
|
schema;
|
|
50
|
-
|
|
130
|
+
resolvedPreset;
|
|
131
|
+
pgService;
|
|
132
|
+
constructor({ schema, pool, resolvedPreset, pgService }) {
|
|
51
133
|
if (!schema)
|
|
52
134
|
throw new Error('requires a schema');
|
|
53
135
|
if (!pool)
|
|
54
136
|
throw new Error('requires a pool');
|
|
137
|
+
if (!resolvedPreset)
|
|
138
|
+
throw new Error('requires resolvedPreset');
|
|
139
|
+
if (!pgService)
|
|
140
|
+
throw new Error('requires pgService');
|
|
55
141
|
this.pool = pool;
|
|
56
142
|
this.schema = schema;
|
|
143
|
+
this.resolvedPreset = resolvedPreset;
|
|
144
|
+
this.pgService = pgService;
|
|
57
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Execute a GraphQL query.
|
|
148
|
+
*
|
|
149
|
+
* @param query - GraphQL query string or DocumentNode
|
|
150
|
+
* @param variables - Optional query variables
|
|
151
|
+
* @returns Promise resolving to the GraphQL execution result
|
|
152
|
+
*/
|
|
58
153
|
async query(query, variables) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
154
|
+
// Parse the query if it's a string
|
|
155
|
+
const document = typeof query === 'string' ? (0, graphql_1.parse)(query) : query;
|
|
156
|
+
// Build context with withPgClient using withPgClientFromPgService
|
|
157
|
+
const withPgClientKey = this.pgService.withPgClientKey ?? 'withPgClient';
|
|
158
|
+
const contextValue = {
|
|
159
|
+
[withPgClientKey]: graphile_build_pg_1.withPgClientFromPgService.bind(null, this.pgService),
|
|
160
|
+
};
|
|
161
|
+
// Execute using grafast
|
|
162
|
+
const result = await (0, grafast_1.execute)({
|
|
163
|
+
schema: this.schema,
|
|
164
|
+
document,
|
|
165
|
+
variableValues: variables ?? undefined,
|
|
166
|
+
contextValue,
|
|
167
|
+
resolvedPreset: this.resolvedPreset,
|
|
67
168
|
});
|
|
169
|
+
// Handle streaming results
|
|
170
|
+
if (Symbol.asyncIterator in result) {
|
|
171
|
+
throw new Error('Streaming results (subscriptions) are not supported');
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
68
174
|
}
|
|
69
175
|
}
|
|
70
176
|
exports.GraphileQuerySimple = GraphileQuerySimple;
|
|
177
|
+
/**
|
|
178
|
+
* Helper function to create a GraphileQuery instance with schema creation.
|
|
179
|
+
*
|
|
180
|
+
* This is a convenience function that combines getSchema and GraphileQuery creation.
|
|
181
|
+
*
|
|
182
|
+
* @param pool - PostgreSQL connection pool
|
|
183
|
+
* @param settings - Schema configuration
|
|
184
|
+
* @returns Promise resolving to a GraphileQuery instance
|
|
185
|
+
*/
|
|
186
|
+
const createGraphileQuery = async (pool, settings) => {
|
|
187
|
+
const { schema, resolvedPreset, pgService } = await (0, exports.getSchema)(pool, settings);
|
|
188
|
+
return new GraphileQuery({ schema, pool, settings, resolvedPreset, pgService });
|
|
189
|
+
};
|
|
190
|
+
exports.createGraphileQuery = createGraphileQuery;
|
|
191
|
+
/**
|
|
192
|
+
* Helper function to create a GraphileQuerySimple instance with schema creation.
|
|
193
|
+
*
|
|
194
|
+
* @param pool - PostgreSQL connection pool
|
|
195
|
+
* @param settings - Schema configuration
|
|
196
|
+
* @returns Promise resolving to a GraphileQuerySimple instance
|
|
197
|
+
*/
|
|
198
|
+
const createGraphileQuerySimple = async (pool, settings) => {
|
|
199
|
+
const { schema, resolvedPreset, pgService } = await (0, exports.getSchema)(pool, settings);
|
|
200
|
+
return new GraphileQuerySimple({ schema, pool, resolvedPreset, pgService });
|
|
201
|
+
};
|
|
202
|
+
exports.createGraphileQuerySimple = createGraphileQuerySimple;
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphile-query",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Execute GraphQL queries against PostGraphile v5 schemas",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
8
|
"types": "index.d.ts",
|
|
@@ -29,21 +29,29 @@
|
|
|
29
29
|
"test:watch": "jest --watch"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
32
|
+
"grafast": "^1.0.0-rc.4",
|
|
33
|
+
"graphile-build": "^5.0.0-rc.3",
|
|
34
|
+
"graphile-build-pg": "^5.0.0-rc.3",
|
|
35
|
+
"graphile-config": "1.0.0-rc.3",
|
|
36
|
+
"graphile-settings": "^4.0.1",
|
|
37
|
+
"graphql": "^16.9.0",
|
|
33
38
|
"pg": "^8.17.1",
|
|
34
|
-
"postgraphile": "^
|
|
39
|
+
"postgraphile": "^5.0.0-rc.4"
|
|
35
40
|
},
|
|
36
41
|
"devDependencies": {
|
|
37
42
|
"@types/pg": "^8.16.0",
|
|
38
|
-
"makage": "^0.1.
|
|
43
|
+
"makage": "^0.1.10",
|
|
44
|
+
"pgsql-test": "^4.0.0"
|
|
39
45
|
},
|
|
40
46
|
"keywords": [
|
|
41
47
|
"graphql",
|
|
42
48
|
"query",
|
|
43
49
|
"builder",
|
|
44
50
|
"graphile",
|
|
51
|
+
"postgraphile",
|
|
52
|
+
"v5",
|
|
45
53
|
"constructive",
|
|
46
54
|
"pgpm"
|
|
47
55
|
],
|
|
48
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "c05d6bd3dfa36690aefe21079042666dfae801a1"
|
|
49
57
|
}
|