From 896f9b44661acaed78fe25c21ff7020efd1848b9 Mon Sep 17 00:00:00 2001 From: jwallet Date: Thu, 21 Dec 2023 17:25:35 -0500 Subject: [PATCH 1/3] added GraphQL operationName param request --- docs-src/docs/replication-graphql.md | 3 +++ .../query-builder-from-rx-schema.ts | 16 +++++++++++----- src/types/plugins/replication-graphql.d.ts | 1 + test/helper/graphql-config.ts | 1 + test/helper/graphql-server.ts | 1 + test/unit/replication-graphql.test.ts | 5 +++++ 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs-src/docs/replication-graphql.md b/docs-src/docs/replication-graphql.md index 5e4ffa40dc6..3ac77b64267 100644 --- a/docs-src/docs/replication-graphql.md +++ b/docs-src/docs/replication-graphql.md @@ -157,6 +157,7 @@ const pullQueryBuilder = (checkpoint, limit) => { }`; return { query, + operationName: 'PullHuman', variables: { checkpoint, limit @@ -229,6 +230,7 @@ const pushQueryBuilder = rows => { }; return { query, + operationName: 'PushHuman', variables }; }; @@ -294,6 +296,7 @@ const pullStreamQueryBuilder = (headers) => { }`; return { query, + operationName: 'onStream', variables: { headers } diff --git a/src/plugins/replication-graphql/query-builder-from-rx-schema.ts b/src/plugins/replication-graphql/query-builder-from-rx-schema.ts index 9e3d762c8ee..faca31fe3fa 100644 --- a/src/plugins/replication-graphql/query-builder-from-rx-schema.ts +++ b/src/plugins/replication-graphql/query-builder-from-rx-schema.ts @@ -22,6 +22,7 @@ export function pullQueryBuilderFromRxSchema( const ucCollectionName = ucfirst(collectionName); const queryName = prefixes.pull + ucCollectionName; + const operationName = ucfirst(queryName); const outputFields = Object.keys(schema.properties).filter(k => !(input.ignoreOutputKeys as string[]).includes(k)); // outputFields.push(input.deletedField); @@ -29,7 +30,7 @@ export function pullQueryBuilderFromRxSchema( const checkpointInputName = ucCollectionName + 'Input' + prefixes.checkpoint; const builder: RxGraphQLReplicationPullQueryBuilder = (checkpoint: any, limit: number) => { - const query = 'query ' + ucfirst(queryName) + '($checkpoint: ' + checkpointInputName + ', $limit: Int!) {\n' + + const query = 'query ' + operationName + '($checkpoint: ' + checkpointInputName + ', $limit: Int!) {\n' + SPACING + SPACING + queryName + '(checkpoint: $checkpoint, limit: $limit) {\n' + SPACING + SPACING + SPACING + 'documents {\n' + SPACING + SPACING + SPACING + SPACING + outputFields.join('\n' + SPACING + SPACING + SPACING + SPACING) + '\n' + @@ -41,6 +42,7 @@ export function pullQueryBuilderFromRxSchema( '}'; return { query, + operationName, variables: { checkpoint, limit @@ -60,12 +62,14 @@ export function pullStreamBuilderFromRxSchema( const prefixes: Prefixes = input.prefixes as any; const ucCollectionName = ucfirst(collectionName); + const queryName = prefixes.stream + ucCollectionName; + const operationName = ucfirst('on' + ucfirst(queryName)); const outputFields = Object.keys(schema.properties).filter(k => !(input.ignoreOutputKeys as string[]).includes(k)); const headersName = ucCollectionName + 'Input' + prefixes.headers; - const query = 'subscription on' + ucfirst(ensureNotFalsy(prefixes.stream)) + '($headers: ' + headersName + ') {\n' + - SPACING + prefixes.stream + ucCollectionName + '(headers: $headers) {\n' + + const query = 'subscription ' + operationName + '($headers: ' + headersName + ') {\n' + + SPACING + queryName + '(headers: $headers) {\n' + SPACING + SPACING + SPACING + 'documents {\n' + SPACING + SPACING + SPACING + SPACING + outputFields.join('\n' + SPACING + SPACING + SPACING + SPACING) + '\n' + SPACING + SPACING + SPACING + '}\n' + @@ -78,6 +82,7 @@ export function pullStreamBuilderFromRxSchema( const builder: RxGraphQLReplicationPullStreamQueryBuilder = (headers: any) => { return { query, + operationName, variables: { headers } @@ -96,15 +101,15 @@ export function pushQueryBuilderFromRxSchema( const ucCollectionName = ucfirst(collectionName); const queryName = prefixes.push + ucCollectionName; + const operationName = ucfirst(queryName); const variableName = collectionName + prefixes.pushRow; - const returnFields: string[] = Object.keys(input.schema.properties); const builder: RxGraphQLReplicationPushQueryBuilder = (pushRows) => { const query = '' + - 'mutation ' + prefixes.push + ucCollectionName + '($' + variableName + ': [' + ucCollectionName + 'Input' + prefixes.pushRow + '!]) {\n' + + 'mutation ' + operationName + '($' + variableName + ': [' + ucCollectionName + 'Input' + prefixes.pushRow + '!]) {\n' + SPACING + queryName + '(' + variableName + ': $' + variableName + ') {\n' + SPACING + SPACING + returnFields.join(',\n' + SPACING + SPACING) + '\n' + SPACING + '}\n' + @@ -137,6 +142,7 @@ export function pushQueryBuilderFromRxSchema( }; return { query, + operationName, variables }; }; diff --git a/src/types/plugins/replication-graphql.d.ts b/src/types/plugins/replication-graphql.d.ts index 562a820ef56..4a19ff9e31d 100644 --- a/src/types/plugins/replication-graphql.d.ts +++ b/src/types/plugins/replication-graphql.d.ts @@ -7,6 +7,7 @@ import { export interface RxGraphQLReplicationQueryBuilderResponseObject { query: string; + operationName?: string; variables: any; } diff --git a/test/helper/graphql-config.ts b/test/helper/graphql-config.ts index f5464705935..b9fa1d828ff 100644 --- a/test/helper/graphql-config.ts +++ b/test/helper/graphql-config.ts @@ -16,6 +16,7 @@ export async function getDocsOnServer( deleted } }`, + operationName: 'getAll', variables: {} }); return response.data.getAll; diff --git a/test/helper/graphql-server.ts b/test/helper/graphql-server.ts index a5c07804efa..46cf3831b56 100644 --- a/test/helper/graphql-server.ts +++ b/test/helper/graphql-server.ts @@ -356,6 +356,7 @@ export async function spawn( writeHumans(writeRows: $writeRows) { id } } `, + operationName: 'CreateHumans', variables: { writeRows: [row] } diff --git a/test/unit/replication-graphql.test.ts b/test/unit/replication-graphql.test.ts index 1cd7461a264..f4eac17229e 100644 --- a/test/unit/replication-graphql.test.ts +++ b/test/unit/replication-graphql.test.ts @@ -101,6 +101,7 @@ describe('replication-graphql.test.ts', () => { }; return Promise.resolve({ query, + operationName: 'FeedForRxDBReplication', variables }); }; @@ -122,6 +123,7 @@ describe('replication-graphql.test.ts', () => { }`; return { query, + operationName: 'onHumanChanged', variables: { headers } @@ -147,6 +149,7 @@ describe('replication-graphql.test.ts', () => { }; return Promise.resolve({ query, + operationName: 'CreateHumans', variables }); }; @@ -195,6 +198,7 @@ describe('replication-graphql.test.ts', () => { }, { query: '{ info }', + operationName: 'info', variables: {} } ); @@ -508,6 +512,7 @@ describe('replication-graphql.test.ts', () => { return { query, + operationName: 'FeedForRxDBReplication', variables }; }; From addf0f8080893d6aaa649feb2d618cd1e7807102 Mon Sep 17 00:00:00 2001 From: jwallet Date: Thu, 21 Dec 2023 18:27:15 -0500 Subject: [PATCH 2/3] test --- test/helper/graphql-config.ts | 1 - test/unit/replication-graphql.test.ts | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/helper/graphql-config.ts b/test/helper/graphql-config.ts index b9fa1d828ff..f5464705935 100644 --- a/test/helper/graphql-config.ts +++ b/test/helper/graphql-config.ts @@ -16,7 +16,6 @@ export async function getDocsOnServer( deleted } }`, - operationName: 'getAll', variables: {} }); return response.data.getAll; diff --git a/test/unit/replication-graphql.test.ts b/test/unit/replication-graphql.test.ts index f4eac17229e..0198cd5bb05 100644 --- a/test/unit/replication-graphql.test.ts +++ b/test/unit/replication-graphql.test.ts @@ -198,7 +198,6 @@ describe('replication-graphql.test.ts', () => { }, { query: '{ info }', - operationName: 'info', variables: {} } ); @@ -209,6 +208,27 @@ describe('replication-graphql.test.ts', () => { assert.strictEqual(res.data.info, 1); server.close(); }); + it('spawn and throw an unknown operation name', async () => { + const server = await SpawnServer.spawn(); + try { + await graphQLRequest( + ensureNotFalsy(server.url.http), + { + headers: {}, + credentials: undefined + }, + { + query: '{ info }', + operationName: 'info', + variables: {} + } + ); + } catch (err: any) { + assert.ok(err.message.includes('Unknown operation named "info".')); + } + + server.close(); + }); it('server.setDocument()', async () => { const server = await SpawnServer.spawn(); const doc = getTestData(1).pop(); From fe1b6521ae0006ca822e4d6c3d2ed45dbbcc9ffe Mon Sep 17 00:00:00 2001 From: jwallet Date: Fri, 22 Dec 2023 09:11:56 -0500 Subject: [PATCH 3/3] prevent breaking change --- docs-src/docs/replication-graphql.md | 1 - .../replication-graphql/query-builder-from-rx-schema.ts | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/docs-src/docs/replication-graphql.md b/docs-src/docs/replication-graphql.md index 3ac77b64267..b4199db160c 100644 --- a/docs-src/docs/replication-graphql.md +++ b/docs-src/docs/replication-graphql.md @@ -296,7 +296,6 @@ const pullStreamQueryBuilder = (headers) => { }`; return { query, - operationName: 'onStream', variables: { headers } diff --git a/src/plugins/replication-graphql/query-builder-from-rx-schema.ts b/src/plugins/replication-graphql/query-builder-from-rx-schema.ts index faca31fe3fa..2414a857840 100644 --- a/src/plugins/replication-graphql/query-builder-from-rx-schema.ts +++ b/src/plugins/replication-graphql/query-builder-from-rx-schema.ts @@ -63,12 +63,11 @@ export function pullStreamBuilderFromRxSchema( const ucCollectionName = ucfirst(collectionName); const queryName = prefixes.stream + ucCollectionName; - const operationName = ucfirst('on' + ucfirst(queryName)); const outputFields = Object.keys(schema.properties).filter(k => !(input.ignoreOutputKeys as string[]).includes(k)); const headersName = ucCollectionName + 'Input' + prefixes.headers; - const query = 'subscription ' + operationName + '($headers: ' + headersName + ') {\n' + + const query = 'subscription on' + ucfirst(ensureNotFalsy(prefixes.stream)) + '($headers: ' + headersName + ') {\n' + SPACING + queryName + '(headers: $headers) {\n' + SPACING + SPACING + SPACING + 'documents {\n' + SPACING + SPACING + SPACING + SPACING + outputFields.join('\n' + SPACING + SPACING + SPACING + SPACING) + '\n' + @@ -82,7 +81,6 @@ export function pullStreamBuilderFromRxSchema( const builder: RxGraphQLReplicationPullStreamQueryBuilder = (headers: any) => { return { query, - operationName, variables: { headers }