mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-06 03:59:09 +08:00
test(graphql-generator): cross-file fragments, shared name namespace, self-overlap
Tests-only run for the stage-3 review of f2ea6726fd. All three findings
hold against the accepted specs.
- Fragments resolve across files (EFF-1831 point 3: a fragment used from
another file is imported from it). The passing case spreads a fragment
from a fragments-only file, and that fragment uses a variable the
operation defines. The failing case has a cycle that spans two files and
an undefined variable, each reported in the file that holds the node.
- Operations and fragments share one namespace (EFF-1831 point 3). A name
used by both kinds, in one file or across files, gives every definition
`There can be only one operation or fragment named "X".` The existing
operation/operation and fragment/fragment messages are unchanged.
- A composite type overlaps itself, as graphql-js's doTypesOverlap does.
The shared schema gains `interface Orphan` with no implementations.
Spreading it inline and as a named fragment inside an Orphan field
passes, and spreading it into User still fails.
The new invalid cases were checked against graphql-js 16.14.2 where it
has a matching rule, with multi-file cases concatenated into one
document.
4 of 246 generator tests fail, all against the current Validate.ts;
pnpm lint and check pass.
This commit is contained in:
@@ -7,8 +7,9 @@
|
||||
* Messages are graphql-js's wording for the same rule, without its "Did you
|
||||
* mean" suggestion lists, so the reference implementation can arbitrate.
|
||||
* Where graphql-js reports several locations, the case uses the one named in
|
||||
* the group's `location` note. Rejecting anonymous operations and cross-file
|
||||
* uniqueness have no graphql-js counterpart. graphql-js's full rule set also
|
||||
* the group's `location` note. Rejecting anonymous operations, cross-file
|
||||
* fragment resolution and name uniqueness, and the shared operation/fragment
|
||||
* namespace (EFF-1831 point 3) have no graphql-js counterpart. graphql-js's full rule set also
|
||||
* flags `$viewer` in the variables group for its position, a rule we leave to
|
||||
* the runtime.
|
||||
*/
|
||||
@@ -52,6 +53,11 @@ union Pet = Dog | Cat
|
||||
|
||||
union SearchResult = User | Repo
|
||||
|
||||
"""An interface nothing implements yet."""
|
||||
interface Orphan {
|
||||
id: ID!
|
||||
}
|
||||
|
||||
enum Role {
|
||||
ADMIN
|
||||
MEMBER
|
||||
@@ -67,6 +73,7 @@ type Query {
|
||||
user(id: ID!): User
|
||||
users(filter: UserFilter, roles: [Role!]): [User!]!
|
||||
search(term: String!): [SearchResult!]!
|
||||
orphan: Orphan
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
@@ -583,5 +590,195 @@ fragment UserFields on User {
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
rule: "fragments resolve across files",
|
||||
location: "a diagnostic is reported in the file holding the offending node",
|
||||
valid: [[
|
||||
file(
|
||||
`query Viewer($id: ID!) {
|
||||
...ViewerUser
|
||||
}
|
||||
`,
|
||||
"viewer.graphql"
|
||||
),
|
||||
file(
|
||||
`fragment ViewerUser on Query {
|
||||
user(id: $id) {
|
||||
...UserName
|
||||
}
|
||||
}
|
||||
|
||||
fragment UserName on User {
|
||||
name
|
||||
}
|
||||
`,
|
||||
"fragments.graphql"
|
||||
)
|
||||
]],
|
||||
invalid: {
|
||||
files: [
|
||||
file(
|
||||
`query Viewer {
|
||||
users {
|
||||
...A
|
||||
}
|
||||
...ViewerUser
|
||||
}
|
||||
|
||||
fragment B on User {
|
||||
...A
|
||||
}
|
||||
`,
|
||||
"viewer.graphql"
|
||||
),
|
||||
file(
|
||||
`fragment ViewerUser on Query {
|
||||
user(id: $id) {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fragment A on User {
|
||||
...B
|
||||
}
|
||||
`,
|
||||
"fragments.graphql"
|
||||
)
|
||||
],
|
||||
diagnostics: [
|
||||
{
|
||||
path: "viewer.graphql",
|
||||
line: 9,
|
||||
column: 3,
|
||||
message: "Cannot spread fragment \"B\" within itself via \"A\"."
|
||||
},
|
||||
{
|
||||
path: "fragments.graphql",
|
||||
line: 2,
|
||||
column: 12,
|
||||
message: "Variable \"$id\" is not defined by operation \"Viewer\"."
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
rule: "operations and fragments share one name namespace",
|
||||
location: "every definition sharing a name is reported at its name, in its own file",
|
||||
valid: [[
|
||||
file(
|
||||
`query Viewer {
|
||||
user(id: "1") {
|
||||
...ViewerFields
|
||||
}
|
||||
}
|
||||
|
||||
fragment ViewerFields on User {
|
||||
id
|
||||
}
|
||||
`,
|
||||
"viewer.graphql"
|
||||
),
|
||||
file(
|
||||
`query Friends {
|
||||
user(id: "1") {
|
||||
name
|
||||
}
|
||||
}
|
||||
`,
|
||||
"friends.graphql"
|
||||
)
|
||||
]],
|
||||
invalid: {
|
||||
files: [
|
||||
file(
|
||||
`query UserFields {
|
||||
user(id: "1") {
|
||||
...UserFields
|
||||
...Friends
|
||||
}
|
||||
}
|
||||
|
||||
fragment UserFields on User {
|
||||
id
|
||||
}
|
||||
|
||||
fragment Friends on User {
|
||||
name
|
||||
}
|
||||
`,
|
||||
"a.graphql"
|
||||
),
|
||||
file(
|
||||
`query Friends {
|
||||
user(id: "2") {
|
||||
id
|
||||
}
|
||||
}
|
||||
`,
|
||||
"b.graphql"
|
||||
)
|
||||
],
|
||||
diagnostics: [
|
||||
{
|
||||
path: "a.graphql",
|
||||
line: 1,
|
||||
column: 7,
|
||||
message: "There can be only one operation or fragment named \"UserFields\"."
|
||||
},
|
||||
{
|
||||
path: "a.graphql",
|
||||
line: 8,
|
||||
column: 10,
|
||||
message: "There can be only one operation or fragment named \"UserFields\"."
|
||||
},
|
||||
{
|
||||
path: "a.graphql",
|
||||
line: 12,
|
||||
column: 10,
|
||||
message: "There can be only one operation or fragment named \"Friends\"."
|
||||
},
|
||||
{
|
||||
path: "b.graphql",
|
||||
line: 1,
|
||||
column: 7,
|
||||
message: "There can be only one operation or fragment named \"Friends\"."
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
rule: "a composite type overlaps itself, even with no possible types",
|
||||
valid: [[file(`query Orphan {
|
||||
orphan {
|
||||
... on Orphan {
|
||||
id
|
||||
}
|
||||
...OrphanId
|
||||
}
|
||||
}
|
||||
|
||||
fragment OrphanId on Orphan {
|
||||
id
|
||||
}
|
||||
`)]],
|
||||
invalid: {
|
||||
files: [file(`query Viewer {
|
||||
user(id: "1") {
|
||||
... on Orphan {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`)],
|
||||
diagnostics: [
|
||||
{
|
||||
path: "query.graphql",
|
||||
line: 3,
|
||||
column: 5,
|
||||
message: "Fragment cannot be spread here as objects of type \"User\" can never be of type \"Orphan\"."
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user