-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
requires + inaccessible + fragments (#14)
- Loading branch information
1 parent
5677f21
commit 00c0542
Showing
6 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { createSubgraph } from "../../subgraph"; | ||
import { bazs, entities, quxs } from "./data"; | ||
|
||
export default createSubgraph("a", { | ||
typeDefs: /* GraphQL */ ` | ||
extend schema | ||
@link( | ||
url: "https://specs.apollo.dev/federation/v2.5" | ||
import: ["@key", "@shareable"] | ||
) | ||
type Query @shareable { | ||
a: Entity | ||
} | ||
type Entity @key(fields: "id") { | ||
id: ID! | ||
data: Foo | ||
} | ||
interface Foo { | ||
foo: String! | ||
} | ||
interface Bar implements Foo { | ||
foo: String! | ||
bar: String! | ||
} | ||
type Baz implements Foo & Bar @shareable { | ||
foo: String! | ||
bar: String! | ||
baz: String! | ||
} | ||
type Qux implements Foo & Bar @shareable { | ||
foo: String! | ||
bar: String! | ||
qux: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
a() { | ||
return entities[1]; | ||
}, | ||
}, | ||
Entity: { | ||
__resolveReference(key: { id: string }) { | ||
const entity = entities.find((e) => e.id === key.id); | ||
|
||
if (!entity) { | ||
return null; | ||
} | ||
|
||
return entity; | ||
}, | ||
data(entity: { id: string; data: string }) { | ||
const data = entities.find((e) => e.id === entity.id)?.data; | ||
|
||
if (!data) { | ||
return null; | ||
} | ||
|
||
const baz = bazs.find((b) => b.id === data); | ||
|
||
if (baz) { | ||
return baz; | ||
} | ||
|
||
const qux = quxs.find((q) => q.id === data); | ||
|
||
if (qux) { | ||
return qux; | ||
} | ||
|
||
throw new Error("Invalid data"); | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { createSubgraph } from "../../subgraph"; | ||
import { entities, bazs, quxs } from "./data"; | ||
|
||
type WithData<T> = T & { | ||
data: { | ||
foo: string; | ||
} & ({ bar: string; baz: string } | { bar: string; qux: string }); | ||
}; | ||
|
||
export default createSubgraph("b", { | ||
typeDefs: /* GraphQL */ ` | ||
extend schema | ||
@link( | ||
url: "https://specs.apollo.dev/federation/v2.5" | ||
import: [ | ||
"@key" | ||
"@shareable" | ||
"@inaccessible" | ||
"@external" | ||
"@requires" | ||
] | ||
) | ||
type Query @shareable { | ||
b: Entity | ||
bb: Entity | ||
} | ||
type Entity @key(fields: "id") { | ||
id: ID! | ||
data: Foo @external | ||
requirer: String! | ||
@requires( | ||
fields: """ | ||
data { | ||
foo | ||
... on Bar { | ||
bar | ||
... on Baz { | ||
baz | ||
} | ||
... on Qux { | ||
qux | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
} | ||
interface Foo { | ||
foo: String! | ||
} | ||
interface Bar implements Foo { | ||
foo: String! | ||
bar: String! | ||
} | ||
type Baz implements Foo & Bar @shareable @inaccessible { | ||
foo: String! | ||
bar: String! | ||
baz: String! | ||
} | ||
type Qux implements Foo & Bar @shareable { | ||
foo: String! | ||
bar: String! | ||
qux: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
b() { | ||
return entities[0]; | ||
}, | ||
bb() { | ||
return entities[1]; | ||
}, | ||
}, | ||
Entity: { | ||
__resolveReference(key: { id: string } | WithData<{ id: string }>) { | ||
const entity = entities.find((e) => e.id === key.id); | ||
|
||
if (!entity) { | ||
return null; | ||
} | ||
|
||
return { | ||
...entity, | ||
...key, | ||
}; | ||
}, | ||
data(entity: { id: string }) { | ||
const data = entities.find((e) => e.id === entity.id)?.data; | ||
|
||
if (!data) { | ||
return null; | ||
} | ||
|
||
const baz = bazs.find((b) => b.id === data); | ||
|
||
if (baz) { | ||
return baz; | ||
} | ||
|
||
const qux = quxs.find((q) => q.id === data); | ||
|
||
if (qux) { | ||
return qux; | ||
} | ||
|
||
throw new Error("Invalid data"); | ||
}, | ||
requirer(entity: WithData<{ id: string }>) { | ||
console.log(); | ||
if (!("data" in entity)) { | ||
throw new Error("Expected entity to have a data field"); | ||
} | ||
|
||
return entity.data.foo + "_requirer"; | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const entities = [ | ||
{ | ||
id: "e1", | ||
data: "b1", | ||
}, | ||
{ | ||
id: "e2", | ||
data: "q1", | ||
}, | ||
]; | ||
|
||
export const bazs = [ | ||
{ | ||
__typename: "Baz", | ||
id: "b1", | ||
foo: "b1-foo", | ||
bar: "b1-bar", | ||
baz: "b1-baz", | ||
}, | ||
]; | ||
|
||
export const quxs = [ | ||
{ | ||
__typename: "Qux", | ||
id: "q1", | ||
foo: "q1-foo", | ||
bar: "q1-bar", | ||
qux: "q1-qux", | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("requires-with-fragments", [a, b], test); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { createTest } from "../../test"; | ||
|
||
export default [ | ||
createTest( | ||
/* GraphQL */ ` | ||
query { | ||
b { | ||
id | ||
data { | ||
__typename | ||
} | ||
} | ||
bb { | ||
id | ||
data { | ||
__typename | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
data: { | ||
b: { | ||
id: "e1", | ||
data: null, | ||
}, | ||
bb: { | ||
id: "e2", | ||
data: { | ||
__typename: "Qux", | ||
}, | ||
}, | ||
}, | ||
} | ||
), | ||
createTest( | ||
/* GraphQL */ ` | ||
query { | ||
a { | ||
requirer | ||
} | ||
} | ||
`, | ||
{ | ||
data: { | ||
a: { | ||
requirer: "q1-foo_requirer", | ||
}, | ||
}, | ||
} | ||
), | ||
createTest( | ||
/* GraphQL */ ` | ||
query { | ||
a { | ||
data { | ||
__typename | ||
} | ||
requirer | ||
} | ||
} | ||
`, | ||
{ | ||
data: { | ||
a: { | ||
data: { | ||
__typename: "Qux", | ||
}, | ||
requirer: "q1-foo_requirer", | ||
}, | ||
}, | ||
} | ||
), | ||
createTest( | ||
/* GraphQL */ ` | ||
query { | ||
bb { | ||
data { | ||
__typename | ||
} | ||
requirer | ||
} | ||
} | ||
`, | ||
{ | ||
data: { | ||
bb: { | ||
data: { | ||
__typename: "Qux", | ||
}, | ||
requirer: "q1-foo_requirer", | ||
}, | ||
}, | ||
} | ||
), | ||
createTest( | ||
/* GraphQL */ ` | ||
query { | ||
b { | ||
data { | ||
__typename | ||
} | ||
requirer | ||
} | ||
} | ||
`, | ||
{ | ||
data: { | ||
b: { | ||
data: null, | ||
requirer: "b1-foo_requirer", | ||
}, | ||
}, | ||
} | ||
), | ||
]; |