Skip to content

Commit 9fac700

Browse files
committed
ok
1 parent 17d3e16 commit 9fac700

File tree

1 file changed

+77
-13
lines changed

1 file changed

+77
-13
lines changed

integration-tests/tests/api/policy/policy-check.spec.ts

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const createPolicy = (level: RuleInstanceSeverityLevel): SchemaPolicyInpu
1717

1818
describe('Schema policy checks', () => {
1919
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 () => {
2121
const policyObject: SchemaPolicyInput = {
2222
rules: [
2323
{
@@ -53,15 +53,23 @@ describe('Schema policy checks', () => {
5353
expect: 'latest-composable',
5454
});
5555

56+
// In this example, the policy checks sees the "hasRole" directive in the schema
57+
// because we are using composeDirective.
5658
const rawMessage = await cli.check({
5759
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+
5866
scalar Unused
5967
6068
scalar Used
6169
62-
directive @hasRole(role: Role!) on QUERY | MUTATION | FIELD_DEFINITION
70+
scalar UsedInInput
6371
64-
directive @hasRoles(roles: [Role!]) on QUERY | MUTATION | FIELD_DEFINITION
72+
directive @hasRole(role: Role!) on QUERY | MUTATION | FIELD_DEFINITION
6573
6674
enum Role {
6775
admin
@@ -77,7 +85,7 @@ describe('Schema policy checks', () => {
7785
7886
type Query {
7987
userRole(roleID: Int!): UserRole! @hasRole(role: admin)
80-
scalar: Used
88+
scalar(input: UsedInInput!): Used
8189
}
8290
8391
type UserRole {
@@ -92,26 +100,82 @@ describe('Schema policy checks', () => {
92100

93101
expect(message).toContain(`Detected 2 errors`);
94102
expect(message.split('\n').slice(1)).toEqual([
95-
'✖ Detected 5 errors',
103+
'✖ Detected 2 errors',
96104
'',
97105
' - Scalar type `Unused` is unreachable. (source: policy-no-unreachable-types)',
98106
' - Enum type `Permission` is unreachable. (source: policy-no-unreachable-types)',
99107
'',
100-
'ℹ Detected 7 changes',
108+
'ℹ Detected 9 changes',
101109
'',
102110
' 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',
110120
'',
111121
'View full report:',
112122
expect.any(String),
113123
'',
114124
]);
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+
);
115179
});
116180

117181
it('Federation project with policy with only warnings, should check only the part that was changed', async () => {

0 commit comments

Comments
 (0)