graphile-build-pg
Advanced tools
Comparing version 4.0.0-beta.5 to 4.0.0-beta.6
@@ -30,3 +30,5 @@ "use strict"; | ||
for (const k in obj) { | ||
firstKey = k; | ||
if (k[0] !== "_" && k[1] !== "_") { | ||
firstKey = k; | ||
} | ||
} | ||
@@ -154,2 +156,6 @@ return obj[firstKey]; | ||
fieldScope.isPgFieldConnection = true; | ||
// We don't return the first value as the value here because it gets | ||
// sent down into PgScalarFunctionConnectionPlugin so the relevant | ||
// EdgeType can return cursor / node; i.e. we might want to add an | ||
// `__cursor` field so we can't just use a scalar. | ||
} | ||
@@ -301,2 +307,10 @@ } else { | ||
} | ||
// If this is a table we can process it directly; but if it's a scalar | ||
// setof function we must dereference '.value' from it, because this | ||
// makes space for '__cursor' to exist alongside it (whereas on a table | ||
// the '__cursor' can just be on the table object itself) | ||
const scalarAwarePg2gql = v => isTableLike ? pg2gql(v, returnType) : { | ||
...v, | ||
value: pg2gql(v.value, returnType) | ||
}; | ||
@@ -312,3 +326,7 @@ return { | ||
if (proc.returnsSet) { | ||
// EITHER `isMutation` is true, or `ConnectionType` does not | ||
// exist - either way, we're not returning a connection. | ||
return value.data.map(firstValue).map(v => pg2gql(v, returnType)); | ||
} else if (rawReturnType.isPgArray) { | ||
return value.map(firstValue).map(v => pg2gql(v, returnType)); | ||
} else { | ||
@@ -319,5 +337,10 @@ return pg2gql(value, returnType); | ||
if (proc.returnsSet && !isMutation) { | ||
return (0, _addStartEndCursor2.default)(value); | ||
return (0, _addStartEndCursor2.default)({ | ||
...value, | ||
data: value.data.map(scalarAwarePg2gql) | ||
}); | ||
} else if (proc.returnsSet || rawReturnType.isPgArray) { | ||
return value.map(v => pg2gql(v, returnType)); | ||
} else { | ||
return value; | ||
return pg2gql(value, returnType); | ||
} | ||
@@ -330,3 +353,3 @@ } | ||
let queryResult; | ||
let queryResultRows; | ||
if (isMutation) { | ||
@@ -339,3 +362,3 @@ const query = makeQuery(parsedResolveInfoFragment, resolveInfo.returnType, functionAlias, functionAlias); | ||
await pgClient.query("SAVEPOINT graphql_mutation"); | ||
queryResult = await (0, _viaTemporaryTable2.default)(pgClient, isVoid ? null : sql.identifier(returnType.namespaceName, returnType.name), sql.query`select ${isPgClass ? sql.query`${intermediateIdentifier}.*` : sql.query`${intermediateIdentifier}.${intermediateIdentifier} as ${functionAlias}`} from ${sqlMutationQuery} ${intermediateIdentifier}`, functionAlias, query, isPgClass); | ||
queryResultRows = await (0, _viaTemporaryTable2.default)(pgClient, isVoid ? null : sql.identifier(returnType.namespaceName, returnType.name), sql.query`select ${isPgClass ? sql.query`${intermediateIdentifier}.*` : sql.query`${intermediateIdentifier}.${intermediateIdentifier} as ${functionAlias}`} from ${sqlMutationQuery} ${intermediateIdentifier}`, functionAlias, query, isPgClass); | ||
await pgClient.query("RELEASE SAVEPOINT graphql_mutation"); | ||
@@ -350,5 +373,6 @@ } catch (e) { | ||
if (debugSql.enabled) debugSql(text); | ||
queryResult = await pgClient.query(text, values); | ||
const queryResult = await pgClient.query(text, values); | ||
queryResultRows = queryResult.rows; | ||
} | ||
const { rows } = queryResult; | ||
const rows = queryResultRows; | ||
const [row] = rows; | ||
@@ -358,4 +382,7 @@ const result = (() => { | ||
if (proc.returnsSet && !isMutation) { | ||
// EITHER `isMutation` is true, or `ConnectionType` does | ||
// not exist - either way, we're not returning a | ||
// connection. | ||
return row.data.map(firstValue).map(v => pg2gql(v, returnType)); | ||
} else if (proc.returnsSet) { | ||
} else if (proc.returnsSet || rawReturnType.isPgArray) { | ||
return rows.map(firstValue).map(v => pg2gql(v, returnType)); | ||
@@ -369,3 +396,4 @@ } else { | ||
return (0, _addStartEndCursor2.default)({ | ||
data: row.data.map(row => pg2gql(row, returnType)) | ||
...row, | ||
data: row.data.map(scalarAwarePg2gql) | ||
}); | ||
@@ -372,0 +400,0 @@ } else if (proc.returnsSet || rawReturnType.isPgArray) { |
@@ -144,4 +144,4 @@ "use strict"; | ||
await pgClient.query("SAVEPOINT graphql_mutation"); | ||
const result = await (0, _viaTemporaryTable2.default)(pgClient, sql.identifier(table.namespace.name, table.name), mutationQuery, insertedRowAlias, query); | ||
row = result.rows[0]; | ||
const rows = await (0, _viaTemporaryTable2.default)(pgClient, sql.identifier(table.namespace.name, table.name), mutationQuery, insertedRowAlias, query); | ||
row = rows[0]; | ||
await pgClient.query("RELEASE SAVEPOINT graphql_mutation"); | ||
@@ -148,0 +148,0 @@ } catch (e) { |
@@ -103,4 +103,4 @@ "use strict"; | ||
await pgClient.query("SAVEPOINT graphql_mutation"); | ||
const result = await (0, _viaTemporaryTable2.default)(pgClient, sqlTypeIdentifier, sqlMutationQuery, modifiedRowAlias, query); | ||
row = result.rows[0]; | ||
const rows = await (0, _viaTemporaryTable2.default)(pgClient, sqlTypeIdentifier, sqlMutationQuery, modifiedRowAlias, query); | ||
row = rows[0]; | ||
await pgClient.query("RELEASE SAVEPOINT graphql_mutation"); | ||
@@ -107,0 +107,0 @@ } catch (e) { |
@@ -110,2 +110,5 @@ "use strict"; | ||
} | ||
if (val.__isNull) { | ||
return null; | ||
} | ||
if (pg2GqlMapper[type.id]) { | ||
@@ -112,0 +115,0 @@ return pg2GqlMapper[type.id].map(val); |
@@ -59,6 +59,7 @@ "use strict"; | ||
// It returns void, just perform the query! | ||
return await performQuery(pgClient, _pgSql2.default.query` | ||
const { rows } = await performQuery(pgClient, _pgSql2.default.query` | ||
with ${sqlResultSourceAlias} as ( | ||
${sqlMutationQuery} | ||
) ${sqlResultQuery}`); | ||
return rows; | ||
} else { | ||
@@ -76,3 +77,10 @@ /* | ||
*/ | ||
const selectionField = isPgClassLike ? sqlResultSourceAlias : _pgSql2.default.query`(${sqlResultSourceAlias}.${sqlResultSourceAlias})::${sqlTypeIdentifier}`; | ||
const selectionField = isPgClassLike ? /* | ||
* This `when foo is null then null` check might *seem* redundant, but it | ||
* is not - e.g. the compound type `(,,,,,,,)::my_type` and | ||
* `null::my_type` differ; however the former also returns true to `foo | ||
* is null`. We use this check to coalesce both into the canonical `null` | ||
* representation to make it easier to deal with below. | ||
*/ | ||
_pgSql2.default.query`(case when ${sqlResultSourceAlias} is null then null else ${sqlResultSourceAlias} end)` : _pgSql2.default.query`(${sqlResultSourceAlias}.${sqlResultSourceAlias})::${sqlTypeIdentifier}`; | ||
const result = await performQuery(pgClient, _pgSql2.default.query` | ||
@@ -84,17 +92,28 @@ with ${sqlResultSourceAlias} as ( | ||
const { rows } = result; | ||
const firstRow = rows[0]; | ||
const firstNonNullRow = rows.find(row => row !== null); | ||
// TODO: we should be able to have `pg` not interpret the results as | ||
// objects and instead just return them as arrays - then we can just do | ||
// `row[0]`. PR welcome! | ||
const firstKey = firstRow && Object.keys(firstRow)[0]; | ||
const values = rows.map(row => row[firstKey]); | ||
const firstKey = firstNonNullRow && Object.keys(firstNonNullRow)[0]; | ||
const rawValues = rows.map(row => row && row[firstKey]); | ||
const values = rawValues.filter(rawValue => rawValue !== null); | ||
const convertFieldBack = isPgClassLike ? _pgSql2.default.query`(str::${sqlTypeIdentifier}).*` : _pgSql2.default.query`str::${sqlTypeIdentifier} as ${sqlResultSourceAlias}`; | ||
return await performQuery(pgClient, _pgSql2.default.query` | ||
with ${sqlResultSourceAlias} as ( | ||
select ${convertFieldBack} | ||
from unnest((${_pgSql2.default.value(values)})::text[]) str | ||
) | ||
${sqlResultQuery}`); | ||
const { rows: filteredValuesResults } = values.length > 0 ? await performQuery(pgClient, _pgSql2.default.query`\ | ||
with ${sqlResultSourceAlias} as ( | ||
select ${convertFieldBack} | ||
from unnest((${_pgSql2.default.value(values)})::text[]) str | ||
) | ||
${sqlResultQuery} | ||
`) : { rows: [] }; | ||
const finalRows = rawValues.map(rawValue => | ||
/* | ||
* We can't simply return 'null' here because this is expected to have | ||
* come from PG, and that would never return 'null' for a row - only | ||
* the fields within said row. Using `__isNull` here is a simple | ||
* workaround to this, that's caught by `pg2gql`. | ||
*/ | ||
rawValue === null ? { __isNull: true } : filteredValuesResults.shift()); | ||
return finalRows; | ||
} | ||
}; | ||
//# sourceMappingURL=viaTemporaryTable.js.map |
{ | ||
"name": "graphile-build-pg", | ||
"version": "4.0.0-beta.5", | ||
"version": "4.0.0-beta.6", | ||
"description": "Build a GraphQL schema by reflection over a PostgreSQL schema. Easy to customize since it's built with plugins on graphile-build", | ||
@@ -39,3 +39,3 @@ "main": "node8plus/index.js", | ||
"debug": ">=2 <3", | ||
"graphile-build": "4.0.0-beta.5", | ||
"graphile-build": "4.0.0-beta.6", | ||
"graphql-iso-date": "^3.2.0", | ||
@@ -42,0 +42,0 @@ "jsonwebtoken": "^8.1.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
972250
5281
+ Addedgraphile-build@4.0.0-beta.6(transitive)
+ Addedgraphql-parse-resolve-info@4.0.0-beta.6(transitive)
- Removedgraphile-build@4.0.0-beta.5(transitive)
- Removedgraphql-parse-resolve-info@4.0.0-beta.5(transitive)
Updatedgraphile-build@4.0.0-beta.6