-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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: allow using file paths for scalars:
config like you can for enumValues
#2418
feat: allow using file paths for scalars:
config like you can for enumValues
#2418
Conversation
…enumValues:` This allows you to do: ```yaml config: scalars: DateTime: Date JSON: { [key: string]: any } CustomType: ../scalars#CustomType ``` or even: ```yaml config: scalars: ../scalars ``` and have the scalars be directly imported from other files
That's great! Thank you @ForbesLindesay ! Thanks! |
It took me a pretty long time to make this change relative to what I expected. I don't think I'm going to find time to add tests any time soon. |
All good @ForbesLindesay , I'll add some tests later today. Thank you! |
When I use the |
@shiatsumat can you please share your config file or a reproduction of the issue? |
Thank you for reply. My schema: ./graphql/schema.graphql
documents: ./graphql/usage/*.graphql
generates:
./graphql/_gen_/resolve.ts:
plugins:
- typescript
- typescript-resolvers
config:
preResolveTypes: true
./graphql/_gen_/api.ts:
plugins:
- typescript
- typescript-operations
- typescript-react-apollo
config:
preResolveTypes: true
withComponent: false
withHOC: false
withHooks: true
config:
scalars: ../scalars
enumValues: ../enums The
|
@dotansimha Any ideas...? |
This is because the relevant code doesn't have access to the schema, and this PR was getting huge already. You'll need to add a PR to ensure that the schema is actually passed into the relevant function where that exception is thrown. |
@ForbesLindesay You are absolutely right. I myself cannot find the way to fix the code now though... |
I'll take a look, thanks @ForbesLindesay @shiatsumat |
@shiatsumat @ForbesLindesay |
@dotansimha Wow great, it works!! |
Does anyone know what the contents of |
In case anyone else comes this way looking for the same answer, it turned out that export type PositiveInt = number
export type CountryCode = string
export type PostalCode = string
// etc... GraphQL Code Generator doesn't actually process the file itself however; it merely refers to it in the generated code it creates, with type import statements like this: import { PositiveInt } from '../scalar-types'
import { CountryCode } from '../scalar-types'
import { PostalCode } from '../scalar-types'
// etc... In my case, types of all of my scalars were defined using the import { writeFileSync } from 'fs'
import { scalarResolvers } from './src/scalar-resolvers' // NOTE: these are my scalar resolvers as an object, ready to spread into my resolver config within Apollo server
const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>
const generateTypes = getKeys(scalarResolvers)
.map((name) => `export type ${name} = ${scalarResolvers[name].extensions.codegenScalarType}`)
.join('\n')
writeFileSync('./src/scalar-types.gen.ts', generateTypes) The
Since
Hope that helps someone! |
@dchambers do you think we could add something to the docs to make it easier for people? |
Hi @Urigo 👋 . Yes, absolutely. The current documentation for whereas I would ideally hope to see something more like this: scalarstype: Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type. Usage ExamplesInline Definitionsconfig:
scalars:
DateTime: Date
JSON: { [key: string]: any }
CustomType: ../scalars#CustomType where export type CustomType = string External Definitionsconfig:
scalars: ../scalars.ts where export type DateTime = Date
export type JSON = { [key: string]: any }
export type CustomType = string |
In regards to the potential for API changes, I believe the ideal outcome would be support for pointing directly at For example, the 'External Definitions' section in the example above could be sub-divided into two parts: External Type Definitionsconfig:
scalars: ../scalars.ts where export type DateTime = Date
export type JSON = { [key: string]: any }
export type CustomType = string External Scalar Resolver Definitionsconfig:
scalars: ../scalar-resolvers.ts where import { RegularExpression } from 'graphql-scalars'
export { DateTime, JSON } from 'graphql-scalars'
export const CustomType = new RegularExpression('CustomType', /^ABC$/); |
@dchambers that is super helpful, thank you! |
This allows you to do:
or even: