Skip to content

Commit

Permalink
feat: rename typegenAutoConfig and rootTyping (#723)
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- Renames `typegenAutoConfig` to `sourceTypes`
- Renames `typegenAutoConfig.sources` to `sourceTypes.modules`
- Renames `typegenAutoConfig.sources[].source` to `sourcesTypes.modules[].module`
- Renames `rootTyping` to `sourceType`
  • Loading branch information
Weakky committed Dec 10, 2020
1 parent 642be8b commit d66e8f1
Show file tree
Hide file tree
Showing 37 changed files with 233 additions and 261 deletions.
Expand Up @@ -128,14 +128,9 @@ export const schema = makeSchema({
typegen: join(__dirname, '..', 'nexus-typegen.ts'),
schema: join(__dirname, '..', 'schema.graphql')
},
+ typegenAutoConfig: {
+ sources: [
+ {
+ source: join(__dirname, "./context.ts"), // 1
+ alias: "ContextModule", // 2
+ },
+ ],
+ contextType: "ContextModule.Context", // 3
+ contextType: { // 1
+ module: join(__dirname, "./context.ts"), // 2
+ alias: "ContextModule", // 3
+ },
})
```
Expand All @@ -156,14 +151,9 @@ export const schema = makeSchema({
typegen: join(__dirname, '..', 'nexus-typegen.ts'),
schema: join(__dirname, '..', 'schema.graphql')
},
typegenAutoConfig: {
sources: [
{
source: join(__dirname, "./context.ts"), // 1
alias: "ContextModule", // 2
},
],
contextType: "ContextModule.Context", // 3
contextType: { // 1
module: join(__dirname, "./context.ts"), // 2
alias: "ContextModule", // 3
},
})
```
Expand All @@ -172,9 +162,9 @@ export const schema = makeSchema({

</TabbedContent>

1. Behind the scenes Nexus will parse the `context.ts` file to extract the available types.
1. These types become available under the umbrella of our `ContextModule` alias.
1. We can then use that alias to refer to any type defined in this file. Here, our context type is named `Context`, so we refer to it as `ContextModule.Context`.
1. Option to set the context type
1. Path to the module where the context type is exported
1. Name of the export in that module

## Use the context

Expand Down
30 changes: 15 additions & 15 deletions docs/content/014-guides/022-source-types.mdx
Expand Up @@ -175,7 +175,7 @@ Configuring your Source Types globally is the recommended way in most cases. Typ
When that's the case, you can configure Nexus to automatically map your Source Types to your GraphQL Object types based on their name.
This is all done in the `typegenAutoConfig` option of `makeSchema`.
This is all done in the `sourceTypes` option of `makeSchema`.
#### Exact Matching Strategy
Expand Down Expand Up @@ -236,9 +236,9 @@ const Query = queryType({

const schema = makeSchema({
types: [User, Query],
typegenAutoConfig: {
sources: [{
source: path.join(__dirname, 'db.ts'),
sourceTypes: {
modules: [{
module: path.join(__dirname, 'db.ts'),
// ^---------------------- Path to the file containing your Source Types
alias: 'db'
// ^----- Arbitrary unique name used as alias to import your Source Types.
Expand Down Expand Up @@ -324,9 +324,9 @@ const Query = queryType({

const schema = makeSchema({
types: [User, Query],
typegenAutoConfig: {
sources: [{
source: path.join(__dirname, 'db.ts'),
sourceTypes: {
modules: [{
module: path.join(__dirname, 'db.ts'),
// ^----------------------------- Path to the file containing your Source Types
alias: 'db',
// ^------------------------------ Name used as alias for Nexus to import your Source Types. eg: import * as <name> from <path>
Expand All @@ -349,9 +349,9 @@ In this case, each regex will be run against your source code in the order in wh
```ts
const schema = makeSchema({
typegenAutoConfig: {
sources: [{
source: path.join(__dirname, 'db.ts'),
sourceTypes: {
modules: [{
module: path.join(__dirname, 'db.ts'),
alias: 'db',
typeMatch(type, defaultRegex) {
return [
Expand Down Expand Up @@ -396,9 +396,9 @@ const User = objectType({
})

const schema = makeSchema({
typegenAutoConfig: {
sources: [{
source: path.join(__dirname, 'db.ts'),
sourceTypes: {
modules: [{
module: path.join(__dirname, 'db.ts'),
// ^---------------- Path to the file containing your Source Types
alias: 'db',
// ^----------------- Name used as alias for Nexus to import your Source Types.
Expand All @@ -420,7 +420,7 @@ However, if _none of your Source Types names can match your GraphQL Object type
Nexus will not output any errors if your Source Types aren't found.
That being said, if you happen to have some Source Types that are not picked up by Nexus, you can enable the `debug` flag in the `typegenAutoConfig` configuration.
That being said, if you happen to have some Source Types that are not picked up by Nexus, you can enable the `debug` flag in the `sourceTypes` configuration.
Nexus will output debug logs, giving your detailed information about which Source Types were found, which were not, and why.
Expand All @@ -429,7 +429,7 @@ import { makeSchema } from '@nexus/schema'

const schema = makeSchema({
// ...
typegenAutoConfig: {
sourceTypes: {
// ...
debug: true
}
Expand Down
72 changes: 32 additions & 40 deletions docs/content/015-api/070-make-schema.mdx
Expand Up @@ -47,15 +47,15 @@ export const schema = makeSchema({
})
```

## shouldGenerateArtifacts, outputs, typegenAutoConfig
## shouldGenerateArtifacts, outputs, sourceTypes

The `shouldGenerateArtifacts` is a boolean value which determines whether artifact files (graphql and TypeScript) are emitted when the code for `makeSchema`

`outputs` is an object which specifies the absolute path for where the emitted files are generated.
If you do not wish to generate one of these types

`typegenAutoConfig` is an object which gives nexus more information about how to properly generate the
type definition file. An example of the `sources` is provided below:
`sourceTypes` is an object which gives nexus more information about how to properly generate the
type definition file. An example of the usage is provided below:

```ts
makeSchema({
Expand All @@ -66,27 +66,25 @@ makeSchema({
schema: path.join(__dirname, 'generated/schema.gen.graphql'),
typegen: path.join(__dirname, 'generated/nexusTypes.gen.ts'),
},
typegenAutoConfig: {
sourceTypes: {
headers: [
'import { ConnectionFieldOpts } from "@packages/api-graphql/src/extensions/connectionType"',
],
sources: [
modules: [
// Automatically finds any interface/type/class named similarly to the and infers it
// the "source" type of that resolver.
{
source: '@packages/types/src/db.ts',
module: '@packages/types/src/db.ts',
alias: 'dbt',
typeMatch: (name) => new RegExp(`(?:interface|type|class)\\s+(${name}s?)\\W`, 'g'),
},
// We also need to import this source in order to provide it as the `contextType` below.
{
source: '@packages/data-context/src/DataContext.ts',
alias: 'ctx',
typeMatch: name => new RegExp(`(?:interface|type|class)\\s+(${name}s?)\\W`, 'g'),
},
],
// Typing from the source
contextType: 'ctx.DataContext',
backingTypeMap: {
// Typing for the GraphQL context
contextType: {
module: '@packages/data-context/src/DataContext.ts',
alias: 'ctx',
},
mapping: {
Date: 'Date',
DateTime: 'Date',
UUID: 'string',
Expand Down Expand Up @@ -144,63 +142,57 @@ Optional, allows you to override the `printSchema` when outputting the generated
```ts
makeSchema({
// ...
customPrintSchemaFn: (schema) => {
customPrintSchemaFn: schema => {
return printSchema(schema, { commentDescriptions: true })
},
})
```

#### Footnotes: Annotated config option for typegenAutoConfig:
#### Footnotes: Annotated config option for sourceTypes:

```ts
export interface TypegenAutoConfigOptions {
/**
* Any headers to prefix on the generated type file
*/
export interface SourceTypesConfigOptions {
/** Any headers to prefix on the generated type file */
headers?: string[]
/**
* Array of files to match for a type
* Array of SourceTypeModule's to look in and match the type names against.
*
* sources: [
* { source: 'typescript', alias: 'ts' },
* { source: path.join(__dirname, '../backingTypes'), alias: 'b' },
* @example
* modules: [
* { module: 'typescript', alias: 'ts' },
* { module: path.join(__dirname, '../sourceTypes'), alias: 'b' },
* ]
*/
sources: TypegenConfigSourceModule[]
/**
* Typing for the context, referencing a type defined in the aliased module
* provided in sources e.g. 'alias.Context'
*/
contextType?: string
modules: SourceTypeModule[]
/**
* Types that should not be matched for a backing type,
* Types that should not be matched for a source type,
*
* By default this is set to ['Query', 'Mutation', 'Subscription']
*
* @example
* skipTypes: ['Query', 'Mutation', /(.*?)Edge/, /(.*?)Connection/]
*/
skipTypes?: (string | RegExp)[]
/**
* If debug is set to true, this will log out info about all types
* found, skipped, etc. for the type generation files.
* If debug is set to true, this will log out info about all types found, skipped, etc. for the type
* generation files. @default false
*/
debug?: boolean
/**
* If provided this will be used for the backing types rather than the auto-resolve
* mechanism above. Useful as an override for one-off cases, or for scalar
* backing types.
* If provided this will be used for the source types rather than the auto-resolve mechanism above. Useful
* as an override for one-off cases, or for scalar source types.
*/
backingTypeMap?: Record<string, string>
mapping?: Record<string, string>
}

export interface TypegenConfigSourceModule {
export interface SourceTypeModule {
/**
* The module for where to look for the types.
* This uses the node resolution algorithm via require.resolve,
* so if this lives in node_modules, you can just provide the module name
* otherwise you should provide the absolute path to the file.
*/
source: string
module: string
/**
* When we import the module, we use 'import * as ____' to prevent
* conflicts. This alias should be a name that doesn't conflict with any other
Expand Down
28 changes: 11 additions & 17 deletions docs/content/040-adoption-guides/020-nexus-framework-users.mdx
Expand Up @@ -376,10 +376,7 @@ Nexus Framework has some builtin logging functionality. You can approximate it a
}

Nexus.makeSchema({
typegenAutoConfig: {
sources: [{ source: __filename, alias: 'ContextModule' }]
contextType: 'ContextModule.Context',
},
contextType: { source: __filename, alias: 'ContextModule' },
...
})
```
Expand Down Expand Up @@ -452,12 +449,9 @@ import * as Nexus from '@nexus/schema'
import * as Path from 'path'

Nexus.makeSchema({
typegenAutoconfig: {
sources: [{
source: Path.join(__dirname, './path/to/contextModule'),
alias: 'ContextModule'
}],
contextType: 'ContextModule.Context',
contextType: {
source: Path.join(__dirname, './path/to/contextModule'),
alias: 'ContextModule'
},
...
})
Expand All @@ -482,10 +476,10 @@ In Nexus Framework backing types worked like this:
Nexus Schema does not have this functionality. There are a few things you can do instead.
- Approach A: If you have a set of exported Backing types whose names match your GraphQL objects then you can set their modules as `sources` in `typegenAutoconfig` and Nexus Schema will take care of the rest.
- Approach B: If you have a set of Backing types whose names having a differing pattern than your GraphQL objects then supply a Regular Expression to the `typeMatch` property in `typegenAutoconfig`. You can for example map `BackingUser -> User` , `BackingPost -> Post` and so on.
- Approach C: If you have a one-off mismatch then supply an arbitrary mapping to the `backingTypeMap` property of `typegenAutoconfig`.
- Approach D: If you want to colocate a type mapping like how it was in Nexus Framework then use the `rootTyping` field on Nexus Schema type defs. Unlike framework it accepts an object with a `path` to a module and `name` of the type to look for being exported from that module.
- Approach A: If you have a set of exported Backing types whose names match your GraphQL objects then you can set their `modules` in `sourceTypes` and Nexus Schema will take care of the rest.
- Approach B: If you have a set of Backing types whose names having a differing pattern than your GraphQL objects then supply a Regular Expression to the `typeMatch` property in `sourceTypes`. You can for example map `BackingUser -> User` , `BackingPost -> Post` and so on.
- Approach C: If you have a one-off mismatch then supply an arbitrary mapping to the `mapping` property of `sourceTypes`.
- Approach D: If you want to colocate a type mapping like how it was in Nexus Framework then use the `sourceType` field on Nexus Schema type defs. Unlike framework it accepts an object with a `path` to a module and `name` of the type to look for being exported from that module.
Here is an example using approach A:
Expand All @@ -505,9 +499,9 @@ import * as Nexus from '@nexus/schema'
import * as Path from 'path'

Nexus.makeSchema({
typegenAutoconfig: {
sources: [{
source: Path.join(__dirname, 'path/to/someModule'),
sourceTypes: {
modules: [{
module: Path.join(__dirname, 'path/to/someModule'),
alias: 'SomeAlias'
}],
},
Expand Down
Expand Up @@ -64,10 +64,10 @@ import { makeSchema } from '@nexus/schema'
import { nexusPrisma } from 'nexus-plugin-prisma'

export const schema = makeSchema({
+ typegenAutoConfig: {
+ sources: [
+ sourceTypes: {
+ modules: [
+ {
+ source: require.resolve('.prisma/client/index.d.ts'),
+ module: require.resolve('.prisma/client/index.d.ts'),
+ alias: "prisma",
+ }
+ ]
Expand Down Expand Up @@ -100,18 +100,17 @@ import { makeSchema } from '@nexus/schema'
import { nexusPrisma } from 'nexus-plugin-prisma'

export const schema = makeSchema({
typegenAutoConfig: {
sources: [
sourceTypes: {
modules: [
{
source: require.resolve('.prisma/client/index.d.ts'),
alias: "prisma",
},
+ {
+ source: require.resolve('./context'),
+ alias: 'ContextModule'
+ }
],
+ contextType: 'ContextModule.Context'
+ contextType: {
+ source: require.resolve('./context'),
+ alias: 'ContextModule'
+ }
}
plugins: [
nexusPrisma({
Expand Down Expand Up @@ -139,18 +138,17 @@ If your Prisma Schema is using either the `Json` or `DateTime` type, the framewo
+import { DateTimeResolver, JSONObjectResolver } from 'graphql-scalars'

export const schema = makeSchema({
typegenAutoConfig: {
sources: [
sourceTypes: {
modules: [
{
source: require.resolve('.prisma/client/index.d.ts'),
module: require.resolve('.prisma/client/index.d.ts'),
alias: "prisma",
},
{
source: require.resolve('./context'),
alias: 'ContextModule'
}
],
contextType: 'ContextModule.Context'
contextType: {
module: require.resolve('./context'),
alias: 'ContextModule'
}
}
plugins: [
nexusPrisma({
Expand Down

0 comments on commit d66e8f1

Please sign in to comment.