Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extension-system): transport now extensible #1272

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 +6,12 @@
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle
.create({
schema: publicGraphQLSchemaEndpoints.Pokemon,
.create()
.transport({
url: publicGraphQLSchemaEndpoints.Pokemon,
})
.anyware(({ pack }) => {
if (pack.input.transportType !== `http`) return pack()

Check warning on line 14 in examples/10_transport-http/transport-http_extension_headers__dynamicHeaders.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, both sides of the expression are literal values
return pack({
input: {
...pack.input,
Expand Down
23 changes: 11 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,25 +7,24 @@
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 }) => {
if (exchange.input.transportType !== `http`) return exchange()

Check warning on line 27 in examples/10_transport-http/transport-http_headers_raw__headers.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, both sides of the expression are literal values
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,8 +7,8 @@ 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
3 changes: 2 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,11 +7,12 @@
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 }) => {
// ^^^^^^^^
if (exchange.input.transportType !== `http`) return exchange()

Check warning on line 15 in examples/50_anyware/anyware_jump-start__jump-start.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, both sides of the expression are literal values

const mergedHeaders = new Headers(exchange.input.request.headers)
mergedHeaders.set(`X-Custom-Header`, `123`)
Expand Down
3 changes: 2 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,12 +7,13 @@
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()

if (exchange.input.transportType !== `http`) return exchange()

Check warning on line 16 in examples/50_anyware/anyware_short-circuit__short-circuit.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, both sides of the expression are literal values

const mergedHeaders = new Headers(exchange.input.request.headers)
mergedHeaders.set(`X-Custom-Header`, `123`)
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
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
"./extensions/schema-errors/generator": {
"default": "./build/entrypoints/extensions/schema-errors/gentime.js"
},
"./extensions/transport-memory": {
"default": "./build/entrypoints/extensions/transport-memory/runtime.js"
},
"./extensions/transport-http": {
"default": "./build/entrypoints/extensions/transport-http/runtime.js"
},
"./presets/bare": {
"default": "./build/entrypoints/presets/__GraffleBare.js"
},
"./presets/basic": {
"default": "./build/entrypoints/presets/__GraffleBasic.js"
},
"./presets/minimal": {
"default": "./build/entrypoints/presets/__GraffleMinimal.js"
},
"./client": {
"default": "./build/entrypoints/client.js"
},
Expand Down
Loading
Loading