Skip to content

Commit 274c5bc

Browse files
author
igor.luckenkov
committed
added opts.defer: boolean to enable @defer directive
1 parent d41fff6 commit 274c5bc

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ const plugin = fp(async function (app, opts) {
190190
})
191191
}
192192

193+
if (opts.defer) {
194+
schema = extendSchema(
195+
schema,
196+
parse(`
197+
directive @defer(
198+
if: Boolean! = true
199+
label: String
200+
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
201+
`)
202+
)
203+
}
204+
193205
fastifyGraphQl.schema = schema
194206

195207
app.addHook('onReady', async function () {

test/defer.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ const { canUseIncrementalExecution } = require('../lib/util')
55

66
if (canUseIncrementalExecution) {
77
const schema = `
8-
directive @defer(
9-
if: Boolean! = true
10-
label: String
11-
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
12-
138
type Query {
149
allProducts: [Product!]!
1510
}
@@ -70,10 +65,37 @@ if (canUseIncrementalExecution) {
7065
'multipart/mixed; deferSpec=12345'
7166
]
7267

68+
test('errors with @defer when opts.defer is not true', async t => {
69+
const app = Fastify()
70+
await app.register(mercurius, { schema, resolvers, graphiql: true })
71+
72+
const res = await app.inject({
73+
method: 'POST',
74+
url: '/graphql',
75+
headers: {
76+
'content-type': 'application/json'
77+
},
78+
body: JSON.stringify({ query })
79+
})
80+
81+
t.match(res, {
82+
statusCode: 400,
83+
body: JSON.stringify({
84+
data: null,
85+
errors: [{
86+
message: 'Unknown directive "@defer".', locations: [{ line: 5, column: 25 }]
87+
}]
88+
})
89+
})
90+
91+
await app.close()
92+
t.end()
93+
})
94+
7395
for (const accept of wrongAcceptValues) {
7496
test('errors with @defer when used with wrong "accept" header', async t => {
7597
const app = Fastify()
76-
await app.register(mercurius, { schema, resolvers, graphiql: true })
98+
await app.register(mercurius, { schema, resolvers, graphiql: true, defer: true })
7799

78100
const res = await app.inject({
79101
method: 'POST',
@@ -109,7 +131,7 @@ if (canUseIncrementalExecution) {
109131
for (const accept of correctAcceptValues) {
110132
test('works with @defer when used with correct "accept" header', async t => {
111133
const app = Fastify()
112-
await app.register(mercurius, { schema, resolvers, graphiql: true })
134+
await app.register(mercurius, { schema, resolvers, graphiql: true, defer: true })
113135

114136
const res = await app.inject({
115137
method: 'POST',

0 commit comments

Comments
 (0)