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

Simple input object intersection #7

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import unavailableOverrideTestCase from "./test-cases/unavailable-override";
import overrideWithRequiresTestCase from "./test-cases/override-with-requires";
import simpleInaccessible from "./test-cases/simple-inaccessible";
import enumIntersection from "./test-cases/enum-intersection";
import inputObjectIntersection from "./test-cases/input-object-intersection";

const testCases = [
unionIntersectionTestCase,
Expand All @@ -25,6 +26,7 @@ const testCases = [
overrideWithRequiresTestCase,
simpleInaccessible,
enumIntersection,
inputObjectIntersection,
];

function routerFetch(request: Request) {
Expand Down
6 changes: 6 additions & 0 deletions src/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export function serve(
return {
id,
createRoutes(router: ReturnType<typeof createRouter>) {
let subgraphNames = new Set<string>();
for (const subgraph of subgraphs) {
if (subgraphNames.has(subgraph.name)) {
throw new Error(`Duplicate subgraph name ${subgraph.name}`);
}

subgraphNames.add(subgraph.name);
subgraph.createRoutes(id, router);
}

Expand Down
47 changes: 47 additions & 0 deletions src/test-cases/input-object-intersection/a.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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: ["@key", "@shareable"]
)

input UsersFilter {
first: Int!
}

type User @key(fields: "id") {
id: ID!
name: String! @shareable
}

type Query {
usersInA(filter: UsersFilter!): [User!]
}
`,
resolvers: {
Query: {
usersInA(_: {}, { filter }: { filter: { first: number } }) {
if ("offset" in filter) {
return [];
}

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

if (!user) {
return null;
}

return user;
},
},
},
});
51 changes: 51 additions & 0 deletions src/test-cases/input-object-intersection/b.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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: ["@key", "@shareable"]
)

input UsersFilter {
offset: Int
first: Int!
}

type User @key(fields: "id") {
id: ID!
name: String! @shareable
}

type Query {
usersInB(filter: UsersFilter!): [User!]
}
`,
resolvers: {
Query: {
usersInB(
_: {},
{ filter }: { filter: { offset?: number; first: number } }
) {
if (typeof filter.offset !== "undefined") {
return [];
}

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

if (!user) {
return null;
}

return user;
},
},
},
});
10 changes: 10 additions & 0 deletions src/test-cases/input-object-intersection/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const users = [
{
id: "u1",
name: "u1-name",
},
{
id: "u2",
name: "u2-name",
},
];
6 changes: 6 additions & 0 deletions src/test-cases/input-object-intersection/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("input-object-intersection", [a, b], test);
49 changes: 49 additions & 0 deletions src/test-cases/input-object-intersection/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createTest } from "../../test";

export default [
createTest(
/* GraphQL */ `
query {
usersInA(filter: { first: 1 }) {
id
}
}
`,
{
data: {
usersInA: [
{
id: "u1",
},
{
id: "u2",
},
],
},
}
),
createTest(
/* GraphQL */ `
query {
usersInA(filter: { first: 1, offset: 2 }) {
id
}
}
`,
{
errors: true,
}
),
createTest(
/* GraphQL */ `
query {
usersInB(filter: { first: 1, offset: 2 }) {
id
}
}
`,
{
errors: true,
}
),
];