-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts-client): generated namespace and client ctor (#815)
- Loading branch information
1 parent
5410032
commit fb715d9
Showing
49 changed files
with
838 additions
and
635 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from '../../layers/5_client/client.js' | ||
export { Client, create, createPrefilled, Input, InputPrefilled } from '../../layers/5_client/client.js' | ||
export { create as createSelect, select } from '../../layers/5_select/select.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createCodeGenerator } from '../createCodeGenerator.js' | ||
import { moduleNameSchemaRuntime } from './SchemaRuntime.js' | ||
|
||
export const { generate: generateClient, moduleName: moduleNameClient } = createCodeGenerator( | ||
`Client`, | ||
(config) => { | ||
const code: string[] = [] | ||
|
||
code.push( | ||
`import { createPrefilled } from '${config.libraryPaths.client}'`, | ||
`import { $Index } from './${moduleNameSchemaRuntime}.js'`, | ||
``, | ||
`export const create = createPrefilled(\`${config.name}\`, $Index)`, | ||
) | ||
|
||
return code.join(`\n\n`) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,67 @@ | ||
import { isUnionType } from 'graphql' | ||
import { Code } from '../../../lib/Code.js' | ||
import { hasMutation, hasQuery, hasSubscription, unwrapToNamed } from '../../../lib/graphql.js' | ||
import type { Config } from './generateCode.js' | ||
import { createCodeGenerator } from '../createCodeGenerator.js' | ||
import { moduleNameSchemaBuildtime } from './SchemaBuildtime.js' | ||
|
||
export const moduleNameIndex = `Index` | ||
export const { generate: generateIndex, moduleName: moduleNameIndex } = createCodeGenerator( | ||
`Index`, | ||
(config) => { | ||
const namespace = `Schema` | ||
const code = [] | ||
code.push(`/* eslint-disable */\n`) | ||
code.push(`import type * as ${namespace} from './${moduleNameSchemaBuildtime}.js'\n`) | ||
|
||
export const generateIndex = (config: Config) => { | ||
const namespace = `Schema` | ||
const code = [] | ||
code.push(`/* eslint-disable */\n`) | ||
code.push(`import type * as ${namespace} from './${moduleNameSchemaBuildtime}.js'\n`) | ||
|
||
code.push(Code.export$( | ||
Code.interface$( | ||
`Index`, | ||
Code.objectFrom({ | ||
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, | ||
}), | ||
}, | ||
objects: Code.objectFromEntries( | ||
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${namespace}.Object.${_.name}`]), | ||
), | ||
unions: Code.objectFromEntries( | ||
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]), | ||
), | ||
interfaces: Code.objectFromEntries( | ||
config.typeMapByKind.GraphQLInterfaceType.map(_ => [_.name, `${namespace}.Interface.${_.name}`]), | ||
), | ||
// todo jsdoc comment saying: | ||
// Objects that match this pattern name: /.../ | ||
error: Code.objectFrom({ | ||
code.push(Code.export$( | ||
Code.interface$( | ||
`Index`, | ||
Code.objectFrom({ | ||
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, | ||
}), | ||
}, | ||
objects: Code.objectFromEntries( | ||
config.error.objects.map(_ => [_.name, `${namespace}.Object.${_.name}`]), | ||
config.typeMapByKind.GraphQLObjectType.map(_ => [_.name, `${namespace}.Object.${_.name}`]), | ||
), | ||
objectsTypename: Code.objectFromEntries( | ||
config.error.objects.map(_ => [_.name, `{ __typename: "${_.name}" }`]), | ||
unions: Code.objectFromEntries( | ||
config.typeMapByKind.GraphQLUnionType.map(_ => [_.name, `${namespace}.Union.${_.name}`]), | ||
), | ||
rootResultFields: `{ | ||
interfaces: Code.objectFromEntries( | ||
config.typeMapByKind.GraphQLInterfaceType.map(_ => [_.name, `${namespace}.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}`]), | ||
), | ||
objectsTypename: Code.objectFromEntries( | ||
config.error.objects.map(_ => [_.name, `{ __typename: "${_.name}" }`]), | ||
), | ||
rootResultFields: `{ | ||
${ | ||
Object.entries(config.rootTypes).map(([rootTypeName, rootType]) => { | ||
if (!rootType) return `${rootTypeName}: {}` | ||
Object.entries(config.rootTypes).map(([rootTypeName, rootType]) => { | ||
if (!rootType) return `${rootTypeName}: {}` | ||
const resultFields = Object.values(rootType.getFields()).filter((field) => { | ||
const type = unwrapToNamed(field.type) | ||
return isUnionType(type) | ||
&& type.getTypes().some(_ => config.error.objects.some(__ => __.name === _.name)) | ||
}).map((field) => field.name) | ||
const resultFields = Object.values(rootType.getFields()).filter((field) => { | ||
const type = unwrapToNamed(field.type) | ||
return isUnionType(type) | ||
&& type.getTypes().some(_ => config.error.objects.some(__ => __.name === _.name)) | ||
}).map((field) => field.name) | ||
return `${rootType.name}: {\n${resultFields.map(_ => `${_}: "${_}"`).join(`,\n`)} }` | ||
}).join(`\n`) | ||
} | ||
return `${rootType.name}: {\n${resultFields.map(_ => `${_}: "${_}"`).join(`,\n`)} }` | ||
}).join(`\n`) | ||
} | ||
}`, | ||
}), | ||
}), | ||
}), | ||
), | ||
)) | ||
), | ||
)) | ||
|
||
return { | ||
code: code.join(`\n`), | ||
moduleName: moduleNameIndex, | ||
} | ||
} | ||
return code.join(`\n`) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
import type { Config } from './generateCode.js' | ||
import { createCodeGenerator } from '../createCodeGenerator.js' | ||
|
||
export const moduleNameScalar = `Scalar` | ||
export const { generate: generateScalar, moduleName: moduleNameScalar } = createCodeGenerator( | ||
`Scalar`, | ||
(config) => { | ||
let code = `` | ||
|
||
export const generateScalar = (config: Config) => { | ||
let code = `` | ||
// todo test case for when this is true | ||
const needsDefaultCustomScalarImplementation = config.typeMapByKind.GraphQLScalarTypeCustom.length > 0 | ||
&& !config.options.customScalars | ||
|
||
// todo test case for when this is true | ||
const needsDefaultCustomScalarImplementation = config.typeMapByKind.GraphQLScalarTypeCustom.length > 0 | ||
&& !config.options.customScalars | ||
|
||
const StandardScalarNamespace = `StandardScalar` | ||
code += ` | ||
const StandardScalarNamespace = `StandardScalar` | ||
code += ` | ||
${ | ||
needsDefaultCustomScalarImplementation | ||
? `import * as ${StandardScalarNamespace} from '${config.libraryPaths.scalars}'` | ||
: `` | ||
} | ||
needsDefaultCustomScalarImplementation | ||
? `import * as ${StandardScalarNamespace} from '${config.libraryPaths.scalars}'` | ||
: `` | ||
} | ||
export * from '${config.libraryPaths.scalars}' | ||
${config.options.customScalars ? `export * from '${config.importPaths.customScalarCodecs}'` : ``} | ||
` | ||
|
||
if (needsDefaultCustomScalarImplementation) { | ||
console.log( | ||
`WARNING: Custom scalars detected in the schema, but you have not created a custom scalars module to import implementations from.`, | ||
) | ||
code += ` | ||
if (needsDefaultCustomScalarImplementation) { | ||
console.log( | ||
`WARNING: Custom scalars detected in the schema, but you have not created a custom scalars module to import implementations from.`, | ||
) | ||
code += ` | ||
${ | ||
config.typeMapByKind.GraphQLScalarTypeCustom | ||
.flatMap((_) => { | ||
return [ | ||
`export const ${_.name} = ${StandardScalarNamespace}.String`, | ||
`export type ${_.name} = ${StandardScalarNamespace}.String`, | ||
] | ||
}).join(`\n`) | ||
} | ||
config.typeMapByKind.GraphQLScalarTypeCustom | ||
.flatMap((_) => { | ||
return [ | ||
`export const ${_.name} = ${StandardScalarNamespace}.String`, | ||
`export type ${_.name} = ${StandardScalarNamespace}.String`, | ||
] | ||
}).join(`\n`) | ||
} | ||
` | ||
} | ||
} | ||
|
||
return { | ||
code, | ||
moduleName: moduleNameScalar, | ||
} | ||
} | ||
return code | ||
}, | ||
) |
Oops, something went wrong.