Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use primary key fields if available for the @key directive #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
$$isQuery,
$$nodeType,
getTypeByName,
scopeByType,
inflection,
nodeIdFieldName,
getNodeIdForTypeAndIdentifiers,
} = build;
// Cache
let Query: any;
Expand Down Expand Up @@ -88,18 +90,61 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
if (!representation || typeof representation !== "object") {
throw new Error("Invalid representation");
}
const { __typename, [nodeIdFieldName]: nodeId } = representation;
if (!__typename || typeof nodeId !== "string") {
throw new Error("Failed to interpret representation");

const { __typename } = representation;
if (!__typename) {
throw new Error(
"Failed to interpret representation, no typename"
);
}

if (nodeIdFieldName in representation) {
const { [nodeIdFieldName]: nodeId } = representation;
if (typeof nodeId !== "string") {
throw new Error(
"Failed to interpret representation, invalid nodeId"
);
}

return resolveNode(
nodeId,
build,
fieldContext,
data,
context,
resolveInfo
);
} else {
// This only works with NodePlugin enabled
if (!getNodeIdForTypeAndIdentifiers) {
throw new Error("Failed to resolve node by identifiers");
}

const type = getTypeByName(__typename);
const {
pgIntrospection: {
primaryKeyConstraint: { keyAttributes: attrs },
},
} = scopeByType.get(type);
const identifiers = attrs.map(
attr => representation[inflection.column(attr)]
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good until here, but from here on we want to query in a way that will work even if we don't have the NodePlugin loaded. You should have access to resolveInfo.graphile.selectGraphQLResultsFromTable which you can read about here:

https://www.graphile.org/postgraphile/make-extend-schema-plugin/#the-selectgraphqlresultfromtable-helper


const nodeId = getNodeIdForTypeAndIdentifiers.call(
build,
type,
...identifiers
);

return resolveNode(
nodeId,
build,
fieldContext,
data,
context,
resolveInfo
);
}
return resolveNode(
nodeId,
build,
fieldContext,
data,
context,
resolveInfo
);
});
},

Expand Down Expand Up @@ -153,7 +198,7 @@ const AddKeyPlugin: Plugin = builder => {
const {
GraphQLObjectType: spec,
Self,
scope: { isRootQuery },
scope: { isRootQuery, pgIntrospection },
} = context;
const NodeInterface = getTypeByName(inflection.builtin("Node"));

Expand All @@ -169,7 +214,8 @@ const AddKeyPlugin: Plugin = builder => {
build.federationEntityTypes.push(Self);

/*
* We're going to add the `@key(fields: "nodeId")` directive to this type.
* We're going to add the key directive to this type. If this type has a
* defined primary key we will use that, otherwise `@key(fields: "nodeId")`
* First, we need to generate an `astNode` as if the type was generateted
* from a GraphQL SDL initially; then we assign this astNode to to the type
* (via type mutation, ick) so that Apollo Federation's `printSchema` can
Expand All @@ -179,9 +225,16 @@ const AddKeyPlugin: Plugin = builder => {
...ObjectTypeDefinition(spec),
...Self.astNode,
};
astNode.directives.push(
Directive("key", { fields: StringValue(nodeIdFieldName) })
);

const {
primaryKeyConstraint: { keyAttributes },
} = pgIntrospection;
const primaryKeyNames = keyAttributes.map(attr => inflection.column(attr));
const keyName = primaryKeyNames.length
? primaryKeyNames.join(" ")
: nodeIdFieldName;

astNode.directives.push(Directive("key", { fields: StringValue(keyName) }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely support the nodeId by default; using the primary keys directly is not a best practice. Adding support for primary keys is desired if the node plugin is disabled, and it could also be used as an alternate fetcher. Fortunately according to the Apollo federation spec you can provide @key multiple times; so this is what we should do - once for nodeId, and once for the PKs.

Self.astNode = astNode;

// We're not changing the interfaces, so return them unmodified.
Expand Down