Skip to content

Commit 1d51abf

Browse files
author
Lenz Weber
committed
use queryFromResolveData to retrieve _entity by primary key(s)
1 parent a3645eb commit 1d51abf

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

Diff for: src/index.ts

+28-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { Plugin } from "graphile-build";
77
import printFederatedSchema from "./printFederatedSchema";
88
import { ObjectTypeDefinition, Directive, StringValue } from "./AST";
9-
import { PgAttribute, PgClass } from "graphile-build-pg";
9+
import { PgAttribute, QueryBuilder } from "graphile-build-pg";
1010

1111
/**
1212
* This plugin installs the schema outlined in the Apollo Federation spec, and
@@ -27,6 +27,9 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
2727
inflection,
2828
nodeIdFieldName,
2929
pgSql: sql,
30+
parseResolveInfo,
31+
pgQueryFromResolveData: queryFromResolveData,
32+
pgPrepareAndRun,
3033
} = build;
3134
// Cache
3235
let Query: any;
@@ -84,6 +87,7 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
8487
resolvers: {
8588
Query: {
8689
_entities(data, { representations }, context, resolveInfo) {
90+
const { pgClient } = context;
8791
const {
8892
graphile: { fieldContext },
8993
} = resolveInfo;
@@ -104,16 +108,14 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
104108
"Failed to interpret representation, invalid nodeId"
105109
);
106110
}
107-
const x = resolveNode(
111+
return resolveNode(
108112
nodeId,
109113
build,
110114
fieldContext,
111115
data,
112116
context,
113117
resolveInfo
114118
);
115-
116-
return x;
117119
} else {
118120
const type = getTypeByName(__typename);
119121
const { pgIntrospection: table } = scopeByType.get(type);
@@ -135,18 +137,32 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
135137
") and ("
136138
)})`;
137139

138-
const rows = await resolveInfo.graphile.selectGraphQLResultFromTable(
139-
sql.identifier(table.namespace, table.name),
140-
(_alias, queryBuilder) => {
140+
const resolveData = fieldContext.getDataFromParsedResolveInfoFragment(
141+
parseResolveInfo(resolveInfo),
142+
type
143+
);
144+
145+
const query = queryFromResolveData(
146+
sql.identifier(table.namespace.name, table.name),
147+
undefined,
148+
resolveData,
149+
{
150+
useAsterisk: false, // Because it's only a single relation, no need
151+
},
152+
(queryBuilder: QueryBuilder) => {
141153
queryBuilder.where(whereClause);
142-
}
154+
},
155+
context,
156+
resolveInfo.rootValue
143157
);
144158

145-
if (rows.count !== 1) {
146-
throw new Error("Failed to interpret representation");
147-
}
159+
const { text, values } = sql.compile(query);
160+
161+
const {
162+
rows: [row],
163+
} = await pgPrepareAndRun(pgClient, text, values);
148164

149-
return rows[0];
165+
return { [$$nodeType]: __typename, ...row };
150166
}
151167
});
152168
},
@@ -283,8 +299,6 @@ const AddKeyPlugin: Plugin = builder => {
283299
}
284300
const { federationEntityTypes } = build;
285301

286-
console.log(federationEntityTypes.map((type: any) => type.name));
287-
288302
// Add our types to the entity types
289303
return [...types, ...federationEntityTypes];
290304
});

0 commit comments

Comments
 (0)