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(transport-http): support get method #1066

Merged
merged 20 commits into from
Sep 6, 2024
Prev Previous commit
Next Next commit
no code formatting on generated code blocks
  • Loading branch information
jasonkuhrt committed Sep 6, 2024
commit 7e56d7e4756780fe9f5d8ae55125ed33c5673c2a
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': '1725646899211'
'x-sent-at-time': '1725648269194'
},
signal: undefined,
method: 'post',
Expand Down
1 change: 1 addition & 0 deletions examples/transport-http|transport-http_method-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { serveSchema, show } from './$/helpers.js'
import { schema } from './$/schemas/pokemon/schema.js'

const server = await serveSchema({ schema })

const graffle = Pokemon
.create({
schema: server.url,
Expand Down
7 changes: 6 additions & 1 deletion scripts/generate-examples-derivatives/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,23 @@ aside: false

# ${example.fileName.canonicalTitle}${example.description ? `\n\n${example.description}\n` : ``}

<!-- dprint-ignore-start -->
\`\`\`ts twoslash
${example.file.content.trim()}
\`\`\`
<!-- dprint-ignore-end -->

#### Outputs

${
example.output.blocks.map(block => {
return `
<!-- dprint-ignore-start -->
\`\`\`${example.isUsingJsonOutput ? `json` : `txt`}
${block}
\`\`\``.trim()
\`\`\`
<!-- dprint-ignore-end -->
`.trim()
}).join(`\n`)
}
`.trim()
Expand Down
4 changes: 4 additions & 0 deletions website/content/examples/generated-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Arguments

<!-- dprint-ignore-start -->
```ts twoslash
import { SocialStudies } from './$/generated-clients/SocialStudies/__.js'

Expand All @@ -18,9 +19,11 @@ const countries = await socialStudies.query.countries({
console.log(countries)
// ^?
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```json
[
{
Expand All @@ -43,3 +46,4 @@ console.log(countries)
}
]
```
<!-- dprint-ignore-end -->
4 changes: 4 additions & 0 deletions website/content/examples/raw-string-typed.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Raw String Typed

<!-- dprint-ignore-start -->
```ts twoslash
import { Graffle } from 'graffle'
// todo from 'graffle/utils'
Expand Down Expand Up @@ -43,13 +44,16 @@ const result = await graffle.rawString({
console.log(result.data?.countries)
// ^?
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
[
{ name: 'Canada', continent: { name: 'North America' } },
{ name: 'Germany', continent: { name: 'Europe' } },
{ name: 'Japan', continent: { name: 'Asia' } }
]
```
<!-- dprint-ignore-end -->
4 changes: 4 additions & 0 deletions website/content/examples/raw-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Raw String

<!-- dprint-ignore-start -->
```ts twoslash
import { Graffle } from 'graffle'

Expand All @@ -26,9 +27,11 @@ const result = await graffle.rawString({
console.log(result.data)
// ^?
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
{
countries: [
Expand Down Expand Up @@ -136,3 +139,4 @@ console.log(result.data)
]
}
```
<!-- dprint-ignore-end -->
24 changes: 10 additions & 14 deletions website/content/examples/raw-typed.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ aside: false

# Raw Typed

<!-- dprint-ignore-start -->
```ts twoslash
import { gql, Graffle } from 'graffle'
import type { TypedQueryDocumentNode } from 'graphql'
import { gql, Graffle } from 'graffle'

const graffle = Graffle.create({
schema: `https://countries.trevorblades.com/graphql`,
Expand All @@ -21,10 +22,7 @@ const graffle = Graffle.create({
*/

{
const document = gql<
{ countries: { name: string; continent: { name: string } }[] },
{ filter: string[] }
>`
const document = gql<{ countries: { name: string; continent: { name: string } }[] }, { filter: string[] }>`
query countries ($filter: [String!]) {
countries (filter: { name: { in: $filter } }) {
name
Expand All @@ -35,10 +33,7 @@ const graffle = Graffle.create({
}
`

const result = await graffle.raw({
document,
variables: { filter: [`Canada`, `Germany`, `Japan`] },
})
const result = await graffle.raw({ document, variables: { filter: [`Canada`, `Germany`, `Japan`] } })

console.log(result.data?.countries)
}
Expand Down Expand Up @@ -68,29 +63,30 @@ const graffle = Graffle.create({
}
`

const result = await graffle.raw({
document,
variables: { filter: [`Canada`, `Germany`, `Japan`] },
})
const result = await graffle.raw({ document, variables: { filter: [`Canada`, `Germany`, `Japan`] } })

console.log(result.data?.countries)
}
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
[
{ name: 'Canada', continent: { name: 'North America' } },
{ name: 'Germany', continent: { name: 'Europe' } },
{ name: 'Japan', continent: { name: 'Asia' } }
]
```

<!-- dprint-ignore-end -->
<!-- dprint-ignore-start -->
```txt
[
{ name: 'Canada', continent: { name: 'North America' } },
{ name: 'Germany', continent: { name: 'Europe' } },
{ name: 'Japan', continent: { name: 'Asia' } }
]
```
<!-- dprint-ignore-end -->
4 changes: 4 additions & 0 deletions website/content/examples/raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Raw

<!-- dprint-ignore-start -->
```ts twoslash
import { gql, Graffle } from 'graffle'

Expand All @@ -28,9 +29,11 @@ const result = await graffle.raw({
console.log(result.data)
// ^?
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
{
countries: [
Expand All @@ -40,3 +43,4 @@ console.log(result.data)
]
}
```
<!-- dprint-ignore-end -->
8 changes: 5 additions & 3 deletions website/content/examples/transport-http-abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ aside: false

It is possible to cancel a request using an `AbortController` signal.

<!-- dprint-ignore-start -->
```ts twoslash
import { Graffle } from 'graffle'

Expand All @@ -29,18 +30,19 @@ const resultPromise = graffle

abortController.abort()

const result = await resultPromise.catch((error: unknown) =>
(error as Error).message
)
const result = await resultPromise.catch((error: unknown) => (error as Error).message)

console.log(result)
// ^?

// todo .with(...) variant
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
'This operation was aborted'
```
<!-- dprint-ignore-end -->
6 changes: 5 additions & 1 deletion website/content/examples/transport-http-dynamic-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Dynamic Headers

<!-- dprint-ignore-start -->
```ts twoslash
import { Graffle } from 'graffle'

Expand All @@ -29,20 +30,23 @@ const graffle = Graffle

await graffle.rawString({ document: `{ languages { code } }` })
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
{
methodMode: 'post',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1725646899211'
'x-sent-at-time': '1725648269194'
},
signal: undefined,
method: 'post',
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}'
}
```
<!-- dprint-ignore-end -->
14 changes: 6 additions & 8 deletions website/content/examples/transport-http-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aside: false

# Fetch

<!-- dprint-ignore-start -->
```ts twoslash
import { Graffle } from 'graffle'

Expand All @@ -15,27 +16,23 @@ const graffle = Graffle
return await exchange({
using: {
fetch: async () => {
return new Response(
JSON.stringify({
data: { countries: [{ name: `Canada Mocked!` }] },
}),
)
return new Response(JSON.stringify({ data: { countries: [{ name: `Canada Mocked!` }] } }))
},
},
})
},
})

const countries = await graffle.rawString({
document: `{ countries { name } }`,
})
const countries = await graffle.rawString({ document: `{ countries { name } }` })

console.log(countries.data)
// ^?
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```json
{
"countries": [
Expand All @@ -45,3 +42,4 @@ console.log(countries.data)
]
}
```
<!-- dprint-ignore-end -->
17 changes: 9 additions & 8 deletions website/content/examples/transport-http-method-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ aside: false
This example shows usage of the `getReads` method mode for the HTTP transport. This mode causes read-kind operations (query, subscription)
to be sent over HTTP GET method. Note write-kind operations (mutation) are still sent over HTTP POST method.

<!-- dprint-ignore-start -->
```ts twoslash
import { Pokemon } from './$/generated-clients/Pokemon/__.js'
import { schema } from './$/schemas/pokemon/schema.js'

const server = await serveSchema({ schema })

const graffle = Pokemon
.create({
schema: server.url,
Expand All @@ -24,22 +26,19 @@ const graffle = Pokemon

// The following request will use an HTTP POST method because it is
// using a "mutation" type of operation.
await graffle.rawString({
document:
`mutation addPokemon(attack:0, defense:0, hp:1, name:"Nano") { name }`,
})
await graffle.rawString({ document: `mutation addPokemon(attack:0, defense:0, hp:1, name:"Nano") { name }` })

// The following request will use an HTTP GET method because it
// is using a "query" type of operation.
await graffle.rawString({
document: `query { pokemonByName(name:"Nano") { hp } }`,
})
await graffle.rawString({ document: `query { pokemonByName(name:"Nano") { hp } }` })

await server.stop()
```
<!-- dprint-ignore-end -->

#### Outputs

<!-- dprint-ignore-start -->
```txt
{
methodMode: 'getReads',
Expand All @@ -66,7 +65,8 @@ await server.stop()
body: '{"query":"mutation addPokemon(attack:0, defense:0, hp:1, name:\\"Nano\\") { name }"}'
}
```

<!-- dprint-ignore-end -->
<!-- dprint-ignore-start -->
```txt
{
methodMode: 'getReads',
Expand All @@ -91,3 +91,4 @@ await server.stop()
}
}
```
<!-- dprint-ignore-end -->
Loading
Loading