Skip to content

Commit

Permalink
Merge branch 'v3' into v3-graphql-http
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/graphql-yoga/src/plugins/requestValidation/usePreventMutationViaGET.ts
  • Loading branch information
enisdenjo committed Aug 17, 2022
2 parents 2a9c48c + 74e1f83 commit db3aca3
Show file tree
Hide file tree
Showing 36 changed files with 707 additions and 533 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-kangaroos-tell.md
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`
5 changes: 5 additions & 0 deletions .changeset/seven-tomatoes-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Expose readonly "graphqlEndpoint" in `YogaServerInstance`
2 changes: 1 addition & 1 deletion benchmark/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
},
"dependencies": {
"graphql-yoga": "2.13.4",
"graphql": "16.5.0"
"graphql": "16.6.0"
}
}
6 changes: 3 additions & 3 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"version": "0.0.0",
"license": "MIT",
"dependencies": {
"@pulumi/pulumi": "3.37.2",
"@pulumi/pulumi": "3.38.0",
"@pulumi/cloudflare": "4.9.0",
"@pulumi/azure-native": "1.67.0",
"@pulumi/azure-native": "1.68.2",
"@pulumi/awsx": "0.40.0",
"@pulumi/aws": "5.10.0",
"@pulumi/docker": "3.4.0",
"@types/node": "16.11.47",
"@types/node": "16.11.49",
"typescript": "4.7.4",
"ts-node": "10.9.1"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"devDependencies": {
"@aws-cdk/assert": "2.37.1",
"@types/aws-lambda": "8.10.102",
"@types/node": "16.11.47",
"@types/node": "16.11.49",
"ts-node": "10.9.1",
"aws-cdk": "2.37.1",
"aws-cdk-lib": "2.37.1",
"esbuild": "0.15.1",
"esbuild": "0.15.5",
"typescript": "4.7.4"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"devDependencies": {
"@azure/functions": "3.2.0",
"esbuild": "0.15.1",
"esbuild": "0.15.5",
"typescript": "4.7.4",
"tslib": "2.4.0"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"dependencies": {
"graphql-yoga": "2.13.4",
"@cloudflare/workers-types": "^3.0.0",
"graphql": "16.5.0"
"graphql": "16.6.0"
},
"devDependencies": {
"wrangler": "2.0.25",
"wrangler": "2.0.26",
"typescript": "4.7.4",
"ts-loader": "9.3.1",
"webpack": "5.74.0"
Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
},
"dependencies": {
"graphql-yoga": "2.13.4",
"graphql": "16.5.0"
"graphql": "16.6.0"
},
"devDependencies": {
"typescript": "4.7.4",
"wrangler": "2.0.25"
"wrangler": "2.0.26"
}
}
4 changes: 2 additions & 2 deletions examples/fastify-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"check": "tsc --pretty --noEmit"
},
"dependencies": {
"@envelop/graphql-modules": "3.4.2",
"@envelop/graphql-modules": "3.5.0",
"@graphql-tools/load-files": "6.6.1",
"graphql-yoga": "2.13.4",
"fastify": "4.4.0",
"graphql-modules": "2.1.0",
"reflect-metadata": "0.1.13"
},
"devDependencies": {
"@types/node": "16.11.47",
"@types/node": "16.11.49",
"ts-node": "10.9.1"
}
}
2 changes: 1 addition & 1 deletion examples/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"pino-pretty": "9.0.0"
},
"devDependencies": {
"@types/node": "16.11.47",
"@types/node": "16.11.49",
"ts-node": "10.9.1"
}
}
10 changes: 5 additions & 5 deletions examples/file-upload-nextjs-pothos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
},
"dependencies": {
"graphql-yoga": "2.13.4",
"@pothos/core": "3.13.0",
"graphql": "16.5.0",
"next": "12.2.4",
"@pothos/core": "3.15.0",
"graphql": "16.6.0",
"next": "12.2.5",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"@types/react": "^17.0.0",
"eslint": "8.21.0",
"eslint-config-next": "12.2.4",
"eslint": "8.22.0",
"eslint-config-next": "12.2.5",
"typescript": "4.7.4"
}
}
2 changes: 1 addition & 1 deletion examples/file-upload-nexus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"graphql-yoga": "2.13.4",
"graphql": "16.5.0",
"graphql": "16.6.0",
"nexus": "^1.3.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/file-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"graphql-yoga": "2.13.4",
"graphql": "16.5.0",
"graphql": "16.6.0",
"ts-node": "10.9.1"
}
}
6 changes: 3 additions & 3 deletions examples/generic-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "16.11.47",
"@types/node": "16.11.49",
"cross-env": "7.0.3",
"ts-node": "10.9.1",
"ts-node-dev": "2.0.0",
"typescript": "4.7.4"
},
"dependencies": {
"@envelop/generic-auth": "4.3.2",
"@envelop/generic-auth": "4.4.0",
"graphql-yoga": "2.13.4",
"graphql": "16.5.0"
"graphql": "16.6.0"
}
}
79 changes: 79 additions & 0 deletions examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts
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' } })
})
})
104 changes: 104 additions & 0 deletions examples/graphql-ws/src/app.ts
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())
}),
}
}
Loading

0 comments on commit db3aca3

Please sign in to comment.