Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Operation name graphql #5418

Merged
merged 4 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs-src/docs/replication-graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const pullQueryBuilder = (checkpoint, limit) => {
}`;
return {
query,
operationName: 'PullHuman',
variables: {
checkpoint,
limit
Expand Down Expand Up @@ -229,6 +230,7 @@ const pushQueryBuilder = rows => {
};
return {
query,
operationName: 'PushHuman',
variables
};
};
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/replication-graphql/query-builder-from-rx-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ 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);

const checkpointInputName = ucCollectionName + 'Input' + prefixes.checkpoint;

const builder: RxGraphQLReplicationPullQueryBuilder<any> = (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' +
Expand All @@ -41,6 +42,7 @@ export function pullQueryBuilderFromRxSchema(
'}';
return {
query,
operationName,
variables: {
checkpoint,
limit
Expand All @@ -60,12 +62,13 @@ export function pullStreamBuilderFromRxSchema(
const prefixes: Prefixes = input.prefixes as any;

const ucCollectionName = ucfirst(collectionName);
const queryName = prefixes.stream + ucCollectionName;
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' +
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' +
Expand Down Expand Up @@ -96,15 +99,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' +
Expand Down Expand Up @@ -137,6 +140,7 @@ export function pushQueryBuilderFromRxSchema(
};
return {
query,
operationName,
variables
};
};
Expand Down
1 change: 1 addition & 0 deletions src/types/plugins/replication-graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

export interface RxGraphQLReplicationQueryBuilderResponseObject {
query: string;
operationName?: string;
variables: any;
}

Expand Down
1 change: 1 addition & 0 deletions test/helper/graphql-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ export async function spawn(
writeHumans(writeRows: $writeRows) { id }
}
`,
operationName: 'CreateHumans',
variables: {
writeRows: [row]
}
Expand Down
25 changes: 25 additions & 0 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('replication-graphql.test.ts', () => {
};
return Promise.resolve({
query,
operationName: 'FeedForRxDBReplication',
variables
});
};
Expand All @@ -122,6 +123,7 @@ describe('replication-graphql.test.ts', () => {
}`;
return {
query,
operationName: 'onHumanChanged',
variables: {
headers
}
Expand All @@ -147,6 +149,7 @@ describe('replication-graphql.test.ts', () => {
};
return Promise.resolve({
query,
operationName: 'CreateHumans',
variables
});
};
Expand Down Expand Up @@ -205,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();
Expand Down Expand Up @@ -508,6 +532,7 @@ describe('replication-graphql.test.ts', () => {

return {
query,
operationName: 'FeedForRxDBReplication',
variables
};
};
Expand Down