Skip to content

Commit

Permalink
fix(graphql): solve some of the array of custom type issues (#92)
Browse files Browse the repository at this point in the history
* Solve some of the array of custom type issues

* Rewrite to lazy evaluation

* Add note about omitting NonNull
  • Loading branch information
benjie committed Oct 28, 2017
1 parent e169d33 commit 2a7ee5e
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 341 deletions.
7 changes: 0 additions & 7 deletions packages/graphile-build-pg/res/introspection-query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ with
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.
(
rel.relnamespace in (select "id" from namespace) or
rel.reltype in (select "returnTypeId" from procedure) or
rel.reltype in (select unnest("argTypeIds") from procedure)
) and
rel.relpersistence in ('p') and
-- We don't want classes that will clash with GraphQL (treat them as private)
rel.relname not like '\_\_%' and
Expand Down
8 changes: 3 additions & 5 deletions packages/graphile-build-pg/src/plugins/PgAllRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default (async function PgAllRows(
parseResolveInfo,
extend,
getTypeByName,
pgGetGqlTypeByTypeId,
pgSql: sql,
pgIntrospectionResultsByKind: introspectionResultsByKind,
},
Expand All @@ -33,11 +34,8 @@ export default (async function PgAllRows(
.filter(table => table.isSelectable)
.filter(table => table.namespace)
.reduce((memo, table) => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
const TableType = getTypeByName(tableTypeName);
const TableType = pgGetGqlTypeByTypeId(table.type.id);
const tableTypeName = TableType.name;
const ConnectionType = getTypeByName(
inflection.connection(TableType.name)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default (function PgBackwardRelationPlugin(
{
extend,
getTypeByName,
pgGetGqlTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgSql: sql,
getAliasFromResolveInfo,
Expand Down Expand Up @@ -45,7 +46,7 @@ export default (function PgBackwardRelationPlugin(
table.name,
table.namespace.name
);
const gqlTableType = getTypeByName(tableTypeName);
const gqlTableType = pgGetGqlTypeByTypeId(table.type.id);
if (!gqlTableType) {
debug(
`Could not determine type for table with id ${constraint.classId}`
Expand All @@ -54,8 +55,8 @@ export default (function PgBackwardRelationPlugin(
}
const foreignTable =
introspectionResultsByKind.classById[constraint.foreignClassId];
const gqlForeignTableType = getTypeByName(
inflection.tableType(foreignTable.name, foreignTable.namespace.name)
const gqlForeignTableType = pgGetGqlTypeByTypeId(
foreignTable.type.id
);
if (!gqlForeignTableType) {
debug(
Expand Down
8 changes: 4 additions & 4 deletions packages/graphile-build-pg/src/plugins/PgColumnsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default (function PgColumnsPlugin(
fields,
{
extend,
pgGqlTypeByTypeId: gqlTypeByTypeId,
pgGetGqlTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgSql: sql,
pg2gql,
Expand Down Expand Up @@ -62,7 +62,7 @@ export default (function PgColumnsPlugin(
fieldName,
({ getDataFromParsedResolveInfoFragment, addDataGenerator }) => {
const ReturnType =
gqlTypeByTypeId[attr.typeId] || GraphQLString;
pgGetGqlTypeByTypeId(attr.typeId) || GraphQLString;
addDataGenerator(parsedResolveInfoFragment => {
const { alias } = parsedResolveInfoFragment;
if (attr.type.type === "c") {
Expand Down Expand Up @@ -127,7 +127,7 @@ export default (function PgColumnsPlugin(
fields,
{
extend,
pgGqlInputTypeByTypeId: gqlInputTypeByTypeId,
pgGetGqlInputTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
},
{
Expand Down Expand Up @@ -161,7 +161,7 @@ export default (function PgColumnsPlugin(
description: attr.description,
type: nullableIf(
isPgPatch || !attr.isNotNull || attr.hasDefault,
gqlInputTypeByTypeId[attr.typeId] || GraphQLString
pgGetGqlInputTypeByTypeId(attr.typeId) || GraphQLString
),
});
return memo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default (function PgConnectionArgCondition(
{
newWithHooks,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgGqlInputTypeByTypeId: gqlTypeByTypeId,
pgGetGqlInputTypeByTypeId,
graphql: { GraphQLInputObjectType, GraphQLString },
}
) => {
Expand Down Expand Up @@ -44,7 +44,9 @@ export default (function PgConnectionArgCondition(
fieldName,
{
description: `Checks for equality with the object’s \`${fieldName}\` field.`,
type: gqlTypeByTypeId[attr.typeId] || GraphQLString,
type:
pgGetGqlInputTypeByTypeId(attr.typeId) ||
GraphQLString,
},
{
isPgConnectionConditionInputField: true,
Expand All @@ -71,6 +73,7 @@ export default (function PgConnectionArgCondition(
gql2pg,
extend,
getTypeByName,
pgGetGqlTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
},
{
Expand All @@ -86,10 +89,9 @@ export default (function PgConnectionArgCondition(
) {
return args;
}
const TableType = pgGetGqlTypeByTypeId(table.type.id);
const TableConditionType = getTypeByName(
inflection.conditionType(
inflection.tableType(table.name, table.namespace.name)
)
inflection.conditionType(TableType.name)
);

addArgDataGenerator(function connectionCondition({ condition }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default (function PgConnectionArgOrderBy(
"GraphQLObjectType:fields:field:args",
(
args,
{ extend, getTypeByName, pgSql: sql },
{ extend, getTypeByName, pgGetGqlTypeByTypeId, pgSql: sql },
{
scope: { isPgConnectionField, pgIntrospection: table },
addArgDataGenerator,
Expand All @@ -67,10 +67,8 @@ export default (function PgConnectionArgOrderBy(
) {
return args;
}
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
const TableType = pgGetGqlTypeByTypeId(table.type.id);
const tableTypeName = TableType.name;
const TableOrderByType = getTypeByName(
inflection.orderByType(tableTypeName)
);
Expand Down
16 changes: 6 additions & 10 deletions packages/graphile-build-pg/src/plugins/PgForwardRelationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default (function PgForwardRelationPlugin(
fields,
{
extend,
getTypeByName,
getAliasFromResolveInfo,
pgGetGqlTypeByTypeId,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgSql: sql,
},
Expand Down Expand Up @@ -51,11 +51,8 @@ export default (function PgForwardRelationPlugin(
return extend(
fields,
foreignKeyConstraints.reduce((memo, constraint) => {
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
const gqlTableType = getTypeByName(tableTypeName);
const gqlTableType = pgGetGqlTypeByTypeId(table.type.id);
const tableTypeName = gqlTableType.name;
if (!gqlTableType) {
debug(
`Could not determine type for table with id ${constraint.classId}`
Expand All @@ -64,11 +61,10 @@ export default (function PgForwardRelationPlugin(
}
const foreignTable =
introspectionResultsByKind.classById[constraint.foreignClassId];
const foreignTableTypeName = inflection.tableType(
foreignTable.name,
foreignTable.namespace.name
const gqlForeignTableType = pgGetGqlTypeByTypeId(
foreignTable.type.id
);
const gqlForeignTableType = getTypeByName(foreignTableTypeName);
const foreignTableTypeName = gqlForeignTableType.name;
if (!gqlForeignTableType) {
debug(
`Could not determine type for foreign table with id ${constraint.foreignClassId}`
Expand Down
85 changes: 43 additions & 42 deletions packages/graphile-build-pg/src/plugins/PgJWTPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default (function PgJWTPlugin(
newWithHooks,
pgSql: sql,
pgIntrospectionResultsByKind: introspectionResultsByKind,
pgGqlTypeByTypeId,
pgGqlInputTypeByTypeId,
pgRegisterGqlTypeByTypeId,
pg2GqlMapper,
pgTweaksByTypeId,
graphql: { GraphQLScalarType },
Expand Down Expand Up @@ -69,49 +68,51 @@ export default (function PgJWTPlugin(
compositeClass.namespaceName
);

const JWTType = newWithHooks(
GraphQLScalarType,
{
name: compositeTypeName,
description:
"A JSON Web Token defined by [RFC 7519](https://tools.ietf.org/html/rfc7519) which securely represents claims between two parties.",
serialize(value) {
const token = attributes.reduce((memo, attr) => {
memo[attr.name] = value[attr.name];
return memo;
}, {});
return signJwt(token, pgJwtSecret, {
audience: "postgraphql",
issuer: "postgraphql",
expiresIn: token.exp ? undefined : "1 day",
});
// NOTE: we deliberately do not create an input type
pgRegisterGqlTypeByTypeId(compositeType.id, cb => {
const JWTType = newWithHooks(
GraphQLScalarType,
{
name: compositeTypeName,
description:
"A JSON Web Token defined by [RFC 7519](https://tools.ietf.org/html/rfc7519) which securely represents claims between two parties.",
serialize(value) {
const token = attributes.reduce((memo, attr) => {
memo[attr.name] = value[attr.name];
return memo;
}, {});
return signJwt(token, pgJwtSecret, {
audience: "postgraphql",
issuer: "postgraphql",
expiresIn: token.exp ? undefined : "1 day",
});
},
},
},
{
isPgJwtType: true,
}
);

pg2GqlMapper[compositeType.id] = {
map: value => {
if (!value) return null;
const values = Object.keys(value).map(k => value[k]);
if (values.every(v => v == null)) {
return null;
{
isPgJwtType: true,
}
return value;
},
unmap: () => {
throw new Error(
"We don't support passing a JWT token into GraphQL currently"
);
},
};
);
cb(JWTType);

pg2GqlMapper[compositeType.id] = {
map: value => {
if (!value) return null;
const values = Object.keys(value).map(k => value[k]);
if (values.every(v => v == null)) {
return null;
}
return value;
},
unmap: () => {
throw new Error(
"We don't support passing a JWT token into GraphQL currently"
);
},
};

pgGqlTypeByTypeId[compositeType.id] = JWTType;
pgGqlInputTypeByTypeId[compositeType.id] = null;
pgTweaksByTypeId[compositeType.id] = fragment =>
sql.fragment`to_json(${fragment})`;
pgTweaksByTypeId[compositeType.id] = fragment =>
sql.fragment`to_json(${fragment})`;
});
return _;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export default (function PgMutationCreatePlugin(
fields,
{
extend,
getTypeByName,
newWithHooks,
parseResolveInfo,
pgIntrospectionResultsByKind,
pgGetGqlTypeByTypeId,
pgGetGqlInputTypeByTypeId,
pgSql: sql,
gql2pg,
graphql: {
Expand All @@ -45,17 +46,15 @@ export default (function PgMutationCreatePlugin(
.filter(table => table.isSelectable)
.filter(table => table.isInsertable)
.reduce((memo, table) => {
const Table = getTypeByName(
inflection.tableType(table.name, table.namespace.name)
);
const Table = pgGetGqlTypeByTypeId(table.type.id);
if (!Table) {
debug(
`There was no table type for table '${table.namespace
.name}.${table.name}', so we're not generating a create mutation for it.`
);
return memo;
}
const TableInput = getTypeByName(inflection.inputType(Table.name));
const TableInput = pgGetGqlInputTypeByTypeId(table.type.id);
if (!TableInput) {
debug(
`There was no input type for table '${table.namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default (function PgMutationPayloadEdgePlugin(
"GraphQLObjectType:fields",
(
fields,
{ extend, getTypeByName, pgSql: sql },
{ extend, getTypeByName, pgGetGqlTypeByTypeId, pgSql: sql },
{
scope: { isMutationPayload, pgIntrospection, pgIntrospectionTable },
fieldWithHooks,
Expand All @@ -27,10 +27,8 @@ export default (function PgMutationPayloadEdgePlugin(
) {
return fields;
}
const tableTypeName = inflection.tableType(
table.name,
table.namespace.name
);
const TableType = pgGetGqlTypeByTypeId(table.type.id);
const tableTypeName = TableType.name;
const TableOrderByType = getTypeByName(
inflection.orderByType(tableTypeName)
);
Expand Down
Loading

0 comments on commit 2a7ee5e

Please sign in to comment.