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

Deprecate '.start' and '.stop' methods for common package #1374

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions .changeset/chilly-zebras-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@graphql-yoga/common': minor
---

Deprecate `.start` and `.stop` in favor of more clear usage for Service Workers API which is used by Cloudflare Workers.
Now Yoga implements `EventListenerObject` which has a `handleEvent` method that is an event listener function.

NOTE: `.start` and `.stop` are going to be deprecated in the next major release.

```ts
import { createServer } from '@graphql-yoga/common'

const yoga = createServer({
//...
})

//Before: yoga.start();
self.addEventListener('fetch', yoga)

//Before: yoga.stop();
self.removeEventListener('fetch', yoga)
```
4 changes: 2 additions & 2 deletions examples/cloudflare-advanced/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const server = createServer({
async *subscribe() {
while (true) {
yield { time: new Date().toISOString() }
await new Promise((resolve) => setTimeout(resolve, 1000))
await new Promise((resolve) => setTimeout(resolve, 1000, {}))
}
},
},
Expand All @@ -102,4 +102,4 @@ const server = createServer({
},
})

server.start()
self.addEventListener('fetch', server)
2 changes: 1 addition & 1 deletion examples/cloudflare-advanced/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"outDir": "./dist",
"module": "esnext",
"target": "esnext",
"lib": ["esnext", "webworker"],
"lib": ["esnext"],
"moduleResolution": "node",
"sourceMap": true,
"skipLibCheck": true,
Expand Down
13 changes: 13 additions & 0 deletions examples/cloudflare-modules/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"outDir": "./dist",
"module": "esnext",
"target": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"sourceMap": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion examples/file-upload-nexus/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"]
"lib": ["esnext"]
}
}
2 changes: 1 addition & 1 deletion examples/file-upload/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"]
"lib": ["esnext"]
}
}
2 changes: 1 addition & 1 deletion examples/service-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { createServer } from '@graphql-yoga/common'

const server = createServer()

server.start()
self.addEventListener('fetch', server)
29 changes: 22 additions & 7 deletions packages/common/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { ExecutionResult, IResolvers, TypeSource } from '@graphql-tools/utils'
import {
GraphQLServerInject,
YogaInitialContext,
FetchEvent,
FetchAPI,
GraphQLParams,
} from './types.js'
Expand Down Expand Up @@ -205,7 +204,8 @@ export class YogaServer<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
TRootValue,
> {
> implements EventListenerObject
{
/**
* Instance of envelop
*/
Expand Down Expand Up @@ -611,7 +611,7 @@ export class YogaServer<

fetch: WindowOrWorkerGlobalScope['fetch'] = (
input: RequestInfo,
init: RequestInit,
init?: RequestInit,
) => {
let request: Request
if (typeof input === 'string') {
Expand All @@ -623,15 +623,30 @@ export class YogaServer<
}

// FetchEvent is not available in all envs
private fetchEventListener = (event: FetchEvent) =>
event.respondWith(this.handleRequest(event.request, event as any))
handleEvent = (event: FetchEvent) => {
if (!event.respondWith || !event.request) {
throw new TypeError(`Expected FetchEvent, got ${event}`)
}
const response$ = this.handleRequest(event.request, event as any)
event.respondWith(response$)
}

/**
* @deprecated
* Will be removed in next major version
* Use self.addEventListener('fetch', yogaServer) instead
*/
start() {
self.addEventListener('fetch', this.fetchEventListener as EventListener)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we also add console.warn for the depcreation warning.

self.addEventListener('fetch', this)
}

/**
* @deprecated
* Will be removed in next major version
* Use self.removeEventListener('fetch', yogaServer) instead
*/
stop() {
self.removeEventListener('fetch', this.fetchEventListener as EventListener)
self.removeEventListener('fetch', this)
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ declare global {
}
}

export type FetchEvent = Event & {
respondWith: (response: PromiseOrValue<Response>) => void
request: Request
}

export type FetchAPI = {
/**
* WHATWG compliant Request object constructor
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"experimentalDecorators": true,
"module": "node16",
"target": "es2018",
"lib": ["es6", "esnext", "es2015", "dom", "webworker"],
"lib": ["esnext", "webworker"],
"suppressImplicitAnyIndexErrors": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
Expand All @@ -35,5 +35,5 @@
"jsx": "preserve"
},
"include": ["packages"],
"exclude": ["**/dist", "**/temp", "**/.bob"]
"exclude": ["**/dist", "**/temp", "**/.bob", "**/examples"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { createServer } from '@graphql-yoga/common'

const server = createServer()

server.start()
self.addEventListener('fetch', server)
```

> You can also check a full example on our GitHub repository [here](https://github.com/dotansimha/graphql-yoga/tree/master/examples/service-worker)
Expand Down