Skip to content

Commit

Permalink
Fed v1 - @external and extends (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Apr 24, 2024
1 parent 697553c commit 891bb3f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import entityAndNoEntity from "./test-cases/entity-and-no-entity";
import nonResolvableInterfaceObject from "./test-cases/non-resolvable-interface-object";
import interfaceObjectWithRequires from "./test-cases/interface-object-with-requires";
import requiresInterface from "./test-cases/requires-interface";
import fed1ExternalExtends from "./test-cases/fed1-external-extends";

const testCases = [
unionIntersectionTestCase,
Expand All @@ -37,6 +38,7 @@ const testCases = [
nonResolvableInterfaceObject,
interfaceObjectWithRequires,
requiresInterface,
fed1ExternalExtends,
];

function routerFetch(
Expand Down
43 changes: 43 additions & 0 deletions src/test-cases/fed1-external-extends/a.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createSubgraph } from "../../subgraph";
import { users } from "./data";

export default createSubgraph("a", {
typeDefs: /* GraphQL */ `
type Query {
randomUser: User
}
extend type User @key(fields: "id") {
id: ID! @external
}
extend type User {
name: String! @external
}
`,
resolvers: {
Query: {
randomUser() {
return {
id: users[0].id,
};
},
},
User: {
__resolveReference(key: { id: string }) {
const user = users.find((u) => u.id === key.id);

if (!user) {
return null;
}

return {
id: user.id,
};
},
name() {
return "never";
},
},
},
});
48 changes: 48 additions & 0 deletions src/test-cases/fed1-external-extends/b.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createSubgraph } from "../../subgraph";
import { users } from "./data";

export default createSubgraph("b", {
typeDefs: /* GraphQL */ `
type Query {
userById(id: ID): User
}
type User @key(fields: "id") {
id: ID!
name: String!
nickname: String
}
`,
resolvers: {
Query: {
userById: (_: {}, { id }: { id: string }) => {
const user = users.find((u) => u.id === id);

if (!user) {
return null;
}

return {
id: user.id,
name: user.name,
nickname: user.nickname,
};
},
},
User: {
__resolveReference: (key: { id: string }) => {
const user = users.find((u) => u.id === key.id);

if (!user) {
return null;
}

return {
id: user.id,
name: user.name,
nickname: user.nickname,
};
},
},
},
});
12 changes: 12 additions & 0 deletions src/test-cases/fed1-external-extends/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const users = [
{
id: "u1",
name: "u1-name",
nickname: "u1-nickname",
},
{
id: "u2",
name: "u2-name",
nickname: "u2-nickname",
},
];
6 changes: 6 additions & 0 deletions src/test-cases/fed1-external-extends/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { serve } from "../../supergraph";
import a from "./a.subgraph";
import b from "./b.subgraph";
import test from "./test";

export default serve("fed1-external-extends", [a, b], test);
32 changes: 32 additions & 0 deletions src/test-cases/fed1-external-extends/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createTest } from "../../test";

export default [
createTest(
/* GraphQL */ `
query {
randomUser {
id
name
}
userById(id: "u2") {
id
name
nickname
}
}
`,
{
data: {
randomUser: {
id: "u1",
name: "u1-name",
},
userById: {
id: "u2",
name: "u2-name",
nickname: "u2-nickname",
},
},
}
),
];

0 comments on commit 891bb3f

Please sign in to comment.