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

fix(wobe-graphql-apollo): add WobeResponse and request on context #23

Merged
merged 1 commit into from
Jun 7, 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
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
})
}
}