Skip to content

Commit b970110

Browse files
committed
SOFA Plugin
1 parent af0db63 commit b970110

File tree

5 files changed

+172
-7
lines changed

5 files changed

+172
-7
lines changed

packages/graphql-yoga/src/plugins/types.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Plugin as EnvelopPlugin, PromiseOrValue } from '@envelop/core'
22
import { ExecutionResult } from 'graphql'
3+
import { YogaServer } from '../server.js'
34
import {
45
ExecutionPatchResult,
56
FetchAPI,
@@ -9,9 +10,14 @@ import {
910

1011
export type Plugin<
1112
PluginContext extends Record<string, any> = {},
12-
TServerContext = {},
13-
TUserContext = {},
13+
TServerContext extends Record<string, any> = {},
14+
TUserContext extends Record<string, any> = {},
1415
> = EnvelopPlugin<PluginContext> & {
16+
/**
17+
* Use this hook with your own risk. It is still experimental and may change in the future.
18+
* @internal
19+
*/
20+
onYogaInit?: OnYogaInitHook<TServerContext>
1521
/**
1622
* Use this hook with your own risk. It is still experimental and may change in the future.
1723
* @internal
@@ -39,6 +45,15 @@ export type Plugin<
3945
onResponse?: OnResponseHook<TServerContext>
4046
}
4147

48+
export type OnYogaInitHook<TServerContext extends Record<string, any>> = (
49+
payload: OnYogaInitEventPayload<TServerContext>,
50+
) => void
51+
52+
export type OnYogaInitEventPayload<TServerContext extends Record<string, any>> =
53+
{
54+
yoga: YogaServer<TServerContext, any, any>
55+
}
56+
4257
export type OnRequestHook<TServerContext> = (
4358
payload: OnRequestEventPayload<TServerContext>,
4459
) => PromiseOrValue<void>

packages/graphql-yoga/src/server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ export class YogaServer<
417417
this.onResponseHooks = []
418418
for (const plugin of this.plugins) {
419419
if (plugin) {
420+
if (plugin.onYogaInit) {
421+
plugin.onYogaInit({
422+
yoga: this,
423+
})
424+
}
420425
if (plugin.onRequest) {
421426
this.onRequestHooks.push(plugin.onRequest)
422427
}

packages/plugins/sofa/package.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "@graphql-yoga/plugin-sofa",
3+
"version": "0.0.0",
4+
"description": "",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/dotansimha/graphql-yoga.git",
8+
"directory": "packages/plugins/sofa"
9+
},
10+
"main": "dist/cjs/index.js",
11+
"module": "dist/esm/index.js",
12+
"scripts": {
13+
"check": "tsc --pretty --noEmit"
14+
},
15+
"keywords": [
16+
"graphql",
17+
"server",
18+
"api",
19+
"graphql-server"
20+
],
21+
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
22+
"license": "MIT",
23+
"buildOptions": {
24+
"input": "./src/index.ts"
25+
},
26+
"exports": {
27+
".": {
28+
"require": {
29+
"types": "./dist/typings/index.d.cts",
30+
"default": "./dist/cjs/index.js"
31+
},
32+
"import": {
33+
"types": "./dist/typings/index.d.ts",
34+
"default": "./dist/esm/index.js"
35+
},
36+
"default": {
37+
"types": "./dist/typings/index.d.ts",
38+
"default": "./dist/esm/index.js"
39+
}
40+
},
41+
"./package.json": "./package.json"
42+
},
43+
"typings": "dist/typings/index.d.ts",
44+
"typescript": {
45+
"definition": "dist/typings/index.d.ts"
46+
},
47+
"publishConfig": {
48+
"directory": "dist",
49+
"access": "public"
50+
},
51+
"dependencies": {
52+
"sofa-api": "0.12.0-alpha.0"
53+
},
54+
"peerDependencies": {
55+
"graphql": "^15.2.0 || ^16.0.0",
56+
"graphql-yoga": "^2.13.11"
57+
},
58+
"type": "module"
59+
}

packages/plugins/sofa/src/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { useSofa as createSofaHandler } from 'sofa-api'
2+
import { Plugin, YogaInitialContext, YogaServerInstance } from 'graphql-yoga'
3+
4+
type SofaHandler = ReturnType<typeof createSofaHandler>
5+
type SofaHandlerConfig = Parameters<typeof createSofaHandler>[0]
6+
7+
export type SofaPluginConfig = Omit<
8+
SofaHandlerConfig,
9+
'schema' | 'context' | 'execute' | 'subscribe'
10+
>
11+
12+
export function useSofa(config: SofaPluginConfig): Plugin {
13+
let sofaHandler: SofaHandler
14+
const envelopedByRequest = new WeakMap<
15+
any,
16+
ReturnType<YogaServerInstance<any, any, any>['getEnveloped']>
17+
>()
18+
let getEnveloped: YogaServerInstance<any, any, any>['getEnveloped']
19+
return {
20+
onYogaInit({ yoga }) {
21+
getEnveloped = yoga.getEnveloped
22+
},
23+
onSchemaChange(onSchemaChangeEventPayload) {
24+
sofaHandler = createSofaHandler({
25+
schema: onSchemaChangeEventPayload.schema,
26+
async context(serverContext: YogaInitialContext) {
27+
const enveloped = getEnveloped(serverContext)
28+
const contextValue = await enveloped.contextFactory()
29+
envelopedByRequest.set(serverContext.request, enveloped)
30+
return contextValue
31+
},
32+
execute(args) {
33+
const contextValue = args.contextValue as YogaInitialContext
34+
const enveloped = envelopedByRequest.get(contextValue.request)
35+
if (!enveloped) {
36+
throw new TypeError('Illegal invocation.')
37+
}
38+
return enveloped.execute(args)
39+
},
40+
subscribe(args) {
41+
const contextValue = args.contextValue as YogaInitialContext
42+
const enveloped = envelopedByRequest.get(contextValue.request)
43+
if (!enveloped) {
44+
throw new TypeError('Illegal invocation.')
45+
}
46+
return enveloped.subscribe(args)
47+
},
48+
...config,
49+
})
50+
},
51+
async onRequest({ request, serverContext, endResponse }) {
52+
const response = await sofaHandler.handle(request, serverContext)
53+
if (response) {
54+
endResponse(response)
55+
}
56+
},
57+
}
58+
}

yarn.lock

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5883,7 +5883,7 @@
58835883
"@webassemblyjs/ast" "1.11.1"
58845884
"@xtuc/long" "4.2.2"
58855885

5886-
"@whatwg-node/fetch@0.4.3", "@whatwg-node/fetch@^0.4.0", "@whatwg-node/fetch@^0.4.2":
5886+
"@whatwg-node/fetch@0.4.3", "@whatwg-node/fetch@^0.4.0", "@whatwg-node/fetch@^0.4.2", "@whatwg-node/fetch@^0.4.3":
58875887
version "0.4.3"
58885888
resolved "https://registry.yarnpkg.com/@whatwg-node/fetch/-/fetch-0.4.3.tgz#6c95c039f0912bbcb1af8e5c496104bbeab242d2"
58895889
integrity sha512-+NzflVRaWl48Jdjq7FOxkmb8wLpSWtk6XKAEMfr/yDOqJS7HWxA+Rc5rTVqh2IRi6QZJyKGoaGKjOAqrGq07nA==
@@ -5906,6 +5906,14 @@
59065906
"@whatwg-node/fetch" "^0.4.0"
59075907
tslib "^2.3.1"
59085908

5909+
"@whatwg-node/server@^0.4.1":
5910+
version "0.4.1"
5911+
resolved "https://registry.yarnpkg.com/@whatwg-node/server/-/server-0.4.1.tgz#04d3ac95eafbb8b6ab19f44fc7150a34176ef0c1"
5912+
integrity sha512-v8Xxo32AZPcw/NBdFjMULU/0LJpwiHSKtCu7mHjGYhAvTEmhJ/YPbRVvGUMaiu1GcS86c4E2BD3tey0hM2r/ww==
5913+
dependencies:
5914+
"@whatwg-node/fetch" "^0.4.0"
5915+
tslib "^2.3.1"
5916+
59095917
"@wry/context@^0.6.0":
59105918
version "0.6.1"
59115919
resolved "https://registry.yarnpkg.com/@wry/context/-/context-0.6.1.tgz#c3c29c0ad622adb00f6a53303c4f965ee06ebeb2"
@@ -6123,7 +6131,7 @@ ansi-align@^3.0.0:
61236131
dependencies:
61246132
string-width "^4.1.0"
61256133

6126-
ansi-colors@^4.1.1, ansi-colors@^4.1.3:
6134+
ansi-colors@4.1.3, ansi-colors@^4.1.1, ansi-colors@^4.1.3:
61276135
version "4.1.3"
61286136
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.3.tgz#37611340eb2243e70cc604cad35d63270d48781b"
61296137
integrity sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==
@@ -12901,6 +12909,11 @@ iterall@^1.3.0:
1290112909
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
1290212910
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==
1290312911

12912+
itty-router@^2.6.1:
12913+
version "2.6.1"
12914+
resolved "https://registry.yarnpkg.com/itty-router/-/itty-router-2.6.1.tgz#eecb59fa25b5f6f519276cc5bcaaa1cb341d5fee"
12915+
integrity sha512-l9gxWe5TOLUESYnBn85Jxd6tIZLWdRX5YKkHIBfSgbNQ7UFPNUGuWihRV+LlEbfJJIzgLmhwAbaWRi5yWJm8kg==
12916+
1290412917
jest-changed-files@^29.0.0:
1290512918
version "29.0.0"
1290612919
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-29.0.0.tgz#aa238eae42d9372a413dd9a8dadc91ca1806dce0"
@@ -16353,7 +16366,7 @@ parallel-transform@^1.2.0:
1635316366
inherits "^2.0.3"
1635416367
readable-stream "^2.1.5"
1635516368

16356-
param-case@^3.0.4:
16369+
param-case@3.0.4, param-case@^3.0.4:
1635716370
version "3.0.4"
1635816371
resolved "https://registry.yarnpkg.com/param-case/-/param-case-3.0.4.tgz#7d17fe4aa12bde34d4a77d91acfb6219caad01c5"
1635916372
integrity sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A==
@@ -18553,6 +18566,21 @@ socks@^2.6.2:
1855318566
ip "^1.1.5"
1855418567
smart-buffer "^4.2.0"
1855518568

18569+
sofa-api@0.12.0-alpha.0:
18570+
version "0.12.0-alpha.0"
18571+
resolved "https://registry.yarnpkg.com/sofa-api/-/sofa-api-0.12.0-alpha.0.tgz#f3cace18d27f6df96709b4e127981b3b28e0bf0f"
18572+
integrity sha512-caR3WGCDbIAR7Rc9zQhSVnXFV6rU9AWsCdaENHza3TastpuEun+2czZNIJcyNl29mfA+gj+e1PExI8xwiXmLPQ==
18573+
dependencies:
18574+
"@graphql-tools/utils" "8.12.0"
18575+
"@whatwg-node/fetch" "^0.4.3"
18576+
"@whatwg-node/server" "^0.4.1"
18577+
ansi-colors "4.1.3"
18578+
itty-router "^2.6.1"
18579+
param-case "3.0.4"
18580+
title-case "3.0.3"
18581+
tslib "2.4.0"
18582+
uuid "8.3.2"
18583+
1855618584
sonic-boom@^3.0.0:
1855718585
version "3.0.0"
1855818586
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-3.0.0.tgz#235119a6606e2646919a27d83ef687f2ba6c0fba"
@@ -19501,7 +19529,7 @@ tiny-warning@^1.0.3:
1950119529
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
1950219530
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==
1950319531

19504-
title-case@^3.0.3:
19532+
title-case@3.0.3, title-case@^3.0.3:
1950519533
version "3.0.3"
1950619534
resolved "https://registry.yarnpkg.com/title-case/-/title-case-3.0.3.tgz#bc689b46f02e411f1d1e1d081f7c3deca0489982"
1950719535
integrity sha512-e1zGYRvbffpcHIrnuqT0Dh+gEJtDaxDSoG4JAIpq4oDFyooziLBIiYQv0GBT4FUAnUop5uZ1hiIAj7oAF6sOCA==
@@ -20350,7 +20378,7 @@ uuid@3.3.2:
2035020378
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
2035120379
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==
2035220380

20353-
uuid@^8.0.0, uuid@^8.3.2:
20381+
uuid@8.3.2, uuid@^8.0.0, uuid@^8.3.2:
2035420382
version "8.3.2"
2035520383
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
2035620384
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

0 commit comments

Comments
 (0)