-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(anyware): adjust signature for fetch (#1100)
- Loading branch information
1 parent
d452c5e
commit 507637d
Showing
54 changed files
with
547 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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
23
examples/50_anyware/anyware_short-circuit__short-circuit.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,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, | ||
}, | ||
}) | ||
}) |
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,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
34
examples/50_anyware/anyware_slot_slot-body__slot-search-params.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,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
23
examples/50_anyware/anyware_slot_slot-fetch__slot-fetch.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,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) |
File renamed without changes.
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
File renamed without changes.
Empty file.
12 changes: 12 additions & 0 deletions
12
examples/__outputs__/50_anyware/anyware_slot_slot-body__slot-body.output.txt
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,12 @@ | ||
---------------------------------------- SHOW ---------------------------------------- | ||
{ | ||
continents: [ | ||
{ name: 'Africa' }, | ||
{ name: 'Antarctica' }, | ||
{ name: 'Asia' }, | ||
{ name: 'Europe' }, | ||
{ name: 'North America' }, | ||
{ name: 'Oceania' }, | ||
{ name: 'South America' } | ||
] | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/__outputs__/50_anyware/anyware_slot_slot-body__slot-search-params.output.txt
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,12 @@ | ||
---------------------------------------- SHOW ---------------------------------------- | ||
{ | ||
continents: [ | ||
{ name: 'Africa' }, | ||
{ name: 'Antarctica' }, | ||
{ name: 'Asia' }, | ||
{ name: 'Europe' }, | ||
{ name: 'North America' }, | ||
{ name: 'Oceania' }, | ||
{ name: 'South America' } | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/__outputs__/50_anyware/anyware_slot_slot-fetch__slot-fetch.output.txt
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,2 @@ | ||
---------------------------------------- SHOW ---------------------------------------- | ||
{ continents: [ { name: 'Earthsea' } ] } |
File renamed without changes.
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
Oops, something went wrong.