Skip to content

Commit

Permalink
improve: avoid conflict import paths for Twoslash (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Sep 6, 2024
1 parent 534e043 commit 4182be3
Show file tree
Hide file tree
Showing 59 changed files with 206 additions and 69 deletions.
1 change: 0 additions & 1 deletion examples/$/generated-clients/SocialStudies/Scalar.ts

This file was deleted.

12 changes: 9 additions & 3 deletions examples/$/generated-clients/SocialStudies/_.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export { create } from './Client.js'
export { isError } from './Error.js'
export { Select } from './Select.js'
// We import the global module for good measure although it is not clear it is always needed.
// It at least helps with Twoslash wherein without this import here Twoslash will not include the global module.
// In real TypeScript projects it seems the global module is included automatically. But there could be certain tsconfig
// setups where this still indeed does help.
import './modules/Global.js'

export { create } from './modules/Client.js'
export { isError } from './modules/Error.js'
export { Select } from './modules/Select.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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Index } from './Index.js'
import type { Index } from './SchemaIndex.js'

declare global {
export namespace GraffleGlobalTypes {
Expand Down
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 { Index } from './Index.js'
import type { ResultSet, SelectionSet } from '../../../../../src/entrypoints/schema.js'
import type { Index } from './SchemaIndex.js'

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

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

// Buildtime
Expand Down
1 change: 0 additions & 1 deletion examples/$/generated-clients/pokemon/Scalar.ts

This file was deleted.

12 changes: 9 additions & 3 deletions examples/$/generated-clients/pokemon/_.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export { create } from './Client.js'
export { isError } from './Error.js'
export { Select } from './Select.js'
// We import the global module for good measure although it is not clear it is always needed.
// It at least helps with Twoslash wherein without this import here Twoslash will not include the global module.
// In real TypeScript projects it seems the global module is included automatically. But there could be certain tsconfig
// setups where this still indeed does help.
import './modules/Global.js'

export { create } from './modules/Client.js'
export { isError } from './modules/Error.js'
export { Select } from './modules/Select.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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Index } from './Index.js'
import type { Index } from './SchemaIndex.js'

declare global {
export namespace GraffleGlobalTypes {
Expand Down
1 change: 1 addition & 0 deletions examples/$/generated-clients/pokemon/modules/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 = undefined
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 { Index } from './Index.js'
import type { ResultSet, SelectionSet } from '../../../../../src/entrypoints/schema.js'
import type { Index } from './SchemaIndex.js'

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

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

// Buildtime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---------------------------------------- SHOW ----------------------------------------
{
methodMode: 'post',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
authorization: 'Bearer MY_TOKEN'
},
signal: undefined,
mode: 'cors',
method: 'post',
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}'
}
2 changes: 2 additions & 0 deletions examples/transport-http|transport-http_abort.output.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---------------------------------------- SHOW ----------------------------------------
'This operation was aborted'
8 changes: 8 additions & 0 deletions examples/transport-http|transport-http_fetch.output.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---------------------------------------- SHOW ----------------------------------------
{
"countries": [
{
"name": "Canada Mocked!"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---------------------------------------- SHOW ----------------------------------------
{
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': 'DYNAMIC_VALUE'
},
signal: undefined,
method: 'post',
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}'
}
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': '1725656299249'
'x-sent-at-time': '1725665559439'
},
signal: undefined,
method: 'post',
Expand Down
48 changes: 48 additions & 0 deletions examples/transport-http|transport-http_method-get.output.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---------------------------------------- SHOW ----------------------------------------
{
methodMode: 'getReads',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json'
},
signal: undefined,
method: 'post',
url: URL {
href: 'http://localhost:3000/graphql',
origin: 'http://localhost:3000',
protocol: 'http:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/graphql',
search: '',
searchParams: URLSearchParams {},
hash: ''
},
body: '{"query":"mutation addPokemon(attack:0, defense:0, hp:1, name:\\"Nano\\") { name }"}'
}
---------------------------------------- SHOW ----------------------------------------
{
methodMode: 'getReads',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8'
},
signal: undefined,
method: 'get',
url: URL {
href: 'http://localhost:3000/graphql?query=query+%7B+pokemonByName%28name%3A%22Nano%22%29+%7B+hp+%7D+%7D',
origin: 'http://localhost:3000',
protocol: 'http:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/graphql',
search: '?query=query+%7B+pokemonByName%28name%3A%22Nano%22%29+%7B+hp+%7D+%7D',
searchParams: URLSearchParams { 'query' => 'query { pokemonByName(name:"Nano") { hp } }' },
hash: ''
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"graffle": "tsx ./src/cli/generate.ts",
"gen:test:schema": "tsx tests/_/schemaGenerate.ts",
"gen:examples": "tsx scripts/generate-examples-derivatives/generate.ts && pnpm format",
"gen:docs:examples:clients": "tsx './examples/$/schemas/pokemon/generateSdl.ts' && pnpm graffle --name Pokemon --schema './examples/$/schemas/pokemon/schema.graphql' --output './examples/$/generated-clients/pokemon' --libraryPathClient ../../../../src/entrypoints/client.js --libraryPathSchema ../../../../src/entrypoints/schema.js --libraryPathScalars ../../../../src/entrypoints/scalars.js && pnpm graffle --name SocialStudies --schema https://countries.trevorblades.com/graphql --output './examples/$/generated-clients/SocialStudies' --libraryPathClient ../../../../src/entrypoints/client.js --libraryPathSchema ../../../../src/entrypoints/schema.js --libraryPathScalars ../../../../src/entrypoints/scalars.js",
"gen:examples:clients": "tsx './examples/$/schemas/pokemon/generateSdl.ts' && pnpm graffle --name Pokemon --schema './examples/$/schemas/pokemon/schema.graphql' --output './examples/$/generated-clients/pokemon' --libraryPathClient ../../../../../src/entrypoints/client.js --libraryPathSchema ../../../../../src/entrypoints/schema.js --libraryPathScalars ../../../../../src/entrypoints/scalars.js && pnpm graffle --name SocialStudies --schema https://countries.trevorblades.com/graphql --output './examples/$/generated-clients/SocialStudies' --libraryPathClient ../../../../../src/entrypoints/client.js --libraryPathSchema ../../../../../src/entrypoints/schema.js --libraryPathScalars ../../../../../src/entrypoints/scalars.js",
"dev": "rm -rf dist && tsc --watch",
"format": "pnpm build:docs && dprint fmt",
"lint": "eslint . --fix",
Expand Down
6 changes: 2 additions & 4 deletions scripts/generate-examples-derivatives/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ const transformRewriteGraffleImports = (example: Example) => {
)
.replaceAll(
`import { SocialStudies } from './$/generated-clients/SocialStudies/__.js'`,
`import './graffle/Global.js'
// ---cut---
`// ---cut---
import { Graffle as SocialStudies } from './graffle/__.js'`,
)
.replaceAll(
/import ({[^}]+}) from '.\/\$\/generated-clients\/([^/]+)\/__\.js'/g,
`import './$2/Global.js'
// ---cut---
`// ---cut---
import $1 from './$2/__.js'`,
)
// Any remaining $ imports are entirely removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { createCodeGenerator } from '../createCodeGenerator.js'
import { moduleNameSchemaBuildtime } from './SchemaBuildtime.js'

export const { generate: generateIndex, moduleName: moduleNameIndex } = createCodeGenerator(
`Index`,
`SchemaIndex`,
(config) => {
const namespace = `Schema`
const SchemaNamespace = `Schema`
const code = []
code.push(`/* eslint-disable */\n`)
code.push(`import type * as ${namespace} from './${moduleNameSchemaBuildtime}.js'\n`)
code.push(`import type * as ${SchemaNamespace} from './${moduleNameSchemaBuildtime}.js'\n`)

code.push(Code.export$(
Code.interface$(
Expand All @@ -19,25 +19,25 @@ export const { generate: generateIndex, moduleName: moduleNameIndex } = createCo
name: Code.quote(config.name),
Root: {
type: Code.objectFrom({
Query: hasQuery(config.typeMapByKind) ? `${namespace}.Root.Query` : null,
Mutation: hasMutation(config.typeMapByKind) ? `${namespace}.Root.Mutation` : null,
Subscription: hasSubscription(config.typeMapByKind) ? `${namespace}.Root.Subscription` : null,
Query: hasQuery(config.typeMapByKind) ? `${SchemaNamespace}.Root.Query` : null,
Mutation: hasMutation(config.typeMapByKind) ? `${SchemaNamespace}.Root.Mutation` : null,
Subscription: hasSubscription(config.typeMapByKind) ? `${SchemaNamespace}.Root.Subscription` : null,
}),
},
objects: Code.objectFromEntries(
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${namespace}.Object.${_.name}`]),
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${SchemaNamespace}.Object.${_.name}`]),
),
unions: Code.objectFromEntries(
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]),
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${SchemaNamespace}.Union.${_.name}`]),
),
interfaces: Code.objectFromEntries(
config.typeMapByKind.GraphQLInterfaceType.map(_ => [_.name, `${namespace}.Interface.${_.name}`]),
config.typeMapByKind.GraphQLInterfaceType.map(_ => [_.name, `${SchemaNamespace}.Interface.${_.name}`]),
),
// todo jsdoc comment saying:
// Objects that match this pattern name: /.../
error: Code.objectFrom({
objects: Code.objectFromEntries(
config.error.objects.map(_ => [_.name, `${namespace}.Object.${_.name}`]),
config.error.objects.map(_ => [_.name, `${SchemaNamespace}.Object.${_.name}`]),
),
objectsTypename: Code.objectFromEntries(
config.error.objects.map(_ => [_.name, `{ __typename: "${_.name}" }`]),
Expand Down
2 changes: 1 addition & 1 deletion src/layers/2_generator/code/Select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createCodeGenerator } from '../createCodeGenerator.js'
import { title, typeTitle } from '../helpers.js'
import { moduleNameIndex } from './Index.js'
import { moduleNameIndex } from './SchemaIndex.js'

export const { generate: generateSelect, moduleName: moduleNameSelect } = createCodeGenerator(
`Select`,
Expand Down
12 changes: 9 additions & 3 deletions src/layers/2_generator/code/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ export const { generate: generate_, moduleName: moduleName_ } = createCodeGenera
(_config) => {
const code: string[] = []
code.push(
`export { Select } from './${moduleNameSelect}.js'`,
`export { isError } from './${moduleNameError}.js'`,
`export { create } from './${moduleNameClient}.js'`,
`// We import the global module for good measure although it is not clear it is always needed.`,
`// It at least helps with Twoslash wherein without this import here Twoslash will not include the global module.`,
`// In real TypeScript projects it seems the global module is included automatically. But there could be certain tsconfig`,
`// setups where this still indeed does help.`,
`import './modules/Global.js'`,
``,
`export { Select } from './modules/${moduleNameSelect}.js'`,
`export { isError } from './modules/${moduleNameError}.js'`,
`export { create } from './modules/${moduleNameClient}.js'`,
)

return code.join(`\n`)
Expand Down
2 changes: 1 addition & 1 deletion src/layers/2_generator/code/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCodeGenerator } from '../createCodeGenerator.js'
import { moduleNameIndex } from './Index.js'
import { moduleNameIndex } from './SchemaIndex.js'

export const { moduleName: moduleNameGlobal, generate: generateGlobal } = createCodeGenerator(
`Global`,
Expand Down
7 changes: 6 additions & 1 deletion src/layers/2_generator/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ export const generateFiles = async (input: Input) => {
},
})

// todo clear directory before generating so that removed or renamed files are cleaned up.
await fs.mkdir(outputDirPath, { recursive: true })
await fs.mkdir(`${outputDirPath}/modules`, { recursive: true })
await Promise.all(
codes.map((code) => {
return fs.writeFile(`${outputDirPath}/${code.moduleName}.ts`, code.code, { encoding: `utf8` })
const isIndexModule = code.moduleName.match(/^_+$/) !== null
return fs.writeFile(`${outputDirPath}/${isIndexModule ? `` : `modules/`}${code.moduleName}.ts`, code.code, {
encoding: `utf8`,
})
}),
)
}
2 changes: 1 addition & 1 deletion src/layers/2_generator/generateCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { generate__ } from './code/__.js'
import { generateClient } from './code/Client.js'
import { generateError } from './code/Error.js'
import { generateGlobal } from './code/global.js'
import { generateIndex } from './code/Index.js'
import { generateScalar } from './code/Scalar.js'
import { generateSchemaBuildtime } from './code/SchemaBuildtime.js'
import { generateIndex } from './code/SchemaIndex.js'
import { generateRuntimeSchema } from './code/SchemaRuntime.js'
import { generateSelect } from './code/Select.js'

Expand Down
1 change: 0 additions & 1 deletion website/content/examples/generated-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ aside: false

<!-- dprint-ignore-start -->
```ts twoslash
import './graffle/Global.js'
// ---cut---
import { Graffle as SocialStudies } from './graffle/__.js'

Expand Down
2 changes: 1 addition & 1 deletion website/content/examples/transport-http-dynamic-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ await graffle.rawString({ document: `{ languages { code } }` })
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1725656299249'
'x-sent-at-time': '1725665559439'
},
signal: undefined,
method: 'post',
Expand Down
1 change: 0 additions & 1 deletion website/content/examples/transport-http-method-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ to be sent over HTTP GET method. Note write-kind operations (mutation) are still

<!-- dprint-ignore-start -->
```ts twoslash
import './pokemon/Global.js'
// ---cut---
import { Pokemon } from './pokemon/__.js'

Expand Down
Loading

0 comments on commit 4182be3

Please sign in to comment.