@@ -17,7 +17,7 @@ export const createPolicy = (level: RuleInstanceSeverityLevel): SchemaPolicyInpu
17
17
18
18
describe ( 'Schema policy checks' , ( ) => {
19
19
describe ( 'model: composite' , ( ) => {
20
- it . only ( 'no-unreachable-types issue: directives are not scanned/marked as unused' , async ( ) => {
20
+ it ( 'no-unreachable-types issue: directives are not scanned/marked as unused' , async ( ) => {
21
21
const policyObject : SchemaPolicyInput = {
22
22
rules : [
23
23
{
@@ -53,15 +53,23 @@ describe('Schema policy checks', () => {
53
53
expect : 'latest-composable' ,
54
54
} ) ;
55
55
56
+ // In this example, the policy checks sees the "hasRole" directive in the schema
57
+ // because we are using composeDirective.
56
58
const rawMessage = await cli . check ( {
57
59
sdl : /* GraphQL */ `
60
+ extend schema
61
+ @link(url: "https://specs.apollo.dev/link/v1.0")
62
+ @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@composeDirective"])
63
+ @link(url: "https://myspecs.dev/myDirective/v1.0", import: ["@hasRole"])
64
+ @composeDirective(name: "@hasRole")
65
+
58
66
scalar Unused
59
67
60
68
scalar Used
61
69
62
- directive @hasRole(role: Role!) on QUERY | MUTATION | FIELD_DEFINITION
70
+ scalar UsedInInput
63
71
64
- directive @hasRoles(roles: [ Role!] ) on QUERY | MUTATION | FIELD_DEFINITION
72
+ directive @hasRole(role: Role!) on QUERY | MUTATION | FIELD_DEFINITION
65
73
66
74
enum Role {
67
75
admin
@@ -77,7 +85,7 @@ describe('Schema policy checks', () => {
77
85
78
86
type Query {
79
87
userRole(roleID: Int!): UserRole! @hasRole(role: admin)
80
- scalar: Used
88
+ scalar(input: UsedInInput!) : Used
81
89
}
82
90
83
91
type UserRole {
@@ -92,26 +100,82 @@ describe('Schema policy checks', () => {
92
100
93
101
expect ( message ) . toContain ( `Detected 2 errors` ) ;
94
102
expect ( message . split ( '\n' ) . slice ( 1 ) ) . toEqual ( [
95
- '✖ Detected 5 errors' ,
103
+ '✖ Detected 2 errors' ,
96
104
'' ,
97
105
' - Scalar type `Unused` is unreachable. (source: policy-no-unreachable-types)' ,
98
106
' - Enum type `Permission` is unreachable. (source: policy-no-unreachable-types)' ,
99
107
'' ,
100
- 'ℹ Detected 7 changes' ,
108
+ 'ℹ Detected 9 changes' ,
101
109
'' ,
102
110
' Safe changes:' ,
103
- ' - Type Permission was added' ,
104
- ' - Type Role was added' ,
105
- ' - Type Unused was added' ,
106
- ' - Type Used was added' ,
107
- ' - Type UserRole was added' ,
108
- ' - Field scalar was added to object type Query' ,
109
- ' - Field userRole was added to object type Query' ,
111
+ ' - Type Permission was added' ,
112
+ ' - Type Role was added' ,
113
+ ' - Type Unused was added' ,
114
+ ' - Type Used was added' ,
115
+ ' - Type UsedInInput was added' ,
116
+ ' - Type UserRole was added' ,
117
+ ' - Field scalar was added to object type Query' ,
118
+ ' - Field userRole was added to object type Query' ,
119
+ ' - Directive hasRole was added' ,
110
120
'' ,
111
121
'View full report:' ,
112
122
expect . any ( String ) ,
113
123
'' ,
114
124
] ) ;
125
+
126
+ // But in this one, we are not using composeDirective, so the final compose directive
127
+ // is not visible by the policy checker, and the policy checker will not detect it.
128
+ // This is why it's being reported an unused, and also other related inputs/types.
129
+ const rawMessage2 = await cli . check ( {
130
+ sdl : /* GraphQL */ `
131
+ scalar Unused
132
+
133
+ scalar Used
134
+
135
+ scalar UsedInInput
136
+
137
+ directive @hasRole(role: Role!) on QUERY | MUTATION | FIELD_DEFINITION
138
+
139
+ enum Role {
140
+ admin
141
+ user
142
+ }
143
+
144
+ enum Permission {
145
+ read
146
+ write
147
+ create
148
+ delete
149
+ }
150
+
151
+ type Query {
152
+ userRole(roleID: Int!): UserRole! @hasRole(role: admin)
153
+ scalar(input: UsedInInput!): Used
154
+ }
155
+
156
+ type UserRole {
157
+ id: ID!
158
+ name: String!
159
+ }
160
+ ` ,
161
+ serviceName : 'c' ,
162
+ expect : 'rejected' ,
163
+ } ) ;
164
+ const message2 = stripAnsi ( rawMessage2 ) ;
165
+
166
+ expect ( message2 ) . toContain ( `Detected 4 errors` ) ;
167
+ expect ( message2 ) . toContain (
168
+ `Scalar type \`Unused\` is unreachable. (source: policy-no-unreachable-types)` ,
169
+ ) ;
170
+ expect ( message2 ) . toContain (
171
+ `Directive \`hasRole\` is unreachable. (source: policy-no-unreachable-types)` ,
172
+ ) ;
173
+ expect ( message2 ) . toContain (
174
+ `Enum type \`Role\` is unreachable. (source: policy-no-unreachable-types)` ,
175
+ ) ;
176
+ expect ( message2 ) . toContain (
177
+ `Enum type \`Permission\` is unreachable. (source: policy-no-unreachable-types)` ,
178
+ ) ;
115
179
} ) ;
116
180
117
181
it ( 'Federation project with policy with only warnings, should check only the part that was changed' , async ( ) => {
0 commit comments