Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wobe-graphql-apollo): add custom wobe context on apollo #29

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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