graphile-test 2.1.9 → 2.1.11

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/README.md CHANGED
@@ -56,8 +56,8 @@ afterEach(() => db.afterEach());
56
56
  afterAll(() => teardown());
57
57
 
58
58
  it('runs a GraphQL mutation', async () => {
59
- const res = await query(`mutation { ... }`);
60
- expect(res.errors).toBeUndefined();
59
+ const res = await query(`mutation { ... }`, { input: { ... } });
60
+ expect(res.data.createUser.username).toBe('alice');
61
61
  });
62
62
  ```
63
63
 
@@ -67,10 +67,16 @@ it('runs a GraphQL mutation', async () => {
67
67
 
68
68
  Returns an object with:
69
69
 
70
- * `query` – A GraphQL executor function: `query(gqlDoc, variables?)`
70
+ * `query(gqlString, variables?)` – A GraphQL executor function with positional arguments
71
71
  * `db`, `pg` – `PgTestClient` instances
72
72
  * `teardown()` – Clean up temp DBs
73
73
 
74
+ **Basic Usage:**
75
+ ```ts
76
+ const result = await query(`mutation { ... }`, { input: { ... } });
77
+ expect(result.data.createUser.username).toBe('alice');
78
+ ```
79
+
74
80
  ### `PgTestClient`
75
81
 
76
82
  Supports:
@@ -86,18 +92,91 @@ Supports:
86
92
  ### GraphQL mutation + snapshot
87
93
 
88
94
  ```ts
89
- const res = await query(MY_MUTATION, { input: { ... } });
90
- expect(snapshot(res)).toMatchSnapshot();
95
+ const res = await query(`mutation { ... }`, { input: { ... } });
96
+ expect(snapshot(res.data)).toMatchSnapshot();
91
97
  ```
92
98
 
93
99
  ### RLS testing with role switch
94
100
 
95
101
  ```ts
96
102
  db.setContext({ role: 'anonymous' });
97
- const res = await query(MY_PROTECTED_QUERY);
103
+ const res = await query(`query { ... }`);
98
104
  expect(res.errors[0].message).toMatch(/permission denied/);
99
105
  ```
100
106
 
107
+ ### Typed queries for better safety
108
+
109
+ ```ts
110
+ interface CreateUserVariables {
111
+ input: {
112
+ user: {
113
+ username: string;
114
+ };
115
+ };
116
+ }
117
+
118
+ interface CreateUserResult {
119
+ createUser: {
120
+ user: {
121
+ id: number;
122
+ username: string;
123
+ };
124
+ };
125
+ }
126
+
127
+ const res = await query<CreateUserResult>(`
128
+ mutation CreateUser($input: CreateUserInput!) {
129
+ createUser(input: $input) {
130
+ user {
131
+ id
132
+ username
133
+ }
134
+ }
135
+ }
136
+ `, { input: { user: { username: 'alice' } } });
137
+
138
+ expect(res.data.createUser.user.username).toBe('alice');
139
+ ```
140
+
141
+ ## 🔧 Advanced Connection Options
142
+
143
+ For specific testing needs, additional connection functions are available:
144
+
145
+ ### Error Handling Variants
146
+ - `getConnectionsUnwrapped()` – Automatically throws on GraphQL errors, returns data directly
147
+
148
+ ### Debugging Variants
149
+ - `getConnectionsWithLogging()` – Logs all queries and responses
150
+ - `getConnectionsWithTiming()` – Times query execution
151
+
152
+ ### Object-Based API
153
+ - `getConnectionsObject()` – Uses `query({ query: "...", variables: {} })` syntax
154
+ - `getConnectionsObjectUnwrapped()` – Object-based with automatic error throwing
155
+
156
+ **Unwrapped Example (cleaner assertions):**
157
+ ```ts
158
+ import { getConnectionsUnwrapped } from 'graphile-test';
159
+
160
+ const { query } = await getConnectionsUnwrapped(config);
161
+
162
+ // Throws automatically on GraphQL errors, returns data directly
163
+ const result = await query(`mutation { ... }`, { input: { ... } });
164
+ expect(result.createUser.username).toBe('alice'); // No .data needed!
165
+ ```
166
+
167
+ **Object-Based Example:**
168
+ ```ts
169
+ import { getConnectionsObject } from 'graphile-test';
170
+
171
+ const { query } = await getConnectionsObject(config);
172
+
173
+ const result = await query({
174
+ query: `mutation { ... }`,
175
+ variables: { input: { ... } }
176
+ });
177
+ expect(result.data.createUser.username).toBe('alice');
178
+ ```
179
+
101
180
  ## 🧱 Under the Hood
102
181
 
103
182
  `graphile-test` wraps and extends `pgsql-test` with GraphQL helpers like `query()` and introspection snapshot tools. You can drop into raw SQL testing anytime via `pg.client.query()` (superuser) or `db.client.query()` (RLS user).
@@ -108,6 +187,8 @@ expect(res.errors[0].message).toMatch(/permission denied/);
108
187
  * Always wrap tests with `beforeEach` / `afterEach`.
109
188
  * Use `snapshot()` to track GraphQL result changes.
110
189
  * Use `useRoot: true` to test schema visibility without RLS.
190
+ * Start with `getConnections()` for most use cases.
191
+ * Consider `getConnectionsUnwrapped()` for cleaner test assertions.
111
192
 
112
193
  ## Related LaunchQL Tooling
113
194
 
@@ -0,0 +1,16 @@
1
+ import type { GetConnectionOpts } from 'pgsql-test';
2
+ import type { PgTestClient } from 'pgsql-test/test-client';
3
+ import type { SeedAdapter } from 'pgsql-test/seed/types';
4
+ import type { GraphQLQueryUnwrappedFn, GraphQLQueryUnwrappedFnPos } from './types';
5
+ export interface GetConnectionsInput {
6
+ useRoot?: boolean;
7
+ schemas: string[];
8
+ authRole?: string;
9
+ }
10
+ export declare const getConnectionsUnwrapped: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
11
+ pg: PgTestClient;
12
+ db: PgTestClient;
13
+ teardown: () => Promise<void>;
14
+ query: GraphQLQueryUnwrappedFn;
15
+ queryPositional: GraphQLQueryUnwrappedFnPos;
16
+ }>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConnectionsUnwrapped = void 0;
4
+ const pgsql_test_1 = require("pgsql-test");
5
+ const graphile_test_1 = require("./graphile-test");
6
+ const unwrap = (res) => {
7
+ if (res.errors?.length) {
8
+ throw new Error(JSON.stringify(res.errors, null, 2));
9
+ }
10
+ if (!res.data) {
11
+ throw new Error('No data returned from GraphQL query');
12
+ }
13
+ return res.data;
14
+ };
15
+ const getConnectionsUnwrapped = async (input, seedAdapters) => {
16
+ const conn = await (0, pgsql_test_1.getConnections)(input, seedAdapters);
17
+ const { pg, db, teardown: dbTeardown } = conn;
18
+ const gqlContext = (0, graphile_test_1.GraphQLTest)(input, conn);
19
+ await gqlContext.setup();
20
+ const teardown = async () => {
21
+ await gqlContext.teardown();
22
+ await dbTeardown();
23
+ };
24
+ const query = async (opts) => unwrap(await gqlContext.query(opts));
25
+ const queryPositional = async (query, variables, commit, reqOptions) => unwrap(await gqlContext.query({ query, variables, commit, reqOptions }));
26
+ return {
27
+ pg,
28
+ db,
29
+ teardown,
30
+ query,
31
+ queryPositional
32
+ };
33
+ };
34
+ exports.getConnectionsUnwrapped = getConnectionsUnwrapped;
package/context.d.ts CHANGED
@@ -2,7 +2,7 @@ import { ExecutionResult, DocumentNode } from 'graphql';
2
2
  import { PostGraphileOptions } from 'postgraphile';
3
3
  import type { Client, Pool } from 'pg';
4
4
  import { GetConnectionOpts, GetConnectionResult } from 'pgsql-test';
5
- import { GetConnectionsInput } from './connect';
5
+ import { GetConnectionsInput } from './types';
6
6
  export declare const runGraphQLInContext: <T = ExecutionResult<{
7
7
  [key: string]: any;
8
8
  }, {
@@ -0,0 +1,30 @@
1
+ import { getConnections as getPgConnections } from 'pgsql-test';
2
+ import { GraphQLTest } from './graphile-test';
3
+ const unwrap = (res) => {
4
+ if (res.errors?.length) {
5
+ throw new Error(JSON.stringify(res.errors, null, 2));
6
+ }
7
+ if (!res.data) {
8
+ throw new Error('No data returned from GraphQL query');
9
+ }
10
+ return res.data;
11
+ };
12
+ export const getConnectionsUnwrapped = async (input, seedAdapters) => {
13
+ const conn = await getPgConnections(input, seedAdapters);
14
+ const { pg, db, teardown: dbTeardown } = conn;
15
+ const gqlContext = GraphQLTest(input, conn);
16
+ await gqlContext.setup();
17
+ const teardown = async () => {
18
+ await gqlContext.teardown();
19
+ await dbTeardown();
20
+ };
21
+ const query = async (opts) => unwrap(await gqlContext.query(opts));
22
+ const queryPositional = async (query, variables, commit, reqOptions) => unwrap(await gqlContext.query({ query, variables, commit, reqOptions }));
23
+ return {
24
+ pg,
25
+ db,
26
+ teardown,
27
+ query,
28
+ queryPositional
29
+ };
30
+ };
@@ -0,0 +1,162 @@
1
+ import { getConnections as getPgConnections } from 'pgsql-test';
2
+ import { GraphQLTest } from './graphile-test';
3
+ // Core unwrapping utility
4
+ const unwrap = (res) => {
5
+ if (res.errors?.length) {
6
+ throw new Error(JSON.stringify(res.errors, null, 2));
7
+ }
8
+ if (!res.data) {
9
+ throw new Error('No data returned from GraphQL query');
10
+ }
11
+ return res.data;
12
+ };
13
+ // Base connection setup - shared across all variants
14
+ const createConnectionsBase = async (input, seedAdapters) => {
15
+ const conn = await getPgConnections(input, seedAdapters);
16
+ const { pg, db, teardown: dbTeardown } = conn;
17
+ const gqlContext = GraphQLTest(input, conn);
18
+ await gqlContext.setup();
19
+ const teardown = async () => {
20
+ await gqlContext.teardown();
21
+ await dbTeardown();
22
+ };
23
+ const baseQuery = (opts) => gqlContext.query(opts);
24
+ const baseQueryPositional = (query, variables, commit, reqOptions) => gqlContext.query({ query, variables, commit, reqOptions });
25
+ return {
26
+ pg,
27
+ db,
28
+ teardown,
29
+ baseQuery,
30
+ baseQueryPositional
31
+ };
32
+ };
33
+ // ============================================================================
34
+ // REGULAR QUERY VERSIONS
35
+ // ============================================================================
36
+ /**
37
+ * Creates connections with raw GraphQL responses
38
+ */
39
+ export const getConnectionsObject = async (input, seedAdapters) => {
40
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
41
+ return {
42
+ pg,
43
+ db,
44
+ teardown,
45
+ query: baseQuery
46
+ };
47
+ };
48
+ /**
49
+ * Creates connections with unwrapped GraphQL responses (throws on errors)
50
+ */
51
+ export const getConnectionsObjectUnwrapped = async (input, seedAdapters) => {
52
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
53
+ const query = async (opts) => unwrap(await baseQuery(opts));
54
+ return {
55
+ pg,
56
+ db,
57
+ teardown,
58
+ query
59
+ };
60
+ };
61
+ /**
62
+ * Creates connections with logging for GraphQL queries
63
+ */
64
+ export const getConnectionsObjectWithLogging = async (input, seedAdapters) => {
65
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
66
+ const query = async (opts) => {
67
+ console.log('Executing GraphQL query:', opts.query);
68
+ const result = await baseQuery(opts);
69
+ console.log('GraphQL result:', result);
70
+ return result;
71
+ };
72
+ return {
73
+ pg,
74
+ db,
75
+ teardown,
76
+ query
77
+ };
78
+ };
79
+ /**
80
+ * Creates connections with timing for GraphQL queries
81
+ */
82
+ export const getConnectionsObjectWithTiming = async (input, seedAdapters) => {
83
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
84
+ const query = async (opts) => {
85
+ const start = Date.now();
86
+ const result = await baseQuery(opts);
87
+ const duration = Date.now() - start;
88
+ console.log(`GraphQL query took ${duration}ms`);
89
+ return result;
90
+ };
91
+ return {
92
+ pg,
93
+ db,
94
+ teardown,
95
+ query
96
+ };
97
+ };
98
+ // ============================================================================
99
+ // POSITIONAL QUERY VERSIONS
100
+ // ============================================================================
101
+ /**
102
+ * Creates connections with raw GraphQL responses (positional API)
103
+ */
104
+ export const getConnections = async (input, seedAdapters) => {
105
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
106
+ return {
107
+ pg,
108
+ db,
109
+ teardown,
110
+ query: baseQueryPositional
111
+ };
112
+ };
113
+ /**
114
+ * Creates connections with unwrapped GraphQL responses (positional API, throws on errors)
115
+ */
116
+ export const getConnectionsUnwrapped = async (input, seedAdapters) => {
117
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
118
+ const query = async (query, variables, commit, reqOptions) => unwrap(await baseQueryPositional(query, variables, commit, reqOptions));
119
+ return {
120
+ pg,
121
+ db,
122
+ teardown,
123
+ query
124
+ };
125
+ };
126
+ /**
127
+ * Creates connections with logging for GraphQL queries (positional API)
128
+ */
129
+ export const getConnectionsWithLogging = async (input, seedAdapters) => {
130
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
131
+ const query = async (query, variables, commit, reqOptions) => {
132
+ console.log('Executing positional GraphQL query:', query);
133
+ const result = await baseQueryPositional(query, variables, commit, reqOptions);
134
+ console.log('GraphQL result:', result);
135
+ return result;
136
+ };
137
+ return {
138
+ pg,
139
+ db,
140
+ teardown,
141
+ query
142
+ };
143
+ };
144
+ /**
145
+ * Creates connections with timing for GraphQL queries (positional API)
146
+ */
147
+ export const getConnectionsWithTiming = async (input, seedAdapters) => {
148
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
149
+ const query = async (query, variables, commit, reqOptions) => {
150
+ const start = Date.now();
151
+ const result = await baseQueryPositional(query, variables, commit, reqOptions);
152
+ const duration = Date.now() - start;
153
+ console.log(`Positional GraphQL query took ${duration}ms`);
154
+ return result;
155
+ };
156
+ return {
157
+ pg,
158
+ db,
159
+ teardown,
160
+ query
161
+ };
162
+ };
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './clean';
2
2
  export * from './graphile-test';
3
- export * from './connect';
3
+ export * from './get-connections';
4
+ export * from './types';
4
5
  export { seed } from 'pgsql-test';
@@ -0,0 +1,76 @@
1
+ import type { GetConnectionOpts } from 'pgsql-test';
2
+ import type { PgTestClient } from 'pgsql-test/test-client';
3
+ import type { SeedAdapter } from 'pgsql-test/seed/types';
4
+ import type { GraphQLQueryFnObj, GraphQLQueryFn, GraphQLQueryUnwrappedFnObj, GraphQLQueryUnwrappedFn, GetConnectionsInput } from './types';
5
+ /**
6
+ * Creates connections with raw GraphQL responses
7
+ */
8
+ export declare const getConnectionsObject: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
9
+ pg: PgTestClient;
10
+ db: PgTestClient;
11
+ teardown: () => Promise<void>;
12
+ query: GraphQLQueryFnObj;
13
+ }>;
14
+ /**
15
+ * Creates connections with unwrapped GraphQL responses (throws on errors)
16
+ */
17
+ export declare const getConnectionsObjectUnwrapped: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
18
+ pg: PgTestClient;
19
+ db: PgTestClient;
20
+ teardown: () => Promise<void>;
21
+ query: GraphQLQueryUnwrappedFnObj;
22
+ }>;
23
+ /**
24
+ * Creates connections with logging for GraphQL queries
25
+ */
26
+ export declare const getConnectionsObjectWithLogging: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
27
+ pg: PgTestClient;
28
+ db: PgTestClient;
29
+ teardown: () => Promise<void>;
30
+ query: GraphQLQueryFnObj;
31
+ }>;
32
+ /**
33
+ * Creates connections with timing for GraphQL queries
34
+ */
35
+ export declare const getConnectionsObjectWithTiming: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
36
+ pg: PgTestClient;
37
+ db: PgTestClient;
38
+ teardown: () => Promise<void>;
39
+ query: GraphQLQueryFnObj;
40
+ }>;
41
+ /**
42
+ * Creates connections with raw GraphQL responses (positional API)
43
+ */
44
+ export declare const getConnections: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
45
+ pg: PgTestClient;
46
+ db: PgTestClient;
47
+ teardown: () => Promise<void>;
48
+ query: GraphQLQueryFn;
49
+ }>;
50
+ /**
51
+ * Creates connections with unwrapped GraphQL responses (positional API, throws on errors)
52
+ */
53
+ export declare const getConnectionsUnwrapped: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
54
+ pg: PgTestClient;
55
+ db: PgTestClient;
56
+ teardown: () => Promise<void>;
57
+ query: GraphQLQueryUnwrappedFn;
58
+ }>;
59
+ /**
60
+ * Creates connections with logging for GraphQL queries (positional API)
61
+ */
62
+ export declare const getConnectionsWithLogging: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
63
+ pg: PgTestClient;
64
+ db: PgTestClient;
65
+ teardown: () => Promise<void>;
66
+ query: GraphQLQueryFn;
67
+ }>;
68
+ /**
69
+ * Creates connections with timing for GraphQL queries (positional API)
70
+ */
71
+ export declare const getConnectionsWithTiming: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
72
+ pg: PgTestClient;
73
+ db: PgTestClient;
74
+ teardown: () => Promise<void>;
75
+ query: GraphQLQueryFn;
76
+ }>;
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConnectionsWithTiming = exports.getConnectionsWithLogging = exports.getConnectionsUnwrapped = exports.getConnections = exports.getConnectionsObjectWithTiming = exports.getConnectionsObjectWithLogging = exports.getConnectionsObjectUnwrapped = exports.getConnectionsObject = void 0;
4
+ const pgsql_test_1 = require("pgsql-test");
5
+ const graphile_test_1 = require("./graphile-test");
6
+ // Core unwrapping utility
7
+ const unwrap = (res) => {
8
+ if (res.errors?.length) {
9
+ throw new Error(JSON.stringify(res.errors, null, 2));
10
+ }
11
+ if (!res.data) {
12
+ throw new Error('No data returned from GraphQL query');
13
+ }
14
+ return res.data;
15
+ };
16
+ // Base connection setup - shared across all variants
17
+ const createConnectionsBase = async (input, seedAdapters) => {
18
+ const conn = await (0, pgsql_test_1.getConnections)(input, seedAdapters);
19
+ const { pg, db, teardown: dbTeardown } = conn;
20
+ const gqlContext = (0, graphile_test_1.GraphQLTest)(input, conn);
21
+ await gqlContext.setup();
22
+ const teardown = async () => {
23
+ await gqlContext.teardown();
24
+ await dbTeardown();
25
+ };
26
+ const baseQuery = (opts) => gqlContext.query(opts);
27
+ const baseQueryPositional = (query, variables, commit, reqOptions) => gqlContext.query({ query, variables, commit, reqOptions });
28
+ return {
29
+ pg,
30
+ db,
31
+ teardown,
32
+ baseQuery,
33
+ baseQueryPositional
34
+ };
35
+ };
36
+ // ============================================================================
37
+ // REGULAR QUERY VERSIONS
38
+ // ============================================================================
39
+ /**
40
+ * Creates connections with raw GraphQL responses
41
+ */
42
+ const getConnectionsObject = async (input, seedAdapters) => {
43
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
44
+ return {
45
+ pg,
46
+ db,
47
+ teardown,
48
+ query: baseQuery
49
+ };
50
+ };
51
+ exports.getConnectionsObject = getConnectionsObject;
52
+ /**
53
+ * Creates connections with unwrapped GraphQL responses (throws on errors)
54
+ */
55
+ const getConnectionsObjectUnwrapped = async (input, seedAdapters) => {
56
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
57
+ const query = async (opts) => unwrap(await baseQuery(opts));
58
+ return {
59
+ pg,
60
+ db,
61
+ teardown,
62
+ query
63
+ };
64
+ };
65
+ exports.getConnectionsObjectUnwrapped = getConnectionsObjectUnwrapped;
66
+ /**
67
+ * Creates connections with logging for GraphQL queries
68
+ */
69
+ const getConnectionsObjectWithLogging = async (input, seedAdapters) => {
70
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
71
+ const query = async (opts) => {
72
+ console.log('Executing GraphQL query:', opts.query);
73
+ const result = await baseQuery(opts);
74
+ console.log('GraphQL result:', result);
75
+ return result;
76
+ };
77
+ return {
78
+ pg,
79
+ db,
80
+ teardown,
81
+ query
82
+ };
83
+ };
84
+ exports.getConnectionsObjectWithLogging = getConnectionsObjectWithLogging;
85
+ /**
86
+ * Creates connections with timing for GraphQL queries
87
+ */
88
+ const getConnectionsObjectWithTiming = async (input, seedAdapters) => {
89
+ const { pg, db, teardown, baseQuery } = await createConnectionsBase(input, seedAdapters);
90
+ const query = async (opts) => {
91
+ const start = Date.now();
92
+ const result = await baseQuery(opts);
93
+ const duration = Date.now() - start;
94
+ console.log(`GraphQL query took ${duration}ms`);
95
+ return result;
96
+ };
97
+ return {
98
+ pg,
99
+ db,
100
+ teardown,
101
+ query
102
+ };
103
+ };
104
+ exports.getConnectionsObjectWithTiming = getConnectionsObjectWithTiming;
105
+ // ============================================================================
106
+ // POSITIONAL QUERY VERSIONS
107
+ // ============================================================================
108
+ /**
109
+ * Creates connections with raw GraphQL responses (positional API)
110
+ */
111
+ const getConnections = async (input, seedAdapters) => {
112
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
113
+ return {
114
+ pg,
115
+ db,
116
+ teardown,
117
+ query: baseQueryPositional
118
+ };
119
+ };
120
+ exports.getConnections = getConnections;
121
+ /**
122
+ * Creates connections with unwrapped GraphQL responses (positional API, throws on errors)
123
+ */
124
+ const getConnectionsUnwrapped = async (input, seedAdapters) => {
125
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
126
+ const query = async (query, variables, commit, reqOptions) => unwrap(await baseQueryPositional(query, variables, commit, reqOptions));
127
+ return {
128
+ pg,
129
+ db,
130
+ teardown,
131
+ query
132
+ };
133
+ };
134
+ exports.getConnectionsUnwrapped = getConnectionsUnwrapped;
135
+ /**
136
+ * Creates connections with logging for GraphQL queries (positional API)
137
+ */
138
+ const getConnectionsWithLogging = async (input, seedAdapters) => {
139
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
140
+ const query = async (query, variables, commit, reqOptions) => {
141
+ console.log('Executing positional GraphQL query:', query);
142
+ const result = await baseQueryPositional(query, variables, commit, reqOptions);
143
+ console.log('GraphQL result:', result);
144
+ return result;
145
+ };
146
+ return {
147
+ pg,
148
+ db,
149
+ teardown,
150
+ query
151
+ };
152
+ };
153
+ exports.getConnectionsWithLogging = getConnectionsWithLogging;
154
+ /**
155
+ * Creates connections with timing for GraphQL queries (positional API)
156
+ */
157
+ const getConnectionsWithTiming = async (input, seedAdapters) => {
158
+ const { pg, db, teardown, baseQueryPositional } = await createConnectionsBase(input, seedAdapters);
159
+ const query = async (query, variables, commit, reqOptions) => {
160
+ const start = Date.now();
161
+ const result = await baseQueryPositional(query, variables, commit, reqOptions);
162
+ const duration = Date.now() - start;
163
+ console.log(`Positional GraphQL query took ${duration}ms`);
164
+ return result;
165
+ };
166
+ return {
167
+ pg,
168
+ db,
169
+ teardown,
170
+ query
171
+ };
172
+ };
173
+ exports.getConnectionsWithTiming = getConnectionsWithTiming;
@@ -1,4 +1,4 @@
1
1
  import type { GraphQLTestContext } from './types';
2
- import { GetConnectionsInput } from './connect';
2
+ import { GetConnectionsInput } from './types';
3
3
  import { GetConnectionOpts, GetConnectionResult } from 'pgsql-test';
4
4
  export declare const GraphQLTest: (input: GetConnectionsInput & GetConnectionOpts, conn: GetConnectionResult) => GraphQLTestContext;
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './clean';
2
2
  export * from './graphile-test';
3
- export * from './connect';
3
+ export * from './get-connections';
4
+ export * from './types';
4
5
  export { seed } from 'pgsql-test';
package/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.seed = void 0;
18
18
  __exportStar(require("./clean"), exports);
19
19
  __exportStar(require("./graphile-test"), exports);
20
- __exportStar(require("./connect"), exports);
20
+ __exportStar(require("./get-connections"), exports);
21
+ __exportStar(require("./types"), exports);
21
22
  var pgsql_test_1 = require("pgsql-test");
22
23
  Object.defineProperty(exports, "seed", { enumerable: true, get: function () { return pgsql_test_1.seed; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-test",
3
- "version": "2.1.9",
3
+ "version": "2.1.11",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "PostGraphile Testing",
6
6
  "main": "index.js",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@launchql/types": "^2.1.6",
38
- "graphile-settings": "^2.1.9",
38
+ "graphile-settings": "^2.1.10",
39
39
  "graphql": "^15.5.2",
40
40
  "mock-req": "^0.2.0",
41
41
  "pg": "^8.16.0",
@@ -48,5 +48,5 @@
48
48
  "launchql",
49
49
  "test"
50
50
  ],
51
- "gitHead": "a763e6c30aa6af1f5c744fa7463fc557def6edb3"
51
+ "gitHead": "dd29fa9997eb53550eaabd017e2f1664f2eb7267"
52
52
  }
package/types.d.ts CHANGED
@@ -1,12 +1,31 @@
1
- import { DocumentNode, ExecutionResult } from 'graphql';
2
- export interface GraphQLQueryOptions {
1
+ import { DocumentNode, GraphQLError } from 'graphql';
2
+ export interface GraphQLQueryOptions<TVariables = Record<string, any>> {
3
3
  query: string | DocumentNode;
4
- variables?: Record<string, any>;
4
+ variables?: TVariables;
5
5
  commit?: boolean;
6
6
  reqOptions?: Record<string, any>;
7
7
  }
8
8
  export interface GraphQLTestContext {
9
9
  setup: () => Promise<void>;
10
10
  teardown: () => Promise<void>;
11
- query: <T = ExecutionResult>(opts: GraphQLQueryOptions) => Promise<T>;
11
+ query: <TResult = any, TVariables = Record<string, any>>(opts: GraphQLQueryOptions<TVariables>) => Promise<TResult>;
12
12
  }
13
+ export interface GetConnectionsInput {
14
+ useRoot?: boolean;
15
+ schemas: string[];
16
+ authRole?: string;
17
+ }
18
+ export interface GraphQLQueryOptions<TVariables = Record<string, any>> {
19
+ query: string | DocumentNode;
20
+ variables?: TVariables;
21
+ commit?: boolean;
22
+ reqOptions?: Record<string, any>;
23
+ }
24
+ export interface GraphQLResponse<T> {
25
+ data?: T;
26
+ errors?: readonly GraphQLError[];
27
+ }
28
+ export type GraphQLQueryFnObj = <TResult = any, TVariables = Record<string, any>>(opts: GraphQLQueryOptions<TVariables>) => Promise<GraphQLResponse<TResult>>;
29
+ export type GraphQLQueryFn = <TResult = any, TVariables = Record<string, any>>(query: string | DocumentNode, variables?: TVariables, commit?: boolean, reqOptions?: Record<string, any>) => Promise<GraphQLResponse<TResult>>;
30
+ export type GraphQLQueryUnwrappedFnObj = <TResult = any, TVariables = Record<string, any>>(opts: GraphQLQueryOptions<TVariables>) => Promise<TResult>;
31
+ export type GraphQLQueryUnwrappedFn = <TResult = any, TVariables = Record<string, any>>(query: string | DocumentNode, variables?: TVariables, commit?: boolean, reqOptions?: Record<string, any>) => Promise<TResult>;
package/connect.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import type { GetConnectionOpts } from 'pgsql-test';
2
- import type { PgTestClient } from 'pgsql-test/test-client';
3
- import type { SeedAdapter } from 'pgsql-test/seed/types';
4
- import type { DocumentNode, ExecutionResult } from 'graphql';
5
- export type GraphQLQueryFn<T = ExecutionResult> = (query: string | DocumentNode, variables?: Record<string, any>, commit?: boolean, reqOptions?: Record<string, any>) => Promise<T>;
6
- export interface GetConnectionsInput {
7
- useRoot?: boolean;
8
- schemas: string[];
9
- authRole?: string;
10
- }
11
- /**
12
- * Combines PostgreSQL test setup with GraphQL test context
13
- */
14
- export declare const getConnections: (input: GetConnectionsInput & GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<{
15
- pg: PgTestClient;
16
- db: PgTestClient;
17
- teardown: () => Promise<void>;
18
- query: GraphQLQueryFn;
19
- }>;
package/connect.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConnections = void 0;
4
- const pgsql_test_1 = require("pgsql-test");
5
- const graphile_test_1 = require("./graphile-test");
6
- /**
7
- * Combines PostgreSQL test setup with GraphQL test context
8
- */
9
- const getConnections = async (input, seedAdapters) => {
10
- const conn = await (0, pgsql_test_1.getConnections)(input, seedAdapters);
11
- const { pg, db, teardown: dbTeardown } = conn;
12
- const gqlContext = (0, graphile_test_1.GraphQLTest)(input, conn);
13
- await gqlContext.setup();
14
- const teardown = async () => {
15
- await gqlContext.teardown();
16
- await dbTeardown();
17
- };
18
- const query = (query, variables, commit, reqOptions) => gqlContext.query({ query, variables, commit, reqOptions });
19
- return {
20
- pg,
21
- db,
22
- teardown,
23
- query
24
- };
25
- };
26
- exports.getConnections = getConnections;
package/esm/connect.js DELETED
@@ -1,22 +0,0 @@
1
- import { getConnections as getPgConnections } from 'pgsql-test';
2
- import { GraphQLTest } from './graphile-test';
3
- /**
4
- * Combines PostgreSQL test setup with GraphQL test context
5
- */
6
- export const getConnections = async (input, seedAdapters) => {
7
- const conn = await getPgConnections(input, seedAdapters);
8
- const { pg, db, teardown: dbTeardown } = conn;
9
- const gqlContext = GraphQLTest(input, conn);
10
- await gqlContext.setup();
11
- const teardown = async () => {
12
- await gqlContext.teardown();
13
- await dbTeardown();
14
- };
15
- const query = (query, variables, commit, reqOptions) => gqlContext.query({ query, variables, commit, reqOptions });
16
- return {
17
- pg,
18
- db,
19
- teardown,
20
- query
21
- };
22
- };