-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor GraphQL Server and CreateYoga to Support "api serve" with Fa…
…stify Server (#8339) * Move makeMergedSchema * refactor createGraphQLYoga * Make a Fastify plugin for graphql yoga * Set yoga options * Adds graphql fastify plugin to template * Whitespace * Adds allowGraphiQL config setting and docs * fastify gql plugin awaits * Merge in subscriptions into schema from project * Get to green on tests * Fix subscriptions module api import * Tests makeSubscriptions * Fastify graphql plugin needed graphqlserver package * remove unneeded comments * Update docs/docs/graphql.md * Adds HookHandlerDoneFunction * Update packages/cli/src/commands/experimental/templates/server.ts.template Co-authored-by: Dominic Saadi <[email protected]> --------- Co-authored-by: Dominic Saadi <[email protected]>
- Loading branch information
1 parent
cb73537
commit b28669e
Showing
31 changed files
with
705 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { FastifyInstance, HookHandlerDoneFunction } from 'fastify' | ||
|
||
import type { GraphQLYogaOptions } from '@redwoodjs/graphql-server' | ||
import { createGraphQLYoga } from '@redwoodjs/graphql-server' | ||
/** | ||
* Encapsulates the routes | ||
* @param {FastifyInstance} fastify Encapsulated Fastify Instance | ||
* @param {Object} options plugin options, refer to https://www.fastify.io/docs/latest/Reference/Plugins/#plugin-options | ||
*/ | ||
export async function redwoodFastifyGraphQLServer( | ||
fastify: FastifyInstance, | ||
options: GraphQLYogaOptions, | ||
done: HookHandlerDoneFunction | ||
) { | ||
try { | ||
const { yoga } = createGraphQLYoga(options) | ||
|
||
fastify.route({ | ||
url: yoga.graphqlEndpoint, | ||
method: ['GET', 'POST', 'OPTIONS'], | ||
handler: async (req, reply) => { | ||
const response = await yoga.handleNodeRequest(req, { | ||
req, | ||
reply, | ||
}) | ||
|
||
for (const [name, value] of response.headers) { | ||
reply.header(name, value) | ||
} | ||
|
||
reply.status(response.status) | ||
reply.send(response.body) | ||
|
||
return reply | ||
}, | ||
}) | ||
|
||
fastify.ready(() => { | ||
console.log(`GraphQL Yoga Server endpoint at ${yoga.graphqlEndpoint}`) | ||
}) | ||
|
||
done() | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/graphql-server/src/__tests__/makeSubscriptions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import gql from 'graphql-tag' | ||
|
||
import { makeSubscriptions } from '../subscriptions/makeSubscriptions' | ||
const countdownSchema = gql` | ||
type Subscription { | ||
countdown(from: Int!, interval: Int!): Int! | ||
} | ||
` | ||
|
||
const newMessageSchema = gql` | ||
type Message { | ||
from: String | ||
body: String | ||
} | ||
type Subscription { | ||
newMessage(roomId: ID!): Message! | ||
} | ||
` | ||
describe('Should map subscription globs to defined structure correctly', () => { | ||
it('Should map a subscribe correctly', async () => { | ||
// Mocking what our import-dir plugin would do | ||
const subscriptionFiles = { | ||
countdown_subscription: { | ||
schema: countdownSchema, | ||
countdown: { | ||
async *subscribe(_, { from, interval }) { | ||
for (let i = from; i >= 0; i--) { | ||
await new Promise((resolve) => | ||
setTimeout(resolve, interval ?? 1000) | ||
) | ||
yield { countdown: i } | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const [countdownSubscription] = makeSubscriptions(subscriptionFiles) | ||
|
||
expect(countdownSubscription.schema.kind).toBe('Document') | ||
expect(countdownSubscription.name).toBe('countdown') | ||
expect(countdownSubscription.resolvers.subscribe).toBeDefined() | ||
expect(countdownSubscription.resolvers.resolve).not.toBeDefined() | ||
}) | ||
|
||
it('Should map a subscribe and resolve correctly', async () => { | ||
// Mocking what our import-dir plugin would do | ||
const subscriptionFiles = { | ||
newMessage_subscription: { | ||
schema: newMessageSchema, | ||
newMessage: { | ||
subscribe: (_, { roomId }) => { | ||
return roomId | ||
}, | ||
resolve: (payload) => { | ||
return payload | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const [newMessageSubscription] = makeSubscriptions(subscriptionFiles) | ||
|
||
expect(newMessageSubscription.schema.kind).toBe('Document') | ||
expect(newMessageSubscription.name).toBe('newMessage') | ||
expect(newMessageSubscription.resolvers.subscribe).toBeDefined() | ||
expect(newMessageSubscription.resolvers.resolve).toBeDefined() | ||
}) | ||
}) |
Oops, something went wrong.