Skip to content

Commit

Permalink
use queryFromResolveData to retrieve _entity by primary key(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenz Weber committed Nov 6, 2019
1 parent a3645eb commit 3f7c000
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Plugin } from "graphile-build";
import printFederatedSchema from "./printFederatedSchema";
import { ObjectTypeDefinition, Directive, StringValue } from "./AST";
import { PgAttribute, PgClass } from "graphile-build-pg";
import { PgAttribute, QueryBuilder } from "graphile-build-pg";

/**
* This plugin installs the schema outlined in the Apollo Federation spec, and
Expand All @@ -27,6 +27,9 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
inflection,
nodeIdFieldName,
pgSql: sql,
parseResolveInfo,
pgQueryFromResolveData: queryFromResolveData,
pgPrepareAndRun,
} = build;
// Cache
let Query: any;
Expand Down Expand Up @@ -84,6 +87,7 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
resolvers: {
Query: {
_entities(data, { representations }, context, resolveInfo) {
const { pgClient } = context;
const {
graphile: { fieldContext },
} = resolveInfo;
Expand All @@ -104,16 +108,14 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
"Failed to interpret representation, invalid nodeId"
);
}
const x = resolveNode(
return resolveNode(
nodeId,
build,
fieldContext,
data,
context,
resolveInfo
);

return x;
} else {
const type = getTypeByName(__typename);
const { pgIntrospection: table } = scopeByType.get(type);
Expand All @@ -135,18 +137,32 @@ const SchemaExtensionPlugin = makeExtendSchemaPlugin(build => {
") and ("
)})`;

const rows = await resolveInfo.graphile.selectGraphQLResultFromTable(
sql.identifier(table.namespace, table.name),
(_alias, queryBuilder) => {
const resolveData = fieldContext.getDataFromParsedResolveInfoFragment(
parseResolveInfo(resolveInfo),
type
);

const query = queryFromResolveData(
sql.identifier(table.namespace.name, table.name),
undefined,
resolveData,
{
useAsterisk: false, // Because it's only a single relation, no need
},
(queryBuilder: QueryBuilder) => {
queryBuilder.where(whereClause);
}
},
context,
resolveInfo.rootValue
);

if (rows.count !== 1) {
throw new Error("Failed to interpret representation");
}
const { text, values } = sql.compile(query);

const {
rows: [row],
} = await pgPrepareAndRun(pgClient, text, values);

return rows[0];
return { [$$nodeType]: __typename, ...row };
}
});
},
Expand Down Expand Up @@ -283,8 +299,6 @@ const AddKeyPlugin: Plugin = builder => {
}
const { federationEntityTypes } = build;

console.log(federationEntityTypes.map((type: any) => type.name));

// Add our types to the entity types
return [...types, ...federationEntityTypes];
});
Expand Down

0 comments on commit 3f7c000

Please sign in to comment.