Skip to content

Commit

Permalink
feat(smart-comments): @foreignkey smart comments on composite types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Apr 20, 2019
1 parent 0f2442b commit ae44c98
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export default (function PgBackwardRelationPlugin(
}
const table =
introspectionResultsByKind.classById[constraint.classId];
if (!table.isSelectable) {
// Could be a composite type
return memo;
}
const tableTypeName = inflection.tableType(table);
const gqlTableType = pgGetGqlTypeByTypeIdAndModifier(
table.type.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default (function PgForwardRelationPlugin(builder, { subscriptions }) {
const {
scope: {
isPgRowType,
isPgCompositeType,
isMutationPayload,
pgIntrospection,
pgIntrospectionTable,
Expand All @@ -34,7 +35,7 @@ export default (function PgForwardRelationPlugin(builder, { subscriptions }) {

const table = pgIntrospectionTable || pgIntrospection;
if (
!(isPgRowType || isMutationPayload) ||
!(isPgRowType || isMutationPayload || isPgCompositeType) ||
!table ||
table.kind !== "class" ||
!table.namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,12 @@ function smartCommentConstraints(introspectionResults) {
if (!namespace) {
return;
}
if (klass.tags.foreignKey) {
const getType = () =>
introspectionResults.type.find(t => t.id === klass.typeId);
const foreignKey = klass.tags.foreignKey || getType().tags.foreignKey;
if (foreignKey) {
const foreignKeys =
typeof klass.tags.foreignKey === "string"
? [klass.tags.foreignKey]
: klass.tags.foreignKey;
typeof foreignKey === "string" ? [foreignKey] : foreignKey;
if (!Array.isArray(foreignKeys)) {
throw new Error(
`Invalid foreign key smart comment specified on '${
Expand Down
3 changes: 2 additions & 1 deletion packages/graphile-build-pg/src/plugins/PgTablesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export default (function PgTablesPlugin(
)}`,
pgIntrospection: table,
isPgRowType: table.isSelectable,
isPgCompoundType: !table.isSelectable,
isPgCompoundType: !table.isSelectable, // TODO:v5: remove - typo
isPgCompositeType: !table.isSelectable,
}
);
cb(TableType);
Expand Down
22 changes: 15 additions & 7 deletions packages/graphile-build-pg/src/plugins/introspectionQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,28 @@ with
from
type_all as typ
where
typ.id in (select "typeId" from class) or
typ.id in (select "typeId" from attribute) or
typ.id in (select "returnTypeId" from procedure) or
typ.id in (select unnest("argTypeIds") from procedure) or
typ.id in (
select "typeId" from class
union all
select "typeId" from attribute
union all
select "returnTypeId" from procedure
union all
select unnest("argTypeIds") from procedure
union all
-- If this type is a base type for *any* domain type, we will include it
-- in our selection. This may mean we fetch more types than we need, but
-- the alternative is to do some funky SQL recursion which would be hard
-- code to read. So we prefer code readability over selecting like 3 or
-- 4 less type rows.
--
-- We also do this for range sub types and array item types.
typ.id in (select "domainBaseTypeId" from type_all) or
typ.id in (select "rangeSubTypeId" from type_all) or
typ.id in (select "arrayItemTypeId" from type_all)
select "domainBaseTypeId" from type_all
union all
select "rangeSubTypeId" from type_all
union all
select "arrayItemTypeId" from type_all
)
order by
"namespaceId", "name"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,27 @@ Object {

exports[`jwt pgJwtTypeIdentifier with payload 1`] = `
Object {
"admin": true,
"id": 1,
"jwt": Object {
"a": 1,
"aud": "postgraphile",
"b": 2,
"c": 3,
"exp": 2130969600,
"iat": "[timestamp]",
"iss": "postgraphile",
"role": "yay",
"authPayload": Object {
"admin": true,
"id": 1,
"jwt": Object {
"a": 1,
"aud": "postgraphile",
"b": 2,
"c": 3,
"exp": 2130969600,
"iat": "[timestamp]",
"iss": "postgraphile",
"role": "yay",
},
"personById": Object {
"id": 1,
"name": "John Smith",
},
},
"personById": Object {
"id": 1,
"name": "John Smith",
},
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,25 @@ const tests = [
jwt
id
admin
personById {
id
name
}
}
personById {
id
name
}
}
}`,
schema: "withJwt",
process: ({
data: {
authenticatePayload: { authPayload },
},
}) => {
const { jwt: str } = authPayload;
return Object.assign({}, authPayload, {
jwt: Object.assign(jwt.verify(str, jwtSecret), {
iat: "[timestamp]",
process: ({ data: { authenticatePayload } }) => {
const { jwt: str } = authenticatePayload.authPayload;
return Object.assign({}, authenticatePayload, {
authPayload: Object.assign({}, authenticatePayload.authPayload, {
jwt: Object.assign(jwt.verify(str, jwtSecret), {
iat: "[timestamp]",
}),
}),
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String

\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -314,6 +317,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken

\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}

\\"\\"\\"
Expand Down Expand Up @@ -10340,6 +10346,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String

\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person

\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -10350,6 +10359,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken

\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}

\\"\\"\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -314,6 +317,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}
\\"\\"\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -319,6 +322,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}
\\"\\"\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -1629,6 +1632,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}
\\"\\"\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ type AuthenticatePayloadPayload {
\\"\\"\\"
clientMutationId: String
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
\\"\\"\\"
Our root query field type. Allows us to run any query from our mutation payload.
\\"\\"\\"
Expand All @@ -314,6 +317,9 @@ type AuthPayload {
admin: Boolean
id: Int
jwt: JwtToken
\\"\\"\\"Reads a single \`Person\` that is related to this \`AuthPayload\`.\\"\\"\\"
personById: Person
}
\\"\\"\\"
Expand Down
2 changes: 2 additions & 0 deletions packages/postgraphile-core/__tests__/kitchen-sink-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ create type b.auth_payload as (
admin bool
);

comment on type b.auth_payload is E'@foreignKey (id) references c.person';

create function b.authenticate_payload(a integer, b numeric, c bigint) returns b.auth_payload as $$ select (('yay', extract(epoch from '2037-07-12'::timestamp), a, b, c)::b.jwt_token, 1, true)::b.auth_payload $$ language sql;

create table a.similar_table_1 (
Expand Down

0 comments on commit ae44c98

Please sign in to comment.