Skip to content

Commit

Permalink
Entity-no-entity (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Apr 19, 2024
1 parent 16297eb commit 5091afb
Show file tree
Hide file tree
Showing 6 changed files with 129 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 @@ -13,6 +13,7 @@ import simpleInaccessible from "./test-cases/simple-inaccessible";
import enumIntersection from "./test-cases/enum-intersection";
import inputObjectIntersection from "./test-cases/input-object-intersection";
import requiresWithFragments from "./test-cases/requires-with-fragments";
import entityAndNoEntity from "./test-cases/entity-and-no-entity";

const testCases = [
unionIntersectionTestCase,
Expand All @@ -29,6 +30,7 @@ const testCases = [
enumIntersection,
inputObjectIntersection,
requiresWithFragments,
entityAndNoEntity,
];

function routerFetch(request: Request) {
Expand Down
27 changes: 27 additions & 0 deletions src/test-cases/entity-and-no-entity/a.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createSubgraph } from "../../subgraph";
import { users } from "./data";

export default createSubgraph("a", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@shareable"]
)
type User {
id: ID @shareable
}
type Query {
users: [User!]!
}
`,
resolvers: {
Query: {
users() {
return users.map((u) => ({ __typename: "User", id: u.id }));
},
},
},
});
44 changes: 44 additions & 0 deletions src/test-cases/entity-and-no-entity/b.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createSubgraph } from "../../subgraph";

import { users } from "./data";

export default createSubgraph("b", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@shareable", "@key"]
)
union Account = User | Admin
type User @key(fields: "id") {
id: ID!
name: String
}
type Admin {
id: ID
photo: String @shareable
}
type Query {
accounts: [Account!]!
}
`,
resolvers: {
Query: {
accounts() {
return [
...users.map((user) => ({ __typename: "User", ...user })),
{ __typename: "Admin", id: "a1", photo: "a1-photo" },
];
},
},
User: {
__resolveReference(key: { id: string }) {
return users.find((user) => user.id === key.id);
},
},
},
});
6 changes: 6 additions & 0 deletions src/test-cases/entity-and-no-entity/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const users = [
{
id: "n1",
name: "n1-profile",
},
];
6 changes: 6 additions & 0 deletions src/test-cases/entity-and-no-entity/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("entity-and-no-entity", [a, b], test);
44 changes: 44 additions & 0 deletions src/test-cases/entity-and-no-entity/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createTest } from "../../test";

export default [
createTest(
/* GraphQL */ `
query {
users {
id
name
}
accounts {
... on User {
id
name
}
... on Admin {
id
photo
}
}
}
`,
{
data: {
users: [
{
id: "n1",
name: "n1-profile",
},
],
accounts: [
{
id: "n1",
name: "n1-profile",
},
{
id: "a1",
photo: "a1-photo",
},
],
},
}
),
];

0 comments on commit 5091afb

Please sign in to comment.