Skip to content

Commit

Permalink
FIX Dynamically get correct input argument type for mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Apr 12, 2023
1 parent 224196e commit 171f652
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultTag } from './tags';
import { getSingularName, getFields, getFragments } from './helpers';
import { getSingularName, getCreateMutationType, getFields, getFragments } from './helpers';

const buildCreateMutation = (tag = defaultTag) => (
tag`mutation Create${getSingularName}(
$input:${getSingularName}CreateInputType!
$input:${getCreateMutationType}!
) {
create${getSingularName}(
input: $input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultTag } from './tags';
import { getSingularName, getVariables, getParams, getFields, getFragments } from './helpers';
import { getSingularName, getUpdateMutationType, getVariables, getParams, getFields, getFragments } from './helpers';

const buildUpdateMutation = (tag = defaultTag) => (
tag`mutation Update${getSingularName}(
$input:${getSingularName}UpdateInputType!
$input:${getUpdateMutationType}!
${getVariables}
) {
update${getSingularName}(
Expand Down
12 changes: 12 additions & 0 deletions client/src/lib/dependency-injection/graphql/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ export const getFragments = ({ availableFragments, fragments = [] }) => (
: capturedFragments
), '')
);

function isLegacy() {
return !!document.body.getAttribute('data-graphql-legacy');
}

export const getCreateMutationType = ({ singularName }) => (
isLegacy() ? `${singularName}CreateInputType` : `Create${singularName}Input`
);

export const getUpdateMutationType = ({ singularName }) => (
isLegacy() ? `${singularName}UpdateInputType` : `Update${singularName}Input`
);
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,44 @@ describe('Dynamic graphql injection', () => {

const AST = fetchManager('MyTestUpdateQuery').getGraphqlAST();

expect(AST.kind).toBe('Document');
const compiledQuery = AST.loc.source.body;
expect(compiledQuery).toMatch(/\s*mutation\s+UpdateDino\(\s*\$input:\s*UpdateDinoInput!\s*\)\s+{\s+updateDino\(\s*input:\s*\$input\s*\)\s+{\s+Teeth Plates\s+}\s+}\s*$/);
});

it('Allows customisation of graphql3 UPDATE queries', () => {
document.body.setAttribute('data-graphql-legacy', 1);

const { UPDATE } = graphqlTemplates;
const query = {
apolloConfig: {
},
templateName: UPDATE,
singularName: 'Dino',
pagination: false,
params: {},
fields: [
'Teeth',
],
};

Injector.test.register('MyTestUpdateQuery', query);
Injector.transform(
'test-transform',
(updater) => {
updater.test('MyTestUpdateQuery', (manager) => {
manager.addField('Plates');
});
}
);
Injector.load();

const AST = fetchManager('MyTestUpdateQuery').getGraphqlAST();

expect(AST.kind).toBe('Document');
const compiledQuery = AST.loc.source.body;
expect(compiledQuery).toMatch(/\s*mutation\s+UpdateDino\(\s*\$input:\s*DinoUpdateInputType!\s*\)\s+{\s+updateDino\(\s*input:\s*\$input\s*\)\s+{\s+Teeth Plates\s+}\s+}\s*$/);
document.body.removeAttribute('data-graphql-legacy');
});

it('Allows customisation of DELETE queries', () => {
Expand Down Expand Up @@ -222,9 +257,44 @@ describe('Dynamic graphql injection', () => {

const AST = fetchManager('MyTestCreateQuery').getGraphqlAST();

expect(AST.kind).toBe('Document');
const compiledQuery = AST.loc.source.body;
expect(compiledQuery).toMatch(/\s*mutation\s+CreateDino\(\s*\$input:\s*CreateDinoInput!\s*\)\s+{\s+createDino\(\s*input:\s*\$input\s*\)\s+{\s+Club Claws\s+}\s+}\s*$/);
});

it('Allows customisation of graphql3 CREATE queries', () => {
document.body.setAttribute('data-graphql-legacy', 1);

const { CREATE } = graphqlTemplates;
const query = {
apolloConfig: {
},
templateName: CREATE,
singularName: 'Dino',
pagination: false,
params: {},
fields: [
'Club',
],
};

Injector.test.register('MyTestCreateQuery', query);
Injector.transform(
'test-transform',
(updater) => {
updater.test('MyTestCreateQuery', (manager) => {
manager.addField('Claws');
});
}
);
Injector.load();

const AST = fetchManager('MyTestCreateQuery').getGraphqlAST();

expect(AST.kind).toBe('Document');
const compiledQuery = AST.loc.source.body;
expect(compiledQuery).toMatch(/\s*mutation\s+CreateDino\(\s*\$input:\s*DinoCreateInputType!\s*\)\s+{\s+createDino\(\s*input:\s*\$input\s*\)\s+{\s+Club Claws\s+}\s+}\s*$/);
document.body.removeAttribute('data-graphql-legacy');
});
});

0 comments on commit 171f652

Please sign in to comment.