Skip to content

Commit

Permalink
feat: add handler for manual GraphQL route setup
Browse files Browse the repository at this point in the history
- Use `handler` for setting up GraphQL routes internally in the bundler.
- The `handler` can be used manually, providing flexibility for cases where `.pylon/index.js` is not suitable (e.g., when mocking dependencies).
- Using the `handler` allows for mocking since `.pylon/index.js` bundles all code, making mocking impossible.
- GraphQL is now handled at the root path instead of `/graphql`, enabling full use of Yoga features. The `graphqlEndpoint` is still `/graphql/`
- Lay the foundation for future configuration options via `export config: PylonConfig = {}`.
  • Loading branch information
schettn committed Nov 25, 2024
1 parent 80d3d82 commit ec275a8
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 147 deletions.
30 changes: 30 additions & 0 deletions docs/pages/docs/guides/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,33 @@ const MOCKED_ENV = {
```

</Callout>

### Optional: Mocking Dependencies

In some cases, you might want to mock certain files or modules during testing. This cannot be done with the `.pylon/index.js` file because it is a bundled output. Instead, you can directly import and use the `./src/index.ts` file. This approach allows you to mock dependencies and have more control over your tests.

However, when using `./src/index.ts` directly, you need to manually set up the `handler` to handle GraphQL requests.

```typescript
import {handler} from '@getcronit/pylon'

import app, {graphql} from './src/index'

app.use(handler({graphql}))

... your tests here ...
```

This setup allows you to directly import and use the `./src/index.ts` file in your tests, giving you more flexibility and control over your test environment.

If you use the `config` export in your `./src/index.ts` file, you can also pass it to the `handler` function:

```typescript
import {handler} from '@getcronit/pylon'

import app, {graphql, config} from './src/index'

app.use(handler({graphql, config}))

... your tests here ...
```
6 changes: 5 additions & 1 deletion examples/bun-testing/pylon.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {describe, expect, test} from 'bun:test'

// Make sure to run `bun run build` before running this test
import app from './.pylon/index'
import {handler} from '@getcronit/pylon'

import app, {graphql} from './src/index'

app.use(handler({graphql}))

describe('GraphQL API', () => {
test('Query.hello', async () => {
Expand Down
66 changes: 39 additions & 27 deletions packages/pylon-builder/src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export class Bundler {
const buildOnce = async () => {
const startTime = Date.now()

const {typeDefs, resolvers: baseResolvers} = options.getBuildDefs()
const {typeDefs, resolvers} = options.getBuildDefs()

const preparedResolvers = prepareObjectInjection(resolvers)

const injectCodePlugin: Plugin = {
name: 'inject-code',
Expand All @@ -56,34 +58,22 @@ export class Bundler {
contents:
contents +
`
import {graphqlHandler} from "@getcronit/pylon"
app.use('/graphql', async c => {
const typeDefs = ${JSON.stringify(typeDefs)}
const resolvers = {
...graphql,
...${prepareObjectInjection(baseResolvers)}
}
import {handler as __internalPylonHandler} from "@getcronit/pylon"
let pylonConfig = undefined
let __internalPylonConfig = undefined
try {
pylonConfig = config
} catch {
// config is not declared, pylonConfig remains undefined
}
let exCtx = undefined
try {
exCtx = c.executionCtx
} catch (e) {}
return graphqlHandler(c)({
typeDefs,
resolvers,
config: pylonConfig
}).fetch(c.req.raw, c.env, exCtx || {})
})
try {
__internalPylonConfig = config
} catch {
// config is not declared, pylonConfig remains undefined
}
app.use(__internalPylonHandler({
typeDefs: ${JSON.stringify(typeDefs)},
graphql,
resolvers: ${preparedResolvers},
config: __internalPylonConfig
}))
`
}
}
Expand Down Expand Up @@ -137,6 +127,28 @@ export class Bundler {
0
)

// Write the typeDefs to a file
const typeDefsPath = path.join(
process.cwd(),
this.outputDir,
'schema.graphql'
)

await fs.promises.writeFile(typeDefsPath, typeDefs)

// Write base resolvers to a file

const resolversPath = path.join(
process.cwd(),
this.outputDir,
'resolvers.js'
)

await fs.promises.writeFile(
resolversPath,
`export const resolvers = ${preparedResolvers}`
)

return {
totalFiles,
totalSize,
Expand Down
116 changes: 0 additions & 116 deletions packages/pylon/src/app/handler/graphql-handler.ts

This file was deleted.

Loading

1 comment on commit ec275a8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for pylon-docs ready!

✅ Preview
https://pylon-docs-mmd5yrucm-schettns-projects.vercel.app

Built with commit ec275a8.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.