Skip to content

Commit

Permalink
fix(wobe-graphql-apollo): add WobeResponse and request on context (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Jun 7, 2024
1 parent b03d4b1 commit b7cc644
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/wobe-graphql-apollo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wobe-graphql-apollo",
"version": "1.0.2",
"version": "1.0.3",
"description": "Apollo GraphQL server for Wobe (official)",
"homepage": "https://wobe.dev",
"author": {
Expand Down
58 changes: 57 additions & 1 deletion packages/wobe-graphql-apollo/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,62 @@ import getPort from 'get-port'
import { WobeGraphqlApolloPlugin } from '.'

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

const wobe = new Wobe()

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

expect(context.response).toBeDefined()
expect(context.request).toBeDefined()
return 'Hello from Apollo!'
},
},
},
},
context: async (tutu) => {
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 use the graphql middleware if it's provided", async () => {
const port = await getPort()

Expand Down Expand Up @@ -84,7 +140,7 @@ describe('Wobe GraphQL Apollo plugin', () => {
},
},
},
context: async (request) => {
context: async ({ request }) => {
expect(request.method).toBe('POST')

return { tata: 'test' }
Expand Down
96 changes: 50 additions & 46 deletions packages/wobe-graphql-apollo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const WobeGraphqlApolloPlugin = async ({
}: {
options: ApolloServerOptions<any>
graphqlEndpoint?: string
context?: (request: Request) => MaybePromise<BaseContext>
context?: (options: {
request: Request
response: WobeResponse
}) => MaybePromise<BaseContext>
} & GraphQLApolloPluginOptions): Promise<WobePlugin> => {
const server = new ApolloServer({
...options,
Expand All @@ -45,77 +48,78 @@ export const WobeGraphqlApolloPlugin = async ({
await server.start()

return (wobe: Wobe) => {
const fetchEndpoint = async (request: Request) => {
const res = await server.executeHTTPGraphQLRequest({
httpGraphQLRequest: {
method: request.method,
body: await request.json(),
// @ts-expect-error
headers: request.headers,
search: getQueryString(request.url),
},
context: context ? () => context(request) as any : () => ({}),
})

if (res.body.kind === 'complete') {
const response = new Response(res.body.string, {
status: res.status ?? 200,
// @ts-expect-error
headers: res.headers,
const getResponse = async (
request: Request,
wobeResponse: WobeResponse,
) => {
const fetchEndpoint = async (request: Request) => {
const res = await server.executeHTTPGraphQLRequest({
httpGraphQLRequest: {
method: request.method,
body: await request.json(),
// @ts-expect-error
headers: request.headers,
search: getQueryString(request.url),
},
context: async () => ({
request: request,
response: wobeResponse,
...(context
? await context({ request, response: wobeResponse })
: {}),
}),
})

return response
}
if (res.body.kind === 'complete') {
const response = new Response(res.body.string, {
status: res.status ?? 200,
// @ts-expect-error
headers: res.headers,
})

return new Response()
}
return response
}

return new Response()
}

wobe.get(graphqlEndpoint, async ({ request, res: wobeResponse }) => {
if (!graphqlMiddleware) return fetchEndpoint(request)

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

return response
},
wobeResponse,
)
return response
}, wobeResponse)
}

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

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

responseAfterMiddleware.headers.set(key, value)
response.headers.set(key, value)
}

return responseAfterMiddleware
return response
})

wobe.post(graphqlEndpoint, async ({ request, res: wobeResponse }) => {
if (!graphqlMiddleware) return fetchEndpoint(request)

const responseAfterMiddleware = await graphqlMiddleware(
async () => {
const response = await fetchEndpoint(request)

return response
},
wobeResponse,
)
const response = await getResponse(request, wobeResponse)

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

responseAfterMiddleware.headers.set(key, value)
response.headers.set(key, value)
}

return responseAfterMiddleware
return response
})
}
}

0 comments on commit b7cc644

Please sign in to comment.