Skip to content

Commit

Permalink
feat: raw overload (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Jul 5, 2024
1 parent 8efd3b2 commit c3a1718
Show file tree
Hide file tree
Showing 25 changed files with 136 additions and 58 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,26 @@ await client.request(document)
## Examples

- Request:
- [Authentication via HTTP header](./examples/request-authentication-via-http-header.ts)
- [Method GET](./examples/request-method-get.ts)
- [Cancellation](./examples/request-cancellation.ts)
- [Headers Per Request (static)](./examples/request-headers-static-per-request.ts)
- [Headers Per Request (dynamic)](./examples/request-headers-dynamic-per-request.ts)
- [Handle Raw Response](./examples/request-handle-raw-response.ts)
- [Authentication via HTTP header](./examples/legacy/request-authentication-via-http-header.ts)
- [Method GET](./examples/legacy/request-method-get.ts)
- [Cancellation](./examples/legacy/request-cancellation.ts)
- [Headers Per Request (static)](./examples/legacy/request-headers-static-per-request.ts)
- [Headers Per Request (dynamic)](./examples/legacy/request-headers-dynamic-per-request.ts)
- [Handle Raw Response](./examples/legacy/request-handle-raw-response.ts)
- GraphQL:
- [Document Variables](./examples/graphql-document-variables.ts)
- [Mutation](./examples/graphql-mutations.ts)
- [Batching Requests](./examples/graphql-batching-requests.ts)
- [Document Variables](./examples/legacy/graphql-document-variables.ts)
- [Mutation](./examples/legacy/graphql-mutations.ts)
- [Batching Requests](./examples/legacy/graphql-batching-requests.ts)
- Configuration:
- [Fetch: Passing Options](./examples/configuration-fetch-options.ts)
- [Custom JSON Serializer](./examples/configuration-request-json-serializer.ts)
- [Incremental: Set Endpoint](./examples/configuration-incremental-endpoint.ts)
- [Incremental: Set Request Headers](./examples/configuration-incremental-request-headers.ts)
- [Fetch: Passing Options](./examples/legacy/configuration-fetch-options.ts)
- [Custom JSON Serializer](./examples/legacy/configuration-request-json-serializer.ts)
- [Incremental: Set Endpoint](./examples/legacy/configuration-incremental-endpoint.ts)
- [Incremental: Set Request Headers](./examples/legacy/configuration-incremental-request-headers.ts)
- TypeScript:
- [Use `TypedDocumentNode`](./examples/typescript-typed-document-node.ts)
- [Use `TypedDocumentNode`](./examples/legacy/typescript-typed-document-node.ts)
- Other:
- [Middleware](./examples/other-middleware.ts)
- [Error Handling](./examples/other-error-handling.ts)
- [Middleware](./examples/legacy/other-middleware.ts)
- [Error Handling](./examples/legacy/other-error-handling.ts)

## Node Version Support

Expand All @@ -136,7 +136,7 @@ Any issue that exists solely for an unsupported version of Nodejs will be reject

## Reference

⚠️ This reference is incomplete. Check out the [examples](./examples/) for more reference material.
⚠️ This reference is incomplete. Check out the [examples](./examples/legacy/) for more reference material.

### Configuration

Expand Down
21 changes: 21 additions & 0 deletions examples/Graffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { gql, Graffle } from '../src/entrypoints/alpha/main.js'

const request = Graffle.create({ schema: `https://countries.trevorblades.com/graphql` }).rawOrThrow // eslint-disable-line

// todo typed document node
// interface Data {
// countries: { name }[]
// }
// const { data } = await request<Data>(

const { data } = await request(
gql`
{
countries {
name
}
}
`,
)

console.log(data)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* If you want to change the endpoint after the GraphQLClient has been initialized, you can use the `setEndpoint()` function.
*/

import { GraphQLClient } from '../src/entrypoints/main.js'
import { GraphQLClient } from '../../src/entrypoints/main.js'

const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* If you want to set headers after the GraphQLClient has been initialized, you can use the `setHeader()` or `setHeaders()` functions.
*/

import { GraphQLClient } from '../src/entrypoints/main.js'
import { GraphQLClient } from '../../src/entrypoints/main.js'

const client = new GraphQLClient(`https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import JSONbig from 'json-bigint'
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const jsonSerializer = JSONbig({ useNativeBigInt: true })
const graphQLClient = new GraphQLClient(`https://some-api`, { jsonSerializer })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* It is possible with `graphql-request` to use batching via the `batchRequests()` function.
* @see https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md
*/
import { batchRequests, gql } from '../src/entrypoints/main.js'
import { batchRequests, gql } from '../../src/entrypoints/main.js'

const endpoint = `https://api.spacex.land/graphql/`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql, request } from '../src/entrypoints/main.js'
import { gql, request } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql, request } from '../src/entrypoints/main.js'
import { gql, request } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* It's possible to use a middleware to pre-process any request or handle raw response.
*/

import type { RequestMiddleware, ResponseMiddleware } from '../src/entrypoints/main.js'
import { GraphQLClient } from '../src/entrypoints/main.js'
import type { RequestMiddleware, ResponseMiddleware } from '../../src/entrypoints/main.js'
import { GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.spacex.land/graphql/`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* It is possible to cancel a request using an `AbortController` signal.
*/

import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* If you need to access the `extensions` key you can use the `rawRequest` method:
*/

import { gql, rawRequest } from '../src/entrypoints/main.js'
import { gql, rawRequest } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To do that, pass a function that returns the headers to the `headers` property when creating a new `GraphQLClient`.
*/

import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const client = new GraphQLClient(`https://some-api`, {
headers: () => ({ 'X-Sent-At-Time': Date.now().toString() }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* It is possible to pass custom headers for each request. `request()` and `rawRequest()` accept a header object as the third parameter
*/

import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Queries can be sent as an HTTP GET request:
*/
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
import { gql, GraphQLClient } from '../../src/entrypoints/main.js'

const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { parse } from 'graphql'
import { gql, GraphQLClient, request } from '../src/entrypoints/main.js'
import { gql, GraphQLClient, request } from '../../src/entrypoints/main.js'

{
const endpoint = `https://graphql-yoga.com/api/graphql`
Expand Down
1 change: 1 addition & 0 deletions src/entrypoints/alpha/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { execute } from '../../layers/0_functions/execute.js'
export { request } from '../../layers/0_functions/request.js'
export { gql } from '../../legacy/functions/gql.js'
export * from './__Graffle.js'
Empty file.
20 changes: 11 additions & 9 deletions src/layers/5_core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ const getRootIndexOrThrow = (context: ContextInterfaceTyped, rootTypeName: strin
}

// eslint-disable-next-line
type InterfaceInput<A = {}, B = {}> =
type InterfaceInput<TypedProperties = {}, RawProperties = {}> =
| ({
interface: InterfaceTyped
context: ContextInterfaceTyped
rootTypeName: Schema.RootTypeName
} & A)
} & TypedProperties)
| ({
interface: InterfaceRaw
context: ContextInterfaceRaw
} & B)
} & RawProperties)

// eslint-disable-next-line
type TransportInput<A = {}, B = {}> =
type TransportInput<HttpProperties = {}, MemoryProperties = {}> =
| ({
transport: TransportHttp
} & A)
} & HttpProperties)
| ({
transport: TransportMemory
} & B)
} & MemoryProperties)

export const hookNamesOrderedBySequence = [`encode`, `pack`, `exchange`, `unpack`, `decode`] as const

Expand All @@ -56,7 +56,7 @@ export type HookDefEncode = {
input:
& InterfaceInput<
{ selection: GraphQLObjectSelection },
{ document: string | DocumentNode; variables?: StandardScalarVariables }
{ document: string | DocumentNode; variables?: StandardScalarVariables; operationName?: string }
>
& TransportInput<{ schema: string | URL }, { schema: GraphQLSchema }>
slots: {
Expand Down Expand Up @@ -163,6 +163,7 @@ export const anyware = Anyware.create<HookSequence, HookMap, ExecutionResult>({
run: ({ input, slots }) => {
let document: string
let variables: StandardScalarVariables | undefined = undefined
let operationName: string | undefined = undefined

switch (input.interface) {
case `raw`: {
Expand All @@ -171,6 +172,7 @@ export const anyware = Anyware.create<HookSequence, HookMap, ExecutionResult>({
: print(input.document)
document = documentPrinted
variables = input.variables
operationName = input.operationName
break
}
case `typed`: {
Expand All @@ -192,7 +194,7 @@ export const anyware = Anyware.create<HookSequence, HookMap, ExecutionResult>({
const body = slots.body({
query: document,
variables,
operationName: `todo`,
operationName,
})

return {
Expand All @@ -207,7 +209,7 @@ export const anyware = Anyware.create<HookSequence, HookMap, ExecutionResult>({
schema: input.schema,
query: document,
variables,
// operationName: '',
operationName,
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion src/layers/6_client/client.raw.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect, test } from 'vitest'
import { beforeEach, describe, expect } from 'vitest'
import { test } from '../../../tests/_/helpers.js'
import { Graffle } from '../../../tests/_/schema/generated/__.js'
import { schema } from '../../../tests/_/schema/schema.js'
import { createExtension } from '../5_createExtension/createExtension.js'

// todo test with custom scalars

Expand All @@ -21,3 +23,31 @@ test(`.raw() returns errors in array`, async () => {
}
`)
})

describe(`memory transport`, () => {
let input: object | undefined
const peakInput = createExtension({
name: `peak`,
anyware: ({ exchange }) => {
if (exchange.input.transport === `memory`) {
input = exchange.input
}
return exchange()
},
})
beforeEach(() => {
input = undefined
})
describe(`operationName`, () => {
test(`undefined by default`, async () => {
await graffle.use(peakInput).raw(`query { id }`)
expect(input).toMatchObject({ operationName: undefined })
})
test(`reflects explicit value`, async () => {
await graffle.use(peakInput).raw(`query { id }`, { operationName: `foo` })
expect(input).toMatchObject({ operationName: `foo` })
})
})
})

// todo http transport
4 changes: 2 additions & 2 deletions src/layers/6_client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe(`without schemaIndex only raw is available`, () => {
})

test(`available methods`, () => {
expect(graffle.raw).toBeTypeOf(`function`)
expect(graffle.rawOrThrow).toBeTypeOf(`function`)
expect(graffle.raw).toBeTypeOf(`function`) // eslint-disable-line
expect(graffle.rawOrThrow).toBeTypeOf(`function`) // eslint-disable-line
})
})

Expand Down
Loading

0 comments on commit c3a1718

Please sign in to comment.