Skip to content

Commit

Permalink
fix(namespaces): allow JWT from private schema; pass non-null schema …
Browse files Browse the repository at this point in the history
…name to inflectors (#70)
  • Loading branch information
benjie committed Sep 14, 2017
1 parent 9a1f2d5 commit 0d48a5d
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 90 deletions.
2 changes: 2 additions & 0 deletions packages/graphile-build-pg/res/introspection-query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ with
rel.relname as "name",
dsc.description as "description",
rel.relnamespace as "namespaceId",
nsp.nspname as "namespaceName",
rel.reltype as "typeId",
-- Here we determine whether or not we can use this class in a
-- `SELECT`’s `FROM` clause. In order to determine this we look at them
Expand All @@ -101,6 +102,7 @@ with
from
pg_catalog.pg_class as rel
left join pg_catalog.pg_description as dsc on dsc.objoid = rel.oid and dsc.objsubid = 0
left join pg_catalog.pg_namespace as nsp on nsp.oid = rel.relnamespace
where
-- Select classes that are in our namespace, or are referenced in a
-- procedure.
Expand Down
4 changes: 2 additions & 2 deletions packages/graphile-build-pg/src/plugins/PgColumnsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default (function PgColumnsPlugin(
const fieldName = inflection.column(
attr.name,
table.name,
table.namespace && table.namespace.name
table.namespaceName
);
memo[
fieldName
Expand Down Expand Up @@ -155,7 +155,7 @@ export default (function PgColumnsPlugin(
const fieldName = inflection.column(
attr.name,
table.name,
table.namespace && table.namespace.name
table.namespaceName
);
memo[fieldName] = pgAddSubfield(fieldName, attr.name, attr.type, {
description: attr.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default (function PgComputedColumnsPlugin(builder) {
isInputType ||
!(isPgRowType || isPgCompoundType) ||
!table ||
table.kind !== "class"
table.kind !== "class" ||
!table.namespace
) {
return fields;
}
Expand Down
100 changes: 51 additions & 49 deletions packages/graphile-build-pg/src/plugins/PgConnectionArgCondition.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,50 @@ export default (function PgConnectionArgCondition(
graphql: { GraphQLInputObjectType, GraphQLString },
}
) => {
introspectionResultsByKind.class.map(table => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace && table.namespace.name
);
/* const TableConditionType = */
newWithHooks(
GraphQLInputObjectType,
{
description: `A condition to be used against \`${tableTypeName}\` object types. All fields are tested for equality and combined with a logical ‘and.’`,
name: inflection.conditionType(
inflection.tableType(
table.name,
table.namespace && table.namespace.name
)
),
fields: ({ fieldWithHooks }) =>
introspectionResultsByKind.attribute
.filter(attr => attr.classId === table.id)
.reduce((memo, attr) => {
const fieldName = inflection.column(
attr.name,
table.name,
table.namespace && table.namespace.name
);
memo[fieldName] = fieldWithHooks(
fieldName,
{
description: `Checks for equality with the object’s \`${fieldName}\` field.`,
type: gqlTypeByTypeId[attr.typeId] || GraphQLString,
},
{
isPgConnectionConditionInputField: true,
}
);
return memo;
}, {}),
},
{
pgIntrospection: table,
isPgCondition: true,
}
);
});
introspectionResultsByKind.class
.filter(table => table.isSelectable)
.filter(table => !!table.namespace)
.forEach(table => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
/* const TableConditionType = */
newWithHooks(
GraphQLInputObjectType,
{
description: `A condition to be used against \`${tableTypeName}\` object types. All fields are tested for equality and combined with a logical ‘and.’`,
name: inflection.conditionType(
inflection.tableType(table.name, table.namespace.name)
),
fields: ({ fieldWithHooks }) =>
introspectionResultsByKind.attribute
.filter(attr => attr.classId === table.id)
.reduce((memo, attr) => {
const fieldName = inflection.column(
attr.name,
table.name,
table.namespace.name
);
memo[fieldName] = fieldWithHooks(
fieldName,
{
description: `Checks for equality with the object’s \`${fieldName}\` field.`,
type: gqlTypeByTypeId[attr.typeId] || GraphQLString,
},
{
isPgConnectionConditionInputField: true,
}
);
return memo;
}, {}),
},
{
pgIntrospection: table,
isPgCondition: true,
}
);
});
return _;
}
);
Expand All @@ -78,15 +78,17 @@ export default (function PgConnectionArgCondition(
addArgDataGenerator,
}
) => {
if (!isPgConnectionField || !table || table.kind !== "class") {
if (
!isPgConnectionField ||
!table ||
table.kind !== "class" ||
!table.namespace
) {
return args;
}
const TableConditionType = getTypeByName(
inflection.conditionType(
inflection.tableType(
table.name,
table.namespace && table.namespace.name
)
inflection.tableType(table.name, table.namespace.name)
)
);

Expand Down
59 changes: 34 additions & 25 deletions packages/graphile-build-pg/src/plugins/PgConnectionArgOrderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@ export default (function PgConnectionArgOrderBy(
graphql: { GraphQLEnumType },
}
) => {
introspectionResultsByKind.class.map(table => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace && table.namespace.name
);
/* const TableOrderByType = */
newWithHooks(
GraphQLEnumType,
{
name: inflection.orderByType(tableTypeName),
description: `Methods to use when ordering \`${tableTypeName}\`.`,
values: {
NATURAL: {
value: {
alias: null,
specs: [],
introspectionResultsByKind.class
.filter(table => table.isSelectable)
.filter(table => !!table.namespace)
.forEach(table => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
/* const TableOrderByType = */
newWithHooks(
GraphQLEnumType,
{
name: inflection.orderByType(tableTypeName),
description: `Methods to use when ordering \`${tableTypeName}\`.`,
values: {
NATURAL: {
value: {
alias: null,
specs: [],
},
},
},
},
},
{
pgIntrospection: table,
isPgRowSortEnum: true,
}
);
});
{
pgIntrospection: table,
isPgRowSortEnum: true,
}
);
});
return _;
}
);
Expand All @@ -55,12 +58,18 @@ export default (function PgConnectionArgOrderBy(
addArgDataGenerator,
}
) => {
if (!isPgConnectionField || !table || table.kind !== "class") {
if (
!isPgConnectionField ||
!table ||
table.kind !== "class" ||
!table.namespace ||
!table.isSelectable
) {
return args;
}
const tableTypeName = inflection.tableType(
table.name,
table.namespace && table.namespace.name
table.namespace.name
);
const TableOrderByType = getTypeByName(
inflection.orderByType(tableTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default (function PgForwardRelationPlugin(
if (
!(isPgRowType || isMutationPayload) ||
!table ||
table.kind !== "class"
table.kind !== "class" ||
!table.namespace
) {
return fields;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/graphile-build-pg/src/plugins/PgJWTPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export default (function PgJWTPlugin(
!table.isUpdatable &&
!table.isDeletable &&
table.name === typeName &&
table.namespace &&
table.namespace.name === namespaceName
table.namespaceName === namespaceName
);
if (!compositeClass) {
throw new Error(
Expand All @@ -67,7 +66,7 @@ export default (function PgJWTPlugin(

const compositeTypeName = inflection.tableType(
compositeClass.name,
compositeClass.namespace.name
compositeClass.namespaceName
);

const JWTType = newWithHooks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default (function PgMutationProceduresPlugin(builder) {
fields,
introspectionResultsByKind.procedure
.filter(proc => !proc.isStable)
.filter(proc => !!proc.namespace)
.reduce((memo, proc) => {
/*
proc =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export default (function PgOrderAllColumnsPlugin(
attr.name,
true,
table.name,
table.namespace && table.namespace.name
table.namespaceName
);
const descFieldName = inflection.orderByEnum(
attr.name,
false,
table.name,
table.namespace && table.namespace.name
table.namespaceName
);
memo[ascFieldName] = {
value: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default (function PgQueryProceduresPlugin(builder) {
fields,
introspectionResultsByKind.procedure
.filter(proc => proc.isStable)
.filter(proc => !!proc.namespace)
.reduce((memo, proc) => {
/*
proc =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default (function PgTablesPlugin(builder, { pgInflection: inflection }) {
const Cursor = getTypeByName("Cursor");
introspectionResultsByKind.procedure
.filter(proc => proc.returnsSet)
.filter(proc => !!proc.namespace)
.forEach(proc => {
const returnType =
introspectionResultsByKind.typeById[proc.returnTypeId];
Expand Down
6 changes: 3 additions & 3 deletions packages/graphile-build-pg/src/plugins/PgTablesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default (function PgTablesPlugin(builder, { pgInflection: inflection }) {
isUpdatable: true,
isDeletable: true }
*/
const schema = table.namespace;
const primaryKeyConstraint = introspectionResultsByKind.constraint
.filter(con => con.classId === table.id)
.filter(con => con.type === "p")[0];
Expand All @@ -71,11 +70,12 @@ export default (function PgTablesPlugin(builder, { pgInflection: inflection }) {
.sort((a1, a2) => a1.num - a2.num);
const tableTypeName = inflection.tableType(
table.name,
schema && schema.name
table.namespaceName
);
const shouldHaveNodeId: boolean =
nodeIdFieldName &&
table.isSelectable &&
table.namespace &&
primaryKeys &&
primaryKeys.length
? true
Expand Down Expand Up @@ -168,7 +168,7 @@ export default (function PgTablesPlugin(builder, { pgInflection: inflection }) {
const fieldName = inflection.column(
attr.name,
table.name,
table.namespace.name
table.namespaceName
);
const pgInputField = pgInputFields[fieldName];
const v = obj[fieldName];
Expand Down
5 changes: 1 addition & 4 deletions packages/graphile-build-pg/src/plugins/makeProcField.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ export default function makeProcField(
const TableType =
returnTypeTable &&
getTypeByName(
inflection.tableType(
returnTypeTable.name,
returnTypeTable.namespace && returnTypeTable.namespace.name
)
inflection.tableType(returnTypeTable.name, returnTypeTable.namespaceName)
);

const isTableLike: boolean =
Expand Down

0 comments on commit 0d48a5d

Please sign in to comment.