graphql-code-generator
graphql-code-generator copied to clipboard
Issue #7615: allow fields to reference interfaces in typescript-mongodb plugin
Description
This change allows users to reference interfaces in the type fields of their GraphQL schema and generate correct MongoDB output. Previously, using the @link directive would cause the typescript-mongodb plugin to always assume the @link referenced an object, when the GraphQL spec explicitly allows users to reference interfaces. This would cause the generated TypeScript to break immediately as it would try to reference a DbObject that was never generated, because it should have instead tried to reference a DbInterface.
Related #7615
Type of change
Please delete options that are not relevant.
- [X] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [X] This change requires a documentation update
Screenshots/Sandbox (if appropriate/relevant):
I don't believe this is relevant, but let me know if you'd prefer something.
How Has This Been Tested?
A new test was added to the bottom of typescript-mongo.spec.ts that checks that referecing an interface in a type's field does not try to access output that was never generated.
- [X]
Interfaces should be valid field types, not just objects - issue #7615
Test Environment:
- OS: macOS Monterey v12.2.1
@graphql-codegen/typescript-mongodb: 4.4.1- NodeJS: 17.2.0
Checklist:
- [X] I have followed the CONTRIBUTING doc and the style guidelines of this project
- [X] I have performed a self-review of my own code
- [X] I have commented my code, particularly in hard-to-understand areas
- [X] I have made corresponding changes to the documentation
- [X] My changes generate no new warnings
- [X] I have added tests that prove my fix is effective or that my feature works
- [X] New and existing unit tests pass locally with my changes
- [X] Any dependent changes have been merged and published in downstream modules
Further comments
This change works by checking whether the type referenced in a field with the @link directive is an object, union, or interface in the user's GraphQL schema. If it's an object or union we can use the config's dbTypeSuffix and if it's an interface we can use the config's dbInterfaceSuffix to generate the correct output. However, despite the fact that this feature was never documented, the @link directive can take an overrideType argument. While this isn't an issue if the user references an overrideType that is explicitly defined in the schema, it is an issue if it's not defined. In fact, typescript-mongo.spec.ts even references an instance when it's not (look for @link(overrideType: "MachineDocument", MachineDocument is never defined in the schema). One way to account for this was to just check whether the type is an interface, and if so use the dbInterfaceSuffix and in all other instances use the dbTypeSuffix. This would not break backwards-compatibility as previously dbTypeSuffix was (incorrectly) always used. However, this felt like a duct-tapey hacky fix, and it made much more sense to me to add a new value to the config of typescript-mongodb which I called defaultLinkOverrideTypeSuffix, which has a default value of DbObject. Then, the code can explicitly check if a specified type is an object, interface, or union in the schema, and in all other instances default to the new config value. By default, it is of value DbObject, which maintains backwards-compatibility and passes all current checks.
Next steps to proceed from here might be to include a new overrideTypeMap value (or something like that) in the config to explicitly map out what overriden types should look like, as the defaultLinkOverrideTypeSuffix approach is farily inflexible and just a stopgap that maintains backwards-compatibility. This will require a broader conversation, though, and I would definitely appreciate input.
⚠️ No Changeset found
Latest commit: 01eddb44173e99c2bc65df51f06855de52e8099c
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.
🔍 Inspect: https://vercel.com/theguild/graphql-code-generator/H8CF3wDkLHogjF4jiF9zaGmDmmW7
✅ Preview: https://graphql-code-generator-git-fork-benhchoi-mongod-035390-theguild.vercel.app
Hi!
All community plugins (see list below) have been moved to a new dotansimha/graphql-code-generator-community.
Community plugins:
@graphql-codegen/typescript-react-apollo@graphql-codegen/typescript-graphql-request@graphql-codegen/typescript-apollo-angular@graphql-codegen/typescript-apollo-client-helpers@graphql-codegen/typescript-react-query@graphql-codegen/typescript-urql@graphql-codegen/named-operations-object@graphql-codegen/urql-introspection@graphql-codegen/flow-resolvers@graphql-codegen/typescript-vue-apollo@graphql-codegen/typescript-rtk-query@graphql-codegen/flow-operations@graphql-codegen/typescript-msw@graphql-codegen/typescript-mongodb@graphql-codegen/typescript-type-graphql@graphql-codegen/jsdoc@graphql-codegen/typescript-vue-urql@graphql-codegen/kotlin@graphql-codegen/typescript-vue-apollo-smart-ops@graphql-codegen/java@graphql-codegen/c-sharp-operations@graphql-codegen/hasura-allow-list@graphql-codegen/typescript-stencil-apollo@graphql-codegen/relay-operation-optimizer@graphql-codegen/typescript-oclif@graphql-codegen/java-resolvers@graphql-codegen/java-apollo-android@graphql-codegen/flutter-freezed
Please move your PR by following these steps:
From your current PR branch:
- Fork the https://github.com/dotansimha/graphql-code-generator-community repository
- git remote add codegen-community <GIT_URL_OF_YOUR_COMMUNITY_REPO_FORK>
- git fetch codegen-community
- git push codegen-community
Open the PR on dotansimha/graphql-code-generator-community and close this PR.
Thank you!
Hey @benhchoi 👋 I'm closing this PR, but if you want to continue your work, please follow the steps provided here: https://github.com/dotansimha/graphql-code-generator/pull/7641#issuecomment-1305396562