Skip to content

Commit

Permalink
fix: remove recurseDataGeneratorsForField from many places (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Oct 15, 2018
1 parent 128b0d5 commit 1f3328e
Show file tree
Hide file tree
Showing 17 changed files with 1,553 additions and 289 deletions.
2 changes: 2 additions & 0 deletions packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
PgAttribute,
PgConstraint,
} from "./PgIntrospectionPlugin";
import pgField from "./pgField";

import queryFromResolveData from "../queryFromResolveData";
import addStartEndCursor from "./addStartEndCursor";
Expand Down Expand Up @@ -212,6 +213,7 @@ export default (function PgBasicsPlugin(
pgParseIdentifier: parseIdentifier,
pgViaTemporaryTable: viaTemporaryTable,
describePgEntity,
pgField,
sqlCommentByAddingTags: (entity, tagsToAdd) => {
// NOTE: this function is NOT intended to be SQL safe; it's for
// displaying in error messages. Nonetheless if you find issues with
Expand Down
22 changes: 14 additions & 8 deletions packages/graphile-build-pg/src/plugins/PgMutationCreatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default (function PgMutationCreatePlugin(
pgViaTemporaryTable: viaTemporaryTable,
describePgEntity,
sqlCommentByAddingTags,
pgField,
} = build;
const {
scope: { isRootMutation },
Expand Down Expand Up @@ -110,22 +111,27 @@ export default (function PgMutationCreatePlugin(
{
name: inflection.createPayloadType(table),
description: `The output of our create \`${tableTypeName}\` mutation.`,
fields: ({ recurseDataGeneratorsForField }) => {
fields: ({ fieldWithHooks }) => {
const tableName = inflection.tableFieldName(table);
recurseDataGeneratorsForField(tableName);
return {
clientMutationId: {
description:
"The exact same `clientMutationId` that was provided in the mutation input, unchanged and unused. May be used by a client to track mutations.",
type: GraphQLString,
},
[tableName]: {
description: `The \`${tableTypeName}\` that was created by this mutation.`,
type: Table,
resolve(data) {
return data.data;
[tableName]: pgField(
build,
fieldWithHooks,
tableName,
{
description: `The \`${tableTypeName}\` that was created by this mutation.`,
type: Table,
},
},
{
isPgCreatePayloadResultField: true,
pgFieldIntrospection: table,
}
),
};
},
},
Expand Down
205 changes: 101 additions & 104 deletions packages/graphile-build-pg/src/plugins/PgMutationPayloadEdgePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default (function PgMutationPayloadEdgePlugin(builder) {
builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
const {
extend,
getSafeAliasFromResolveInfo,
getTypeByName,
pgGetGqlTypeByTypeIdAndModifier,
pgSql: sql,
Expand All @@ -14,11 +15,11 @@ export default (function PgMutationPayloadEdgePlugin(builder) {
inflection,
pgOmit: omit,
describePgEntity,
pgField,
} = build;
const {
scope: { isMutationPayload, pgIntrospection, pgIntrospectionTable },
fieldWithHooks,
recurseDataGeneratorsForField,
Self,
} = context;
const table = pgIntrospectionTable || pgIntrospection;
Expand Down Expand Up @@ -56,125 +57,121 @@ export default (function PgMutationPayloadEdgePlugin(builder) {
const canOrderBy = !omit(table, "order");

const fieldName = inflection.edgeField(table);
recurseDataGeneratorsForField(fieldName);
const defaultValueEnum =
canOrderBy &&
(TableOrderByType.getValues().find(v => v.name === "PRIMARY_KEY_ASC") ||
TableOrderByType.getValues()[0]);
return extend(
fields,
{
[fieldName]: fieldWithHooks(
[fieldName]: pgField(
build,
fieldWithHooks,
fieldName,
({ addArgDataGenerator }) => {
addArgDataGenerator(function connectionOrderBy({
orderBy: rawOrderBy,
}) {
{
description: `An edge for our \`${tableTypeName}\`. May be used by Relay 1.`,
type: TableEdgeType,
args: canOrderBy
? {
orderBy: {
description: `The method to use when ordering \`${tableTypeName}\`.`,
type: new GraphQLList(new GraphQLNonNull(TableOrderByType)),
defaultValue: defaultValueEnum && defaultValueEnum.value,
},
}
: {},
resolve(data, { orderBy: rawOrderBy }, _context, resolveInfo) {
const safeAlias = getSafeAliasFromResolveInfo(resolveInfo);
const edge = data.data[safeAlias];
if (!edge) {
return null;
}
const orderBy =
canOrderBy && rawOrderBy
? Array.isArray(rawOrderBy)
? rawOrderBy
: [rawOrderBy]
: null;
return {
pgQuery: queryBuilder => {
if (orderBy != null) {
const aliases = [];
const expressions = [];
let unique = false;
orderBy.forEach(item => {
const { alias, specs, unique: itemIsUnique } = item;
unique = unique || itemIsUnique;
const orders = Array.isArray(specs[0]) ? specs : [specs];
orders.forEach(([col, _ascending]) => {
if (!col) {
return;
}
const expr = isString(col)
? sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
col
)}`
: col;
expressions.push(expr);
});
if (alias == null) return;
aliases.push(alias);
});
if (!unique && primaryKeys) {
// Add PKs
primaryKeys.forEach(key => {
expressions.push(
sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
key.name
)}`
);
});
}
if (aliases.length) {
queryBuilder.select(
sql.fragment`json_build_array(${sql.join(
aliases.map(
a => sql.fragment`${sql.literal(a)}::text`
),
", "
)}, json_build_array(${sql.join(expressions, ", ")}))`,
"__order_" + aliases.join("__")
);
}
}
},
};
});

const defaultValueEnum =
canOrderBy &&
(TableOrderByType.getValues().find(
v => v.name === "PRIMARY_KEY_ASC"
) ||
TableOrderByType.getValues()[0]);
return {
description: `An edge for our \`${tableTypeName}\`. May be used by Relay 1.`,
type: TableEdgeType,
args: canOrderBy
? {
orderBy: {
description: `The method to use when ordering \`${tableTypeName}\`.`,
type: new GraphQLList(
new GraphQLNonNull(TableOrderByType)
),
defaultValue: defaultValueEnum && defaultValueEnum.value,
},
}
: {},
resolve(data, { orderBy: rawOrderBy }) {
const orderBy =
canOrderBy && rawOrderBy
? Array.isArray(rawOrderBy)
? rawOrderBy
: [rawOrderBy]
: null;
const order =
orderBy && orderBy.some(item => item.alias)
? orderBy.filter(item => item.alias)
: null;
const order =
orderBy && orderBy.some(item => item.alias)
? orderBy.filter(item => item.alias)
: null;

if (!order) {
if (data.data.__identifiers) {
return Object.assign({}, data.data, {
__cursor: ["primary_key_asc", data.data.__identifiers],
});
} else {
return data.data;
}
if (!order) {
if (edge.__identifiers) {
return Object.assign({}, edge, {
__cursor: ["primary_key_asc", edge.__identifiers],
});
} else {
return edge;
}
return Object.assign({}, data.data, {
__cursor:
data.data[
`__order_${order.map(item => item.alias).join("__")}`
],
});
},
};
}

return Object.assign({}, edge, {
__cursor:
edge[`__order_${order.map(item => item.alias).join("__")}`],
});
},
},
{
isPgMutationPayloadEdgeField: true,
pgFieldIntrospection: table,
},
false,
{
withQueryBuilder(queryBuilder, { parsedResolveInfoFragment }) {
const {
args: { orderBy: rawOrderBy },
} = parsedResolveInfoFragment;
const orderBy =
canOrderBy && rawOrderBy
? Array.isArray(rawOrderBy)
? rawOrderBy
: [rawOrderBy]
: null;
if (orderBy != null) {
const aliases = [];
const expressions = [];
let unique = false;
orderBy.forEach(item => {
const { alias, specs, unique: itemIsUnique } = item;
unique = unique || itemIsUnique;
const orders = Array.isArray(specs[0]) ? specs : [specs];
orders.forEach(([col, _ascending]) => {
if (!col) {
return;
}
const expr = isString(col)
? sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
col
)}`
: col;
expressions.push(expr);
});
if (alias == null) return;
aliases.push(alias);
});
if (!unique && primaryKeys) {
// Add PKs
primaryKeys.forEach(key => {
expressions.push(
sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
key.name
)}`
);
});
}
if (aliases.length) {
queryBuilder.select(
sql.fragment`json_build_array(${sql.join(
aliases.map(a => sql.fragment`${sql.literal(a)}::text`),
", "
)}, json_build_array(${sql.join(expressions, ", ")}))`,
"__order_" + aliases.join("__")
);
}
}
},
}
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default (async function PgMutationUpdateDeletePlugin(
pgViaTemporaryTable: viaTemporaryTable,
describePgEntity,
sqlCommentByAddingTags,
pgField,
} = build;
const {
scope: { isRootMutation },
Expand Down Expand Up @@ -203,12 +204,8 @@ export default (async function PgMutationUpdateDeletePlugin(
: "updatePayloadType"
](table),
description: `The output of our ${mode} \`${tableTypeName}\` mutation.`,
fields: ({
recurseDataGeneratorsForField,
fieldWithHooks,
}) => {
fields: ({ fieldWithHooks }) => {
const tableName = inflection.tableFieldName(table);
recurseDataGeneratorsForField(tableName);
// This should really be `-node-id` but for compatibility with PostGraphQL v3 we haven't made that change.
const deletedNodeIdFieldName = camelCase(
`deleted-${singularize(table.name)}-id`
Expand All @@ -220,13 +217,17 @@ export default (async function PgMutationUpdateDeletePlugin(
"The exact same `clientMutationId` that was provided in the mutation input, unchanged and unused. May be used by a client to track mutations.",
type: GraphQLString,
},
[tableName]: {
description: `The \`${tableTypeName}\` that was ${mode}d by this mutation.`,
type: Table,
resolve(data) {
return data.data;
[tableName]: pgField(
build,
fieldWithHooks,
tableName,
{
description: `The \`${tableTypeName}\` that was ${mode}d by this mutation.`,
type: Table,
},
},
{},
false
),
},
mode === "delete"
? {
Expand Down
Loading

0 comments on commit 1f3328e

Please sign in to comment.