Skip to content

Commit

Permalink
feat(extension-system): transport now extensible (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Dec 2, 2024
1 parent a78ebbe commit 579723f
Show file tree
Hide file tree
Showing 189 changed files with 4,382 additions and 3,272 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default tsEslint.config({
},
},
rules: {
['prefer-arrow/prefer-arrow-functions']: 'off',
['@typescript-eslint/only-throw-error']: 'off',
['@typescript-eslint/no-unsafe-assignment']: 'off',
['@typescript-eslint/no-unsafe-call']: 'off',
Expand Down
10 changes: 4 additions & 6 deletions examples/10_transport-http/transport-http_abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'
const abortController = new AbortController()
// ^^^^^^^^^^^^^^^

const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
const graffle = Graffle.create().transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
})

graffle

const resultPromise = graffle
.with({ transport: { raw: { signal: abortController.signal } } })
// ^^^^^^^^^^^^^^^
.transport({ raw: { signal: abortController.signal } })
// ^^^^^^^^^^^^^^^
.gql`
{
pokemon {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { showJson } from '../$/helpers.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon })
.anyware(({ exchange }) =>
exchange({
using: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
})
.anyware(({ pack }) => {
// eslint-disable-next-line
if (pack.input.transportType !== `http`) return pack()
return pack({
input: {
Expand Down
24 changes: 12 additions & 12 deletions examples/10_transport-http/transport-http_headers_raw__headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import { show } from '../$/helpers.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

const graffle = Graffle
.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
transport: {
.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
headers: {
authorization: `Bearer MY_TOKEN`,
'x-something-to-unset': `true`,
},
raw: {
headers: {
authorization: `Bearer MY_TOKEN`,
'x-something-to-unset': `true`,
},
raw: {
headers: {
'x-from-raw': `true`,
},
'x-from-raw': `true`,
},
},
})
.with({
transport: { headers: { 'x-something-to-unset': `` } },
.transport({
headers: { 'x-something-to-unset': `` },
})
.anyware(({ exchange }) => {
// eslint-disable-next-line
if (exchange.input.transportType !== `http`) return exchange()
show(exchange.input.request.headers)
return exchange()
Expand Down
9 changes: 4 additions & 5 deletions examples/10_transport-http/transport-http_method-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { Graffle } from '../../tests/_/schemas/pokemon/graffle/__.js'
import { show } from '../$/helpers.js'

const graffle = Graffle
.create({
transport: {
methodMode: `getReads`, // [!code highlight]
headers: { tenant: `nano` },
},
.create()
.transport({
methodMode: `getReads`, // [!code highlight]
headers: { tenant: `nano` },
})
.anyware(async ({ exchange }) => {
show(exchange.input.request)
Expand Down
11 changes: 5 additions & 6 deletions examples/10_transport-http/transport-http_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import { show } from '../$/helpers.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

const graffle = Graffle
.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
transport: {
raw: {
mode: `cors`,
},
.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
raw: {
mode: `cors`,
},
})
.anyware(async ({ exchange }) => {
Expand Down
7 changes: 3 additions & 4 deletions examples/20_output/output_preset__standard-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import { Graffle, Preset } from '../../src/entrypoints/main.js'
import { show } from '../$/show.js'

const graffle = Graffle.create({
schema: `...`,
output: Preset.traditionalGraphqlOutput,
})
const graffle = Graffle
.create({ output: Preset.traditionalGraphqlOutput })
.transport({ url: `...` })

const result = await graffle.gql(`{ query { thisWillError } }`).send()

Expand Down
8 changes: 5 additions & 3 deletions examples/30_gql/gql_gql-document-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { Throws } from '../../src/entrypoints/extensions/throws/runtime.js'
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
})
const graffle = Graffle
.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
})
.use(Throws())
.use(Opentelemetry())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { parse, type TypedQueryDocumentNode } from 'graphql'
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
})
const graffle = Graffle.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
})

type Document = TypedQueryDocumentNode<
{
Expand Down
4 changes: 1 addition & 3 deletions examples/30_gql/gql_gql-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
})
const graffle = Graffle.create().transport({ url: publicGraphQLSchemaEndpoints.Pokemon })

const data = await graffle.gql`
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import { Graffle, type TypedDocument } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
})
const graffle = Graffle.create().transport({ url: publicGraphQLSchemaEndpoints.Pokemon })

/**
* @remarks Typically this type would come from your code generation tool.
Expand Down
6 changes: 5 additions & 1 deletion examples/40_other/transport-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'
import { Graffle } from '../../src/entrypoints/main.js'
import { TransportMemory } from '../../src/extensions/TransportMemory/TransportMemory.js'
import { showJson } from '../$/helpers.js'

const schema = new GraphQLSchema({
Expand All @@ -18,7 +19,10 @@ const schema = new GraphQLSchema({
}),
})

const graffle = Graffle.create({ schema })
const graffle = Graffle
.create()
.use(TransportMemory({ schema }))
.transport(`memory`)

const data = await graffle.gql`
{
Expand Down
4 changes: 3 additions & 1 deletion examples/50_anyware/anyware_jump-start__jump-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon })
// Notice how we **start** with the `exchange` hook, skipping the `encode` and `pack` hooks.
.anyware(async ({ exchange }) => {
// ^^^^^^^^
// eslint-disable-next-line
if (exchange.input.transportType !== `http`) return exchange()

const mergedHeaders = new Headers(exchange.input.request.headers)
Expand Down
4 changes: 3 additions & 1 deletion examples/50_anyware/anyware_short-circuit__short-circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints } from '../$/helpers.js'

Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon })
.anyware(async ({ encode }) => {
const { pack } = await encode()
const { exchange } = await pack()

// eslint-disable-next-line
if (exchange.input.transportType !== `http`) return exchange()

const mergedHeaders = new Headers(exchange.input.request.headers)
Expand Down
3 changes: 2 additions & 1 deletion examples/50_anyware/anyware_slot_slot-body__slot-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon })
.anyware(async ({ pack }) => {
return await pack({
using: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon, transport: { methodMode: `getReads` } })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon, methodMode: `getReads` })
.anyware(async ({ pack }) => {
return await pack({
using: {
Expand Down
3 changes: 2 additions & 1 deletion examples/50_anyware/anyware_slot_slot-fetch__slot-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({ schema: publicGraphQLSchemaEndpoints.Pokemon })
.create()
.transport({ url: publicGraphQLSchemaEndpoints.Pokemon })
.anyware(async ({ exchange }) => {
return await exchange({
using: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Headers {
'content-type': 'application/json',
authorization: 'Bearer MY_TOKEN',
'x-from-raw': 'true'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ ContextualError: There was an error in the core implementation of hook "exchange
context: { hookName: 'exchange', source: 'implementation' },
[cause]: TypeError: Failed to parse URL from ...
at new Request (node:internal/deps/undici/undici:XX:XX)
at Object.run (/some/path/to/httpTransport.ts:XX:XX:27)
at Object.run (/some/path/to/TransportHttp.ts:XX:XX:31)
... 6 lines matching cause stack trace ...
at async Module.run (/some/path/to/run.ts:XX:XX:10)
at async Object.send (/some/path/to/gql.ts:XX:XX:26) {
[cause]: TypeError: Invalid URL
at new URL (node:internal/url:XX:XX)
at new Request (node:internal/deps/undici/undici:XX:XX)
at Object.run (/some/path/to/httpTransport.ts:XX:XX:27)
at Object.run (/some/path/to/TransportHttp.ts:XX:XX:31)
at Object.run (/some/path/to/Pipeline.ts:XX:XX:51)
at runStep (/some/path/to/runStep.ts:XX:XX:37)
at runPipeline (/some/path/to/runPipeline.ts:XX:XX:8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---------------------------------------- SHOW ----------------------------------------
ContextualError: There was an error in the interceptor "anonymous" (use named functions to improve this error message) while running hook "encode".
at runPipeline (/some/path/to/runPipeline.ts:XX:XX:18)
at async <anonymous> (/some/path/to/runner.ts:XX:XX:20)
at async Module.run (/some/path/to/run.ts:XX:XX:10)
at async executeDocument (/some/path/to/requestMethods.ts:XX:XX:18)
at async executeRootField (/some/path/to/requestMethods.ts:XX:XX:18)
at async <anonymous> (/some/path/to/extension_throws__throws.ts:XX:XX:17) {
context: {
hookName: 'encode',
source: 'extension',
interceptorName: 'anonymous'
},
cause: Error: Something went wrong.
at <anonymous> (/some/path/to/extension_throws__throws.ts:XX:XX:11)
at applyBody (/some/path/to/runner.ts:XX:XX:28)
}
Loading

0 comments on commit 579723f

Please sign in to comment.