Skip to content

Commit 79dd2f3

Browse files
Add unit test for handling external types in federation
1 parent 2891d35 commit 79dd2f3

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package schema
7+
8+
import (
9+
"strings"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// TestApolloServiceQueryExcludesAuxiliaryTypes verifies that auxiliary types
16+
// are not generated for @extends types when apolloServiceQuery=true
17+
func TestApolloServiceQueryExcludesAuxiliaryTypes(t *testing.T) {
18+
schema := `
19+
type Review {
20+
id: ID!
21+
rating: Int! @search
22+
comment: String @search(by: [fulltext])
23+
}
24+
25+
extend type User @key(fields: "userId") {
26+
userId: ID! @external
27+
reviews: [Review]
28+
}
29+
`
30+
31+
// Parse with apolloServiceQuery=true (simulating _service query)
32+
handler, err := NewHandler(schema, true)
33+
require.NoError(t, err)
34+
35+
// Get the SDL that would be returned by _service
36+
sdl := handler.GQLSchemaWithoutApolloExtras()
37+
38+
// These auxiliary types should NOT be present for extended User type
39+
auxiliaryTypes := []string{
40+
"input UserPatch",
41+
"input AddUserInput",
42+
"enum UserOrderable",
43+
"enum UserHasFilter",
44+
"input UserFilter",
45+
"input UserRef",
46+
"type AddUserPayload",
47+
"type UpdateUserPayload",
48+
"type DeleteUserPayload",
49+
"addUser(",
50+
"updateUser(",
51+
"deleteUser(",
52+
"getUser(",
53+
"queryUser(",
54+
}
55+
56+
for _, auxType := range auxiliaryTypes {
57+
if strings.Contains(sdl, auxType) {
58+
t.Errorf("SDL should NOT contain '%s' for extended User type", auxType)
59+
}
60+
}
61+
62+
// Review types SHOULD be present (not extended)
63+
reviewTypes := []string{
64+
"input ReviewPatch",
65+
"input AddReviewInput",
66+
"enum ReviewOrderable",
67+
"enum ReviewHasFilter",
68+
}
69+
70+
for _, reviewType := range reviewTypes {
71+
if !strings.Contains(sdl, reviewType) {
72+
t.Errorf("SDL SHOULD contain '%s' for Review type", reviewType)
73+
}
74+
}
75+
}
76+
77+
// TestApolloServiceQueryWithoutExtends verifies that auxiliary types ARE generated
78+
// for regular types when apolloServiceQuery=true
79+
func TestApolloServiceQueryWithoutExtends(t *testing.T) {
80+
schema := `
81+
type User @key(fields: "userId") {
82+
userId: ID!
83+
username: String! @search(by: [hash])
84+
email: String
85+
}
86+
`
87+
88+
// Parse with apolloServiceQuery=true
89+
handler, err := NewHandler(schema, true)
90+
require.NoError(t, err)
91+
92+
sdl := handler.GQLSchemaWithoutApolloExtras()
93+
94+
// Auxiliary types SHOULD be present for non-extended User type
95+
auxiliaryTypes := []string{
96+
"input UserPatch",
97+
"input AddUserInput",
98+
"enum UserOrderable",
99+
"enum UserHasFilter",
100+
}
101+
102+
for _, auxType := range auxiliaryTypes {
103+
if !strings.Contains(sdl, auxType) {
104+
t.Errorf("SDL SHOULD contain '%s' for regular User type", auxType)
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)