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
Merged
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
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default tsEslint.config({
'eslint.config.js',
'vite.config.ts',
'**/generated/**/*',
'**/$generated-clients/**/*',
'**/$/**/*',
'legacy/**/*',
'build/**/*',
'website/**/*',
Expand Down
5 changes: 5 additions & 0 deletions examples/$/generated-clients/Pokemon/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createPrefilled } from '../../../../src/entrypoints/client.js'

import { $defaultSchemaUrl, $Index } from './SchemaRuntime.js'

export const create = createPrefilled(`Pokemon`, $Index, $defaultSchemaUrl)
17 changes: 17 additions & 0 deletions examples/$/generated-clients/Pokemon/Global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Index } from './Index.js'

declare global {
export namespace GraffleGlobalTypes {
export interface Schemas {
Pokemon: {
name: 'Pokemon'
index: Index
customScalars: {}
featureOptions: {
schemaErrors: true
}
defaultSchemaUrl: null
}
}
}
}
27 changes: 27 additions & 0 deletions examples/$/generated-clients/Pokemon/Index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable */

import type * as Schema from './SchemaBuildtime.js'

export interface Index {
name: 'Pokemon'
Root: {
Query: Schema.Root.Query
Mutation: Schema.Root.Mutation
Subscription: null
}
objects: {
Pokemon: Schema.Object.Pokemon
Trainer: Schema.Object.Trainer
}
unions: {}
interfaces: {}
error: {
objects: {}
objectsTypename: {}
rootResultFields: {
Query: {}
Mutation: {}
Subscription: {}
}
}
}
1 change: 1 addition & 0 deletions examples/$/generated-clients/Pokemon/Scalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../../../src/entrypoints/scalars.js'
90 changes: 90 additions & 0 deletions examples/$/generated-clients/Pokemon/SchemaBuildtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type * as $ from '../../../../src/entrypoints/schema.js'
import type * as $Scalar from './Scalar.ts'

// ------------------------------------------------------------ //
// Root //
// ------------------------------------------------------------ //

export namespace Root {
export type Mutation = $.Object$2<'Mutation', {
addPokemon: $.Field<
$.Output.Nullable<Object.Pokemon>,
$.Args<{
attack: $Scalar.Int
defense: $Scalar.Int
hp: $Scalar.Int
name: $Scalar.String
}>
>
}>

export type Query = $.Object$2<'Query', {
pokemon: $.Field<$.Output.Nullable<$.Output.List<Object.Pokemon>>, null>
pokemonByName: $.Field<
$.Output.Nullable<$.Output.List<Object.Pokemon>>,
$.Args<{
name: $Scalar.String
}>
>
trainerByName: $.Field<
$.Output.Nullable<Object.Trainer>,
$.Args<{
name: $Scalar.String
}>
>
trainers: $.Field<$.Output.Nullable<$.Output.List<Object.Trainer>>, null>
}>
}

// ------------------------------------------------------------ //
// Enum //
// ------------------------------------------------------------ //

export namespace Enum {
// -- no types --
}

// ------------------------------------------------------------ //
// InputObject //
// ------------------------------------------------------------ //

export namespace InputObject {
// -- no types --
}

// ------------------------------------------------------------ //
// Interface //
// ------------------------------------------------------------ //

export namespace Interface {
// -- no types --
}

// ------------------------------------------------------------ //
// Object //
// ------------------------------------------------------------ //

export namespace Object {
export type Pokemon = $.Object$2<'Pokemon', {
attack: $.Field<$.Output.Nullable<$Scalar.Int>, null>
defense: $.Field<$.Output.Nullable<$Scalar.Int>, null>
hp: $.Field<$.Output.Nullable<$Scalar.Int>, null>
id: $.Field<$.Output.Nullable<$Scalar.Int>, null>
name: $.Field<$.Output.Nullable<$Scalar.String>, null>
trainer: $.Field<$.Output.Nullable<Object.Trainer>, null>
}>

export type Trainer = $.Object$2<'Trainer', {
id: $.Field<$.Output.Nullable<$Scalar.Int>, null>
name: $.Field<$.Output.Nullable<$Scalar.String>, null>
pokemon: $.Field<$.Output.Nullable<$.Output.List<Object.Pokemon>>, null>
}>
}

// ------------------------------------------------------------ //
// Union //
// ------------------------------------------------------------ //

export namespace Union {
// -- no types --
}
70 changes: 70 additions & 0 deletions examples/$/generated-clients/Pokemon/SchemaRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable */

import * as $ from '../../../../src/entrypoints/schema.js'
import * as $Scalar from './Scalar.js'

export const $defaultSchemaUrl = undefined

// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Pokemon = $.Object$(`Pokemon`, {
attack: $.field($.Output.Nullable($Scalar.Int)),
defense: $.field($.Output.Nullable($Scalar.Int)),
hp: $.field($.Output.Nullable($Scalar.Int)),
id: $.field($.Output.Nullable($Scalar.Int)),
name: $.field($.Output.Nullable($Scalar.String)),
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
trainer: $.field($.Output.Nullable(() => Trainer)),
})

// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Trainer = $.Object$(`Trainer`, {
id: $.field($.Output.Nullable($Scalar.Int)),
name: $.field($.Output.Nullable($Scalar.String)),
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
pokemon: $.field($.Output.Nullable($.Output.List(() => Pokemon))),
})

// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Mutation = $.Object$(`Mutation`, {
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
addPokemon: $.field(
$.Output.Nullable(() => Pokemon),
$.Args({ attack: $Scalar.Int, defense: $Scalar.Int, hp: $Scalar.Int, name: $Scalar.String }),
),
})

// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Query = $.Object$(`Query`, {
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
pokemon: $.field($.Output.Nullable($.Output.List(() => Pokemon))),
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
pokemonByName: $.field($.Output.Nullable($.Output.List(() => Pokemon)), $.Args({ name: $Scalar.String })),
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
trainerByName: $.field($.Output.Nullable(() => Trainer), $.Args({ name: $Scalar.String })),
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
trainers: $.field($.Output.Nullable($.Output.List(() => Trainer))),
})

export const $Index = {
name: 'Pokemon' as const,
Root: {
Query,
Mutation,
Subscription: null,
},
objects: {
Pokemon,
Trainer,
},
unions: {},
interfaces: {},
error: {
objects: {},
objectsTypename: {},
rootResultFields: {
Query: {},
Mutation: {},
Subscription: {},
},
},
}
47 changes: 47 additions & 0 deletions examples/$/generated-clients/Pokemon/Select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ResultSet, SelectionSet } from '../../../../src/entrypoints/schema.js'
import type { Index } from './Index.js'

// Runtime
// -------

import { createSelect } from '../../../../src/entrypoints/client.js'
export const Select = createSelect(`default`)

// Buildtime
// ---------

export namespace Select {
// Root Types
// ----------

export type Mutation<$SelectionSet extends SelectionSet.Root<Index, 'Mutation'>> = ResultSet.Root<
$SelectionSet,
Index,
'Mutation'
>

export type Query<$SelectionSet extends SelectionSet.Root<Index, 'Query'>> = ResultSet.Root<
$SelectionSet,
Index,
'Query'
>

// Object Types
// ------------

export type Pokemon<$SelectionSet extends SelectionSet.Object<Index['objects']['Pokemon'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['Pokemon'], Index>

export type Trainer<$SelectionSet extends SelectionSet.Object<Index['objects']['Trainer'], Index>> =
ResultSet.Object$<$SelectionSet, Index['objects']['Trainer'], Index>

// Union Types
// -----------

// -- None --

// Interface Types
// ---------------

// -- None --
}
1 change: 1 addition & 0 deletions examples/$/generated-clients/Pokemon/__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as Pokemon from './_.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPrefilled } from '../../../src/entrypoints/client.js'
import { createPrefilled } from '../../../../src/entrypoints/client.js'

import { $defaultSchemaUrl, $Index } from './SchemaRuntime.js'

Expand Down
14 changes: 14 additions & 0 deletions examples/$/generated-clients/SocialStudies/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Include<T, U> = Exclude<T, Exclude<T, U>>

type ObjectWithTypeName = { __typename: string }

const ErrorObjectsTypeNameSelectedEnum = {} as Record<string, ObjectWithTypeName>

const ErrorObjectsTypeNameSelected = Object.values(ErrorObjectsTypeNameSelectedEnum)

type ErrorObjectsTypeNameSelected = (typeof ErrorObjectsTypeNameSelected)[number]

export const isError = <$Value>(value: $Value): value is Include<$Value, ErrorObjectsTypeNameSelected> => {
return typeof value === `object` && value !== null && `__typename` in value
&& ErrorObjectsTypeNameSelected.some(_ => _.__typename === value.__typename)
}
1 change: 1 addition & 0 deletions examples/$/generated-clients/SocialStudies/Scalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../../../src/entrypoints/scalars.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as $ from '../../../src/entrypoints/schema.js'
import type * as $ from '../../../../src/entrypoints/schema.js'
import type * as $Scalar from './Scalar.ts'

// ------------------------------------------------------------ //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */

import * as $ from '../../../src/entrypoints/schema.js'
import * as $ from '../../../../src/entrypoints/schema.js'
import * as $Scalar from './Scalar.js'

export const $defaultSchemaUrl = new URL('https://countries.trevorblades.com/graphql')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ResultSet, SelectionSet } from '../../../src/entrypoints/schema.js'
import type { ResultSet, SelectionSet } from '../../../../src/entrypoints/schema.js'
import type { Index } from './Index.js'

// Runtime
// -------

import { createSelect } from '../../../src/entrypoints/client.js'
import { createSelect } from '../../../../src/entrypoints/client.js'
export const Select = createSelect(`default`)

// Buildtime
Expand Down
3 changes: 3 additions & 0 deletions examples/$/generated-clients/SocialStudies/_.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { create } from './Client.js'
export { isError } from './Error.js'
export { Select } from './Select.js'
53 changes: 53 additions & 0 deletions examples/$/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import getPort from 'get-port'
import type { GraphQLSchema } from 'graphql'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { inspect } from 'node:util'

export const publicGraphQLSchemaEndpoints = {
SocialStudies: `https://countries.trevorblades.com/graphql`,
}

export const showPartition = `---------------------------------------- SHOW ----------------------------------------`

export const show = (value: unknown) => {
console.log(showPartition)
console.log(inspect(value, { depth: null, colors: true }))
}

export const showJson = (value: unknown) => {
console.log(showPartition)
console.log(JSON.stringify(value, null, 2))
}

export const serveSchema = async (input: { schema: GraphQLSchema }) => {
const { schema } = input
const yoga = createYoga({ schema })
const server = createServer(yoga) // eslint-disable-line
const port = await getPort({ port: [3000, 3001, 3002, 3003, 3004] })
const url = new URL(`http://localhost:${String(port)}/graphql`)
server.listen(port)
await new Promise((resolve) =>
server.once(`listening`, () => {
resolve(undefined)
})
)
const stop = async () => {
await new Promise((resolve) => {
server.close(resolve)
setImmediate(() => {
server.emit(`close`)
})
})
}

return {
yoga,
server,
port,
url,
stop,
}
}

export type SchemaServer = Awaited<ReturnType<typeof serveSchema>>
14 changes: 14 additions & 0 deletions examples/$/schemas/pokemon/generateSdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { printSchema } from 'graphql'
import { writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { schema } from './schema.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const sdl = printSchema(schema)
const path = join(__dirname, `./schema.graphql`)
writeFileSync(path, sdl)

console.log(`GraphQL SDL has been written to ${path}`)
Loading