Skip to content

Commit

Permalink
feat(anyware): adjust signature for fetch (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Sep 17, 2024
1 parent d452c5e commit 507637d
Show file tree
Hide file tree
Showing 54 changed files with 547 additions and 168 deletions.
18 changes: 0 additions & 18 deletions examples/40_other/anyware.ts

This file was deleted.

26 changes: 26 additions & 0 deletions examples/50_anyware/anyware_jump-start__jump-start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This example shows how you can jump start your anyware into any hook.
* This is more succinct than having to manually write each hook execution
* until your reach your desired one.
*/
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Atlas })
// Notice how we **start** with the `exchange` hook, skipping the `encode` and `pack` hooks.
.anyware(async ({ exchange }) => {
// ^^^^^^^^
const mergedHeaders = new Headers(exchange.input.request.headers)
mergedHeaders.set(`X-Custom-Header`, `123`)

const { unpack } = await exchange({
input: {
...exchange.input,
headers: mergedHeaders,
},
})
const { decode } = await unpack()
const result = await decode()
return result
})
23 changes: 23 additions & 0 deletions examples/50_anyware/anyware_short-circuit__short-circuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* This example shows how you can short circuit your anyware at any hook.
* This is more succinct than having to manually write each hook execution
* even past your desired one until the final result.
*/
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Atlas })
.anyware(async ({ encode }) => {
const { pack } = await encode()
const { exchange } = await pack()
const mergedHeaders = new Headers(exchange.input.request.headers)
mergedHeaders.set(`X-Custom-Header`, `123`)
// Notice how we **end** with the `exchange` hook, skipping the `unpack` and `decode` hooks.
return await exchange({
input: {
...exchange.input,
headers: mergedHeaders,
},
})
})
34 changes: 34 additions & 0 deletions examples/50_anyware/anyware_slot_slot-body__slot-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* This example shows how to use the `body` slot on the `pack` hook.
*/
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Atlas })
.anyware(async ({ pack }) => {
return await pack({
using: {
body: (graphqlRequest) => {
return JSON.stringify({
...graphqlRequest,
operationName: `queryContinents`,
})
},
},
})
})

const result = await graffle.rawString({
document: `
query queryContinents {
continents { name }
}
query queryCountries {
countries { name }
}
`,
operationName: `queryCountries`,
})

show(result)
34 changes: 34 additions & 0 deletions examples/50_anyware/anyware_slot_slot-body__slot-search-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* This example shows how to use the `searchParams` slot on the `pack` hook.
*/
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Atlas, transport: { methodMode: `getReads` } })
.anyware(async ({ pack }) => {
return await pack({
using: {
searchParams: (graphqlRequest) => {
return {
query: graphqlRequest.query,
operationName: `queryContinents`,
}
},
},
})
})

const result = await graffle.rawString({
document: `
query queryContinents {
continents { name }
}
query queryCountries {
countries { name }
}
`,
operationName: `queryCountries`,
})

show(result)
23 changes: 23 additions & 0 deletions examples/50_anyware/anyware_slot_slot-fetch__slot-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* This example shows how to use the `fetch` slot on `exchange` hook.
*/
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Atlas })
.anyware(async ({ exchange }) => {
return await exchange({
using: {
fetch: () => {
return new Response(JSON.stringify({ data: { continents: [{ name: `Earthsea` }] } }))
},
},
})
})

const result = await graffle.rawString({
document: `query { continents { name } }`,
})

show(result)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1726541102597'
'x-sent-at-time': '1726587622992'
},
signal: undefined,
method: 'post',
Expand Down
10 changes: 5 additions & 5 deletions examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
headers: Headers {
connection: 'keep-alive',
'content-length': '119',
'x-served-by': 'cache-yul1970024-YUL',
'x-served-by': 'cache-yul1970028-YUL',
'accept-ranges': 'bytes',
date: 'Sun, 08 Sep 2024 18:13:26 GMT',
'content-type': 'application/graphql-response+json; charset=utf-8',
Expand All @@ -32,13 +32,13 @@
'alt-svc': 'h3=":443"; ma=86400',
'access-control-allow-origin': '*',
'x-powered-by': 'Stellate',
age: '721898',
age: '768418',
'cache-control': 'public, s-maxage=2628000, stale-while-revalidate=2628000',
'x-cache': 'HIT',
'x-cache-hits': '84',
'x-cache-hits': '99',
'gcdn-cache': 'HIT',
'stellate-rate-limit-budget-remaining': '44',
'stellate-rate-limit-rules': '"IP limit";type="RequestCount";budget=50;limited=?0;remaining=44;refill=59',
'stellate-rate-limit-budget-remaining': '41',
'stellate-rate-limit-rules': '"IP limit";type="RequestCount";budget=50;limited=?0;remaining=41;refill=60',
'stellate-rate-limit-decision': 'pass',
'stellate-rate-limit-budget-required': '5',
'content-encoding': 'br'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---------------------------------------- SHOW ----------------------------------------
{
continents: [
{ name: 'Africa' },
{ name: 'Antarctica' },
{ name: 'Asia' },
{ name: 'Europe' },
{ name: 'North America' },
{ name: 'Oceania' },
{ name: 'South America' }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---------------------------------------- SHOW ----------------------------------------
{
continents: [
{ name: 'Africa' },
{ name: 'Antarctica' },
{ name: 'Asia' },
{ name: 'Europe' },
{ name: 'North America' },
{ name: 'Oceania' },
{ name: 'South America' }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---------------------------------------- SHOW ----------------------------------------
{ continents: [ { name: 'Earthsea' } ] }
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
parentId: 'f0c18d7e669cfd3d',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: 'f3a059903360f365',
traceState: undefined,
name: 'encode',
id: 'e06d4e860f48bc58',
id: '5219af43fdf80466',
kind: 0,
timestamp: 1726541103051000,
duration: 552.125,
timestamp: 1726587623552000,
duration: 542.375,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -33,14 +33,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
parentId: 'f0c18d7e669cfd3d',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: 'f3a059903360f365',
traceState: undefined,
name: 'pack',
id: 'a29e1e9baaa80c0e',
id: '063a4b532a0241a3',
kind: 0,
timestamp: 1726541103053000,
duration: 1086.75,
timestamp: 1726587623556000,
duration: 1065.958,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -57,14 +57,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
parentId: 'f0c18d7e669cfd3d',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: 'f3a059903360f365',
traceState: undefined,
name: 'exchange',
id: 'f2a21df585a2fdbb',
id: 'f5b369b304e991b6',
kind: 0,
timestamp: 1726541103055000,
duration: 177279.959,
timestamp: 1726587623558000,
duration: 148649.083,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -81,14 +81,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
parentId: 'f0c18d7e669cfd3d',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: 'f3a059903360f365',
traceState: undefined,
name: 'unpack',
id: '2088b4af9a15807e',
id: '0e527bfe6c4088cd',
kind: 0,
timestamp: 1726541103233000,
duration: 2944,
timestamp: 1726587623707000,
duration: 2221.792,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -105,14 +105,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
parentId: 'f0c18d7e669cfd3d',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: 'f3a059903360f365',
traceState: undefined,
name: 'decode',
id: '11a1c3568453721f',
id: '8abb47d1d55ae667',
kind: 0,
timestamp: 1726541103236000,
duration: 127.375,
timestamp: 1726587623709000,
duration: 122.375,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -129,14 +129,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '9f4dcc1213a3f3d4605da3d9c3340361',
traceId: '74881568a3d7d3c86f1657709c86b4a0',
parentId: undefined,
traceState: undefined,
name: 'request',
id: 'f0c18d7e669cfd3d',
id: 'f3a059903360f365',
kind: 0,
timestamp: 1726541103050000,
duration: 186111.542,
timestamp: 1726587623552000,
duration: 157809.333,
attributes: {},
status: { code: 0 },
events: [],
Expand Down
Loading

0 comments on commit 507637d

Please sign in to comment.