-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graphql-ws integration adjustments and tests (V3) (#1609)
* fix integration and test * update ws integration docs * failing ws mutation * mutations over ws cb5d83d * changeset
- Loading branch information
Showing
8 changed files
with
226 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-yoga': patch | ||
--- | ||
|
||
`usePreventMutationViaGET` doesn't do assertion if it is not `YogaContext` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-yoga': patch | ||
--- | ||
|
||
Expose readonly "graphqlEndpoint" in `YogaServerInstance` |
79 changes: 79 additions & 0 deletions
79
examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { buildApp } from '../src/app.js' | ||
import WebSocket from 'ws' | ||
import { createClient } from 'graphql-ws' | ||
|
||
describe('graphql-ws example integration', () => { | ||
const app = buildApp() | ||
beforeAll(() => app.start(4000)) | ||
afterAll(() => app.stop()) | ||
|
||
it('should execute query', async () => { | ||
const client = createClient({ | ||
webSocketImpl: WebSocket, | ||
url: 'ws://localhost:4000/graphql', | ||
retryAttempts: 0, // fail right away | ||
}) | ||
|
||
const onNext = jest.fn() | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
client.subscribe( | ||
{ query: '{ hello }' }, | ||
{ | ||
next: onNext, | ||
error: reject, | ||
complete: resolve, | ||
}, | ||
) | ||
}) | ||
|
||
expect(onNext).toBeCalledWith({ data: { hello: 'world' } }) | ||
}) | ||
|
||
it('should execute mutation', async () => { | ||
const client = createClient({ | ||
webSocketImpl: WebSocket, | ||
url: 'ws://localhost:4000/graphql', | ||
retryAttempts: 0, // fail right away | ||
}) | ||
|
||
const onNext = jest.fn() | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
client.subscribe( | ||
{ query: 'mutation { dontChange }' }, | ||
{ | ||
next: onNext, | ||
error: reject, | ||
complete: resolve, | ||
}, | ||
) | ||
}) | ||
|
||
expect(onNext).toBeCalledWith({ data: { dontChange: 'didntChange' } }) | ||
}) | ||
|
||
it('should subscribe', async () => { | ||
const client = createClient({ | ||
webSocketImpl: WebSocket, | ||
url: 'ws://localhost:4000/graphql', | ||
retryAttempts: 0, // fail right away | ||
}) | ||
|
||
const onNext = jest.fn() | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
client.subscribe( | ||
{ query: 'subscription { greetings }' }, | ||
{ | ||
next: onNext, | ||
error: reject, | ||
complete: resolve, | ||
}, | ||
) | ||
}) | ||
|
||
expect(onNext).toBeCalledTimes(5) | ||
expect(onNext).toBeCalledWith({ data: { greetings: 'Hi' } }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Socket } from 'net' | ||
import { createServer } from 'http' | ||
import { WebSocketServer } from 'ws' | ||
import { createYoga, createSchema } from 'graphql-yoga' | ||
import { useServer } from 'graphql-ws/lib/use/ws' | ||
|
||
export function buildApp() { | ||
const yoga = createYoga({ | ||
schema: createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String! | ||
} | ||
type Mutation { | ||
dontChange: String! | ||
} | ||
type Subscription { | ||
greetings: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello() { | ||
return 'world' | ||
}, | ||
}, | ||
Mutation: { | ||
dontChange() { | ||
return 'didntChange' | ||
}, | ||
}, | ||
Subscription: { | ||
greetings: { | ||
async *subscribe() { | ||
for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) { | ||
yield { greetings: hi } | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
|
||
const server = createServer(yoga) | ||
const wss = new WebSocketServer({ | ||
server, | ||
path: yoga.graphqlEndpoint, | ||
}) | ||
|
||
useServer( | ||
{ | ||
execute: (args: any) => args.execute(args), | ||
subscribe: (args: any) => args.subscribe(args), | ||
onSubscribe: async (ctx, msg) => { | ||
const { schema, execute, subscribe, contextFactory, parse, validate } = | ||
yoga.getEnveloped({ | ||
...ctx, | ||
req: ctx.extra.request, | ||
socket: ctx.extra.socket, | ||
}) | ||
|
||
const args = { | ||
schema, | ||
operationName: msg.payload.operationName, | ||
document: parse(msg.payload.query), | ||
variableValues: msg.payload.variables, | ||
contextValue: await contextFactory(), | ||
execute, | ||
subscribe, | ||
} | ||
|
||
const errors = validate(args.schema, args.document) | ||
if (errors.length) return errors | ||
return args | ||
}, | ||
}, | ||
wss, | ||
) | ||
|
||
// for termination | ||
const sockets = new Set<Socket>() | ||
server.on('connection', (socket) => { | ||
sockets.add(socket) | ||
server.once('close', () => sockets.delete(socket)) | ||
}) | ||
|
||
return { | ||
start: (port: number) => | ||
new Promise<void>((resolve, reject) => { | ||
server.on('error', (err) => reject(err)) | ||
server.on('listening', () => resolve()) | ||
server.listen(port) | ||
}), | ||
stop: () => | ||
new Promise<void>((resolve) => { | ||
for (const socket of sockets) { | ||
socket.destroy() | ||
sockets.delete(socket) | ||
} | ||
server.close(() => resolve()) | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters