Skip to content

Commit

Permalink
added opts.defer: boolean to enable @defer directive
Browse files Browse the repository at this point in the history
  • Loading branch information
igrlk committed Oct 26, 2022
1 parent d41fff6 commit 92f04ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ const plugin = fp(async function (app, opts) {
})
}

if (opts.defer) {
schema = extendSchema(
schema,
parse(`
directive @defer(
if: Boolean! = true
label: String
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
`)
)
}

fastifyGraphQl.schema = schema

app.addHook('onReady', async function () {
Expand Down
36 changes: 29 additions & 7 deletions test/defer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ const { canUseIncrementalExecution } = require('../lib/util')

if (canUseIncrementalExecution) {
const schema = `
directive @defer(
if: Boolean! = true
label: String
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
type Query {
allProducts: [Product!]!
}
Expand Down Expand Up @@ -70,10 +65,37 @@ if (canUseIncrementalExecution) {
'multipart/mixed; deferSpec=12345'
]

test('errors with @defer when opts.defer is not true', async t => {
const app = Fastify()
await app.register(mercurius, { schema, resolvers, graphiql: true })

const res = await app.inject({
method: 'POST',
url: '/graphql',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query })
})

t.match(res, {
statusCode: 400,
body: JSON.stringify({
data: null,
errors: [{
message: 'Unknown directive "@defer".', locations: [{ line: 5, column: 25 }]
}]
})
})

await app.close()
t.end()
})

for (const accept of wrongAcceptValues) {
test('errors with @defer when used with wrong "accept" header', async t => {
const app = Fastify()
await app.register(mercurius, { schema, resolvers, graphiql: true })
await app.register(mercurius, { schema, resolvers, graphiql: true, defer: true })

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

const res = await app.inject({
method: 'POST',
Expand Down

0 comments on commit 92f04ad

Please sign in to comment.