Skip to content

Commit

Permalink
feat: better raw (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 7, 2024
1 parent 4913fe1 commit a539a8f
Show file tree
Hide file tree
Showing 217 changed files with 2,734 additions and 2,861 deletions.
15 changes: 7 additions & 8 deletions examples/10_transport-http/transport-http_abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ const graffle = Graffle.create({
const resultPromise = graffle
.with({ transport: { signal: abortController.signal } })
// ^^^^^^^^^^^^^^^
.rawString({
document: `
{
pokemon {
name
}
.gql`
{
pokemon {
name
}
`,
})
}
`
.send()

abortController.abort()
// ^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ const graffle = Graffle
})
)

const data = await graffle.rawString({ document: `{ pokemon { name } }` })
const data = await graffle.gql`{ pokemon { name } }`.send()

showJson(data)
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ const graffle = Graffle
return exchange()
})

await graffle.rawString({ document: `{ pokemons { name } }` })
await graffle.gql`{ pokemons { name } }`.send()
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ const graffle = Graffle
return exchange()
})

await graffle.rawString({ document: `{ pokemons { name } }` })
await graffle.gql`{ pokemons { name } }`.send()
6 changes: 2 additions & 4 deletions examples/10_transport-http/transport-http_method-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ 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", type: grass) { name } }`,
})
await graffle.gql`mutation { addPokemon(attack:0, defense:0, hp:1, name:"Nano", type: grass) { name } }`.send()

// 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.gql`query { pokemonByName(name: "Nano") { hp } }`.send()
2 changes: 1 addition & 1 deletion examples/10_transport-http/transport-http_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const graffle = Graffle
return exchange()
})

await graffle.rawString({ document: `{ pokemons { name } }` })
await graffle.gql`{ pokemons { name } }`.send()
2 changes: 1 addition & 1 deletion examples/20_output/output_preset__standard-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const graffle = Graffle.create({
output: Preset.traditionalGraphqlOutput,
})

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

show(result)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* This example shows how to send a request using a Document instance for the GraphQL document.
*/

import { parse } from 'graphql'
import { Opentelemetry, Throws } from '../../src/entrypoints/extensions.js'
import { gql, Graffle } from '../../src/entrypoints/main.js'
import { Graffle } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
Expand All @@ -12,18 +13,15 @@ const graffle = Graffle.create({
.use(Throws())
.use(Opentelemetry())

const data = await graffle.raw({
document: gql`
query pokemonByName ($Name: String!) {
pokemonByName (name: $Name) {
const data = await graffle.gql(parse(`
query pokemonByName ($name: String!) {
pokemonByName (name: $name) {
name
trainer {
name
continent {
name
}
}
}
`,
variables: { name: `Pikachu` },
})
}
`)).send({ name: `Pikachu` })

show(data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This example shows how to use the TypeScript type "TypedQueryDocumentNode" from the
* package `graphql` to make a type safe request with gql method.
*/

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,
})

type Document = TypedQueryDocumentNode<
{
pokemonByName: {
id: string
name: string
hp: number
attack: number
defense: number
trainer: { name: string }
}
},
{ name: string }
>

const document = parse(`
query ($name: String!) {
pokemonByName (name: $name) {
name
hp
attack
defense
trainer {
name
}
}
}
`) as Document

const data = await graffle.gql(document).send({ name: `Pikachu` })

show(data?.pokemonByName)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const document = /* gql */ `
}
`

const data = await graffle.rawString({ document })
const data = await graffle.gql(document).send()

show(data)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* of automation would generate the types for you.
*/

import { Graffle, type TypedDocumentString } from '../../src/entrypoints/main.js'
import { Graffle, type TypedDocument } from '../../src/entrypoints/main.js'
import { publicGraphQLSchemaEndpoints, show } from '../$/helpers.js'

const graffle = Graffle.create({
Expand All @@ -17,7 +17,7 @@ const graffle = Graffle.create({
* @see https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#documentmode
* @see https://github.com/jasonkuhrt/graffle/issues/997
*/
type Document = TypedDocumentString<
type Document = TypedDocument.String<
{
pokemonByName: {
id: string
Expand Down Expand Up @@ -47,9 +47,6 @@ const document: Document = /* gql */ `
}
`

const data = await graffle.rawString({
document,
variables: { name: `Pikachu` },
})
const data = await graffle.gql(document).send({ name: `Pikachu` })

show(data?.pokemonByName)

This file was deleted.

6 changes: 5 additions & 1 deletion examples/40_other/transport-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const schema = new GraphQLSchema({

const graffle = Graffle.create({ schema })

const data = await graffle.rawString({ document: `{ foo }` })
const data = await graffle.gql`
{
foo
}
`.send()

showJson(data)
7 changes: 2 additions & 5 deletions examples/50_anyware/anyware_slot_slot-body__slot-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ const graffle = Graffle
})
})

const result = await graffle.rawString({
document: `
const result = await graffle.gql`
query pokemon {
trainers { name }
}
query trainers {
pokemon { name }
}
`,
operationName: `pokemon`,
})
`.send(`pokemon`)

show(result)
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ const graffle = Graffle
})
})

const result = await graffle.rawString({
document: `
const result = await graffle.gql`
query trainers {
pokemon { name }
}
query pokemon {
trainers { name }
}
`,
operationName: `queryCountries`,
})
`
.send(`queryCountries`)

show(result)
12 changes: 8 additions & 4 deletions examples/50_anyware/anyware_slot_slot-fetch__slot-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ const graffle = Graffle
return await exchange({
using: {
fetch: () => {
return new Response(JSON.stringify({ data: { continents: [{ name: `Earthsea` }] } }))
return new Response(JSON.stringify({ data: { trainers: [{ name: `Jason` }] } }))
},
},
})
})

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

show(result)
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ provider.addSpanProcessor(processor)
provider.register()

const graffle = Pokemon.create().use(Opentelemetry())
const data = await graffle.rawString({ document: `query { pokemons { name } }` })
const data = await graffle.gql`query { pokemons { name } }`.send()
show(data)
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': '1727803644880'
'x-sent-at-time': '1728318376823'
},
signal: undefined,
method: 'post',
Expand Down
2 changes: 1 addition & 1 deletion examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
headers: Headers {
'content-type': 'application/graphql-response+json; charset=utf-8',
'content-length': '142',
date: 'Tue, 01 Oct 2024 17:27:25 GMT',
date: 'Mon, 07 Oct 2024 16:26:17 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
ContextualError: There was an error in the extension "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 Object.run (/some/path/to/main.ts:XX:XX:22)
at async run (/some/path/to/client.ts:XX:XX:20)
at async executeDocument (/some/path/to/client.ts:XX:XX:12)
at async executeRootTypeField (/some/path/to/client.ts:XX:XX:20)
at async executeDocument (/some/path/to/requestMethods.ts:XX:XX:18)
at async executeRootTypeField (/some/path/to/requestMethods.ts:XX:XX:18)
at async <anonymous> (/some/path/to/output_envelope_envelope-error__envelope-error.ts:XX:XX:16) {
context: {
hookName: 'encode',
Expand Down
Loading

0 comments on commit a539a8f

Please sign in to comment.