-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v3' into v3-graphql-http
# Conflicts: # packages/graphql-yoga/src/plugins/requestValidation/usePreventMutationViaGET.ts
- Loading branch information
Showing
36 changed files
with
707 additions
and
533 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` |
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 |
---|---|---|
|
@@ -9,6 +9,6 @@ | |
}, | ||
"dependencies": { | ||
"graphql-yoga": "2.13.4", | ||
"graphql": "16.5.0" | ||
"graphql": "16.6.0" | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
}, | ||
"dependencies": { | ||
"graphql-yoga": "2.13.4", | ||
"graphql": "16.5.0", | ||
"graphql": "16.6.0", | ||
"ts-node": "10.9.1" | ||
} | ||
} |
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
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()) | ||
}), | ||
} | ||
} |
Oops, something went wrong.