Skip to content

Commit

Permalink
feat: add custom wobe context on apollo (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Jul 23, 2024
1 parent 0cb58c6 commit 82041ce
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 27 deletions.
63 changes: 61 additions & 2 deletions packages/wobe-graphql-apollo/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,65 @@ import getPort from 'get-port'
import { WobeGraphqlApolloPlugin } from '.'

describe('Wobe GraphQL Apollo plugin', () => {
it('should have custom wobe context in graphql context', async () => {
const port = await getPort()

const wobe = new Wobe<{ customType: string }>().beforeHandler((ctx) => {
ctx.customType = 'test'
})

wobe.usePlugin(
await WobeGraphqlApolloPlugin({
options: {
typeDefs: `#graphql
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: (_, __, context) => {
context.res.setCookie('before', 'before')

expect(context.res).toBeDefined()
expect(context.request).toBeDefined()
expect(context.customType).toEqual('test')
return 'Hello from Apollo!'
},
},
},
},
context: async () => {
return { tata: 'test' }
},
}),
)

wobe.listen(port)

const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query {
hello
}
`,
}),
})

expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toBe('before=before;')
expect(await res.json()).toEqual({
data: { hello: 'Hello from Apollo!' },
})

wobe.stop()
})

it('should have WobeResponse in graphql context', async () => {
const port = await getPort()

Expand All @@ -20,9 +79,9 @@ describe('Wobe GraphQL Apollo plugin', () => {
resolvers: {
Query: {
hello: (_, __, context) => {
context.response.setCookie('before', 'before')
context.res.setCookie('before', 'before')

expect(context.response).toBeDefined()
expect(context.res).toBeDefined()
expect(context.request).toBeDefined()
return 'Hello from Apollo!'
},
Expand Down
47 changes: 22 additions & 25 deletions packages/wobe-graphql-apollo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
ApolloServerPluginLandingPageLocalDefault,
ApolloServerPluginLandingPageProductionDefault,
} from '@apollo/server/plugin/landingPage/default'
import type { Wobe, MaybePromise, WobePlugin, WobeResponse } from 'wobe'
import type {
Wobe,
MaybePromise,
WobePlugin,
WobeResponse,
Context,
} from 'wobe'

const getQueryString = (url: string) => url.slice(url.indexOf('?', 11) + 1)

Expand All @@ -22,14 +28,11 @@ export const WobeGraphqlApolloPlugin = async ({
options,
graphqlEndpoint = '/graphql',
graphqlMiddleware,
context,
context: apolloContext,
}: {
options: ApolloServerOptions<any>
graphqlEndpoint?: string
context?: (options: {
request: Request
response: WobeResponse
}) => MaybePromise<BaseContext>
context?: (options: Context) => MaybePromise<BaseContext>
} & GraphQLApolloPluginOptions): Promise<WobePlugin> => {
const server = new ApolloServer({
...options,
Expand All @@ -47,11 +50,8 @@ export const WobeGraphqlApolloPlugin = async ({

await server.start()

return (wobe: Wobe) => {
const getResponse = async (
request: Request,
wobeResponse: WobeResponse,
) => {
return (wobe: Wobe<unknown>) => {
const getResponse = async (context: Context) => {
const fetchEndpoint = async (request: Request) => {
const res = await server.executeHTTPGraphQLRequest({
httpGraphQLRequest: {
Expand All @@ -65,11 +65,8 @@ export const WobeGraphqlApolloPlugin = async ({
search: getQueryString(request.url),
},
context: async () => ({
request: request,
response: wobeResponse,
...(context
? await context({ request, response: wobeResponse })
: {}),
...context,
...(apolloContext ? await apolloContext(context) : {}),
}),
})

Expand All @@ -86,19 +83,19 @@ export const WobeGraphqlApolloPlugin = async ({
return new Response()
}

if (!graphqlMiddleware) return fetchEndpoint(request)
if (!graphqlMiddleware) return fetchEndpoint(context.request)

return graphqlMiddleware(async () => {
const response = await fetchEndpoint(request)
const response = await fetchEndpoint(context.request)

return response
}, wobeResponse)
}, context.res)
}

wobe.get(graphqlEndpoint, async ({ request, res: wobeResponse }) => {
const response = await getResponse(request, wobeResponse)
wobe.get(graphqlEndpoint, async (context) => {
const response = await getResponse(context)

for (const [key, value] of wobeResponse.headers.entries()) {
for (const [key, value] of context.res.headers.entries()) {
if (key === 'set-cookie') {
response.headers.append('set-cookie', value)
continue
Expand All @@ -110,10 +107,10 @@ export const WobeGraphqlApolloPlugin = async ({
return response
})

wobe.post(graphqlEndpoint, async ({ request, res: wobeResponse }) => {
const response = await getResponse(request, wobeResponse)
wobe.post(graphqlEndpoint, async (context) => {
const response = await getResponse(context)

for (const [key, value] of wobeResponse.headers.entries()) {
for (const [key, value] of context.res.headers.entries()) {
if (key === 'set-cookie') {
response.headers.append('set-cookie', value)
continue
Expand Down

0 comments on commit 82041ce

Please sign in to comment.