Skip to content

Commit c2019b4

Browse files
committed
better example
1 parent a9249e5 commit c2019b4

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

e2e/operation-field-permissions/gateway.config.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { useOperationFieldPermissions } from '@envelop/operation-field-permissions';
2-
import { defineConfig } from '@graphql-hive/gateway';
2+
import { defineConfig, GatewayContext } from '@graphql-hive/gateway';
33

44
export const gatewayConfig = defineConfig({
55
plugins: () => [
66
useOperationFieldPermissions({
7-
getPermissions() {
8-
return new Set(['Query.allowed']);
7+
getPermissions(ctx: GatewayContext) {
8+
const auth = ctx.request.headers.get('authorization');
9+
if (
10+
auth ===
11+
'Bearer TOKEN' /** NOTE: proper token validity check goes here */
12+
) {
13+
// allow all fields
14+
return new Set(['*']);
15+
}
16+
// allow only introspection
17+
return new Set(['Query.registrationOpen']);
918
},
1019
}) as any, // TODO: fix generic in envelop
1120
],

e2e/operation-field-permissions/operation-field-permissions.e2e.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect, it } from 'vitest';
33

44
const { gateway, service } = createTenv(__dirname);
55

6-
it('should disallow disallowed', async () => {
6+
it('should allow checking registration but disallow "me" when not authenticated', async () => {
77
const { execute } = await gateway({
88
supergraph: {
99
with: 'mesh',
@@ -15,7 +15,25 @@ it('should disallow disallowed', async () => {
1515
execute({
1616
query: /* GraphQL */ `
1717
{
18-
disallowed
18+
registrationOpen
19+
}
20+
`,
21+
}),
22+
).resolves.toMatchInlineSnapshot(`
23+
{
24+
"data": {
25+
"registrationOpen": false,
26+
},
27+
}
28+
`);
29+
30+
await expect(
31+
execute({
32+
query: /* GraphQL */ `
33+
{
34+
me {
35+
name
36+
}
1937
}
2038
`,
2139
}),
@@ -30,33 +48,52 @@ it('should disallow disallowed', async () => {
3048
"line": 3,
3149
},
3250
],
33-
"message": "Insufficient permissions for selecting 'Query.disallowed'.",
51+
"message": "Insufficient permissions for selecting 'Query.me'.",
52+
},
53+
{
54+
"locations": [
55+
{
56+
"column": 13,
57+
"line": 4,
58+
},
59+
],
60+
"message": "Insufficient permissions for selecting 'User.name'.",
3461
},
3562
],
3663
}
3764
`);
3865
});
3966

40-
it('should allow allowed', async () => {
67+
it('should allow "me" when authenticated', async () => {
4168
const { execute } = await gateway({
4269
supergraph: {
4370
with: 'mesh',
4471
services: [await service('users')],
4572
},
73+
pipeLogs: 'gw.log',
4674
});
4775

4876
await expect(
4977
execute({
5078
query: /* GraphQL */ `
5179
{
52-
allowed
80+
registrationOpen
81+
me {
82+
name
83+
}
5384
}
5485
`,
86+
headers: {
87+
authorization: 'Bearer TOKEN',
88+
},
5589
}),
5690
).resolves.toMatchInlineSnapshot(`
5791
{
5892
"data": {
59-
"allowed": "cool",
93+
"me": {
94+
"name": "John",
95+
},
96+
"registrationOpen": false,
6097
},
6198
}
6299
`);

e2e/operation-field-permissions/services/users.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ createServer(
1010
schema: createSchema<any>({
1111
typeDefs: /* GraphQL */ `
1212
type Query {
13-
allowed: String!
14-
disallowed: String!
13+
registrationOpen: Boolean!
14+
me: User!
15+
}
16+
type User {
17+
name: String!
1518
}
1619
`,
1720
resolvers: {
1821
Query: {
19-
allowed: () => 'cool',
20-
disallowed: () => 'very not cool',
22+
registrationOpen: () => false,
23+
me: () => ({ name: 'John' }),
2124
},
2225
},
2326
}),

0 commit comments

Comments
 (0)