Skip to content

Commit

Permalink
Merge branch '1.13' into 1
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Mar 25, 2023
2 parents c329541 + 3edf6b5 commit 14ee62c
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 45 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
Expand Up @@ -12,7 +12,7 @@ const getQueryType = ({ queryType }) => queryType;

const buildReadQuery = (tag = defaultTag) => (
tag`${getQueryType} ${getOperationName}${getVariables} {
${getQueryName}(${getParams}) {
${getQueryName}${getParams} {
${getFields}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { defaultTag } from './tags';
import { getSingularName, getFields, getFragments } from './helpers';
import { getSingularName, getFields, getFragments, getMutationParams, getMutationVariables } from './helpers';

const buildCreateMutation = (tag = defaultTag) => (
tag`mutation Create${getSingularName}(
$Input:${getSingularName}CreateInputType!
) {
create${getSingularName}(
Input: $Input
) {
tag`mutation Create${getSingularName}${getMutationVariables} {
create${getSingularName}${getMutationParams} {
${getFields}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultTag } from './tags';
import { getSingularName } from './helpers';
import { getSingularName, getIdOnlyVariables, getIdOnlyParams } from './helpers';

const buildDeleteMutation = (tag = defaultTag) => (
tag`mutation Delete${getSingularName}($IDs:[ID]!) {
delete${getSingularName}(IDs: $IDs)
tag`mutation Delete${getSingularName}${getIdOnlyVariables} {
delete${getSingularName}${getIdOnlyParams}
}`
);

Expand Down
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, getIdOnlyVariables, getFields, getFragments, getIdOnlyParams } from './helpers';

const buildReadOneQuery = (tag = defaultTag) => (
tag`query ReadOne${getSingularName}($ID: ID!) {
readOne${getSingularName}(ID: $ID) {
tag`query ReadOne${getSingularName}${getIdOnlyVariables} {
readOne${getSingularName}${getIdOnlyParams} {
${getFields}
}
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/dependency-injection/graphql/buildReadQuery.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultTag } from './tags';
import { getPluralName, getVariables, getRootParams, getFields, getFragments } from './helpers';
import { getPluralName, getVariables, getParams, getFields, getFragments } from './helpers';

const buildReadQuery = (tag = defaultTag) => (
tag`query Read${getPluralName}${getVariables} {
read${getPluralName}${getRootParams} {
read${getPluralName}${getParams} {
${getFields}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { defaultTag } from './tags';
import { getSingularName, getVariables, getParams, getFields, getFragments } from './helpers';
import { getSingularName, getMutationVariables, getMutationParams, getFields, getFragments } from './helpers';

const buildUpdateMutation = (tag = defaultTag) => (
tag`mutation Update${getSingularName}(
$Input:${getSingularName}UpdateInputType!
${getVariables}
) {
update${getSingularName}(
Input: $Input
${getParams}
) {
tag`mutation Update${getSingularName}${getMutationVariables} {
update${getSingularName}${getMutationParams} {
${getFields}
}
}
Expand Down
139 changes: 129 additions & 10 deletions client/src/lib/dependency-injection/graphql/helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { CREATE, DELETE, READ_ONE, UPDATE } from './templates';

export const ROOT_FIELD = 'root';

// used to help generate the parameters and variables
const paginationFields = {
limit: 'Int',
offset: 'Int',
};

// just outright is the parameters
const paginationParams = {
limit: 'limit',
offset: 'offset',
};

const paginateFields = (fields) => (
`edges { node { ${fields.join(' ')} } } pageInfo { totalCount }`
);
Expand All @@ -26,20 +35,14 @@ export const getVariables = ({ params, pagination = true }) => {

export const getParams = ({ params, pagination = true }) => {
const items = (pagination) ? { ...params, ...paginationFields } : params;
const paramList = Object.entries(items)
.map(([paramName, varName]) => (
`${paramName}: $${varName}`
const paramList = Object.keys(items)
.map((paramName) => (
`${paramName}: $${paramName}`
));

return paramList.length ? `(${paramList.join(', ')})` : '';
};

export const getRootParams = ({ args, pagination = true }) => {
const fieldParams = args[ROOT_FIELD] || {};

return getParams({ params: fieldParams, pagination });
};

export const getFields = ({ args, fields, pagination = true }, stack = [ROOT_FIELD]) => {
const strings = fields.map((field, i) => {
if (Array.isArray(field)) {
Expand All @@ -55,7 +58,14 @@ export const getFields = ({ args, fields, pagination = true }, stack = [ROOT_FIE
const key = path.join('/');
const fieldParams = args[key] || {};

const str = `${field}${getParams({ params: fieldParams, pagination: false })}`;
const items = (pagination) ? { ...fieldParams, ...paginationParams } : fieldParams;
const paramList = Object.entries(items)
.map(([paramName, varName]) => (
`${paramName}: $${varName}`
));
const paramOutput = paramList.length ? `(${paramList.join(', ')})` : '';

const str = `${field}${paramOutput}`;

return str;
});
Expand All @@ -76,3 +86,112 @@ export const getFragments = ({ availableFragments, fragments = [] }) => (
: capturedFragments
), '')
);

function getMatchingParamKey(matchWith, params) {
return Object.keys(params).find((paramName) => paramName.toLowerCase() === matchWith);
}

function getInputVariable({ templateName, singularName, params }) {
const inputKey = getMatchingParamKey('input', params);
if (inputKey) {
return `$${inputKey}: ${params[inputKey]}`;
}
// Graphql v3 fallbacks
switch (templateName) {
case CREATE:
return `$Input: ${getSingularName({ singularName })}CreateInputType!`;
case UPDATE:
return `$Input: ${getSingularName({ singularName })}UpdateInputType!`;
default:
throw new Error('Template is not a mutation - no input variable is required.');
}
}

function normaliseMutationParamsAndVars(paramsOrVars, dedup) {
return paramsOrVars.replace(dedup, '').replace(/(^\(+|\)+$)/mg, '');
}

export function getMutationVariables(obj) {
const inputVar = getInputVariable(obj);
switch (obj.templateName) {
case CREATE:
return `(${inputVar})`;
case UPDATE:
return `(
${inputVar}
${normaliseMutationParamsAndVars(getVariables(obj), inputVar)}
)`;
default:
throw new Error('Template is not a mutation - use getVariables() instead.');
}
}

export function getMutationParams(obj) {
const { templateName, params } = obj;
let inputKey = getMatchingParamKey('input', params);
if (!inputKey) {
// Graphql v3 fallback
inputKey = 'Input';
}
const inputParam = `${inputKey}: $${inputKey}`;

switch (templateName) {
case CREATE:
return `(${inputParam})`;
case UPDATE:
return `(
${inputParam}
${normaliseMutationParamsAndVars(getParams(obj), inputParam)}
)`;
default:
throw new Error('Template is not a mutation - use getParams() instead.');
}
}

export function getIdOnlyVariables({ templateName, params }) {
let key;
switch (templateName) {
case DELETE:
// Let developer declare casing (e.g. "Ids" or "ids")
key = getMatchingParamKey('ids', params);
if (key) {
return `($${key}: ${params[key]})`;
}
// Graphql v3 fallback
return '($IDs:[ID]!)';
case READ_ONE:
// Let developer declare casing (e.g. "Id" or "id")
key = getMatchingParamKey('id', params);
if (key) {
return `($${key}: ${params[key]})`;
}
// Graphql v3 fallback
return '($ID: ID!)';
default:
throw new Error('Unexpected template type.');
}
}

export function getIdOnlyParams({ templateName, params }) {
let key;
switch (templateName) {
case DELETE:
// Let developer declare casing (e.g. "Ids" or "ids")
key = getMatchingParamKey('ids', params);
if (key) {
return `(${key}: $${key})`;
}
// Graphql v3 fallback
return '(IDs: $IDs)';
case READ_ONE:
// Let developer declare casing (e.g. "Id" or "id")
key = getMatchingParamKey('id', params);
if (key) {
return `(${key}: $${key})`;
}
// Graphql v3 fallback
return '(ID: $ID)';
default:
throw new Error('Unexpected template type.');
}
}
7 changes: 1 addition & 6 deletions client/src/lib/dependency-injection/injectGraphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ const injectGraphql = (key, context) => (DataHandler) => {
const apolloHOC = graphqlContainer.getApolloHOC();
target = apolloHOC(DataHandler);
error = false;
} catch (e) {
} finally {
this.setState({ target, error });

// re-throw the error, as we do not want to silence it in the console
throw e;
}

this.setState({ target, error });
});
}

Expand Down
Loading

0 comments on commit 14ee62c

Please sign in to comment.