-
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
transform documents hooks #8723
Conversation
…xecuting plugins.
🦋 Changeset detectedLatest commit: 2a8e14f The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@saihaj @n1ru4l Could you please review this? |
@n1ru4l I have added a page to the documentation. I'm concerned about the quality of my texts as I don't usually use English. |
613dd93
to
f16fc4c
Compare
const { visit, print } = require('graphql') | ||
|
||
module.exports = { | ||
plugin(schema, documents, config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not really sure if we need this hook/function. What benefit is it trying to solve?
could this file not simply be the following?
module.exports = function transformDocuments (...args) => {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In terms of the current implementation, I'm using Plugin mechanism to executetransformDocuments
. Currently, we get the transformDocuments
function from Plugin.
Regarding your question, are you suggesting we create a plugin without the plugin
function?
Currently, the plugin
function is not optional, but I can change it to be optional. However, I have concerns that making the plugin
function optional may confuse users and may not be a desirable change to the API. 🤔
Another option would be not to use the Plugin mechanism at all. Instead, we could introduce a new concept called something like DocumentTransform that works independently of Plugin. Just as Preset is a separate concept from Plugin, DocumentTransform is separate from Plugin.
This would be a slightly over-engineered solution, and most implementations would probably be copies of Plugin, but it would make API cleaner. ✨
If this design is acceptable, I will implement it. 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the new DocumentTransform
concept is definitely what we should do here! Can you please adjust this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed #8723 (comment)
Hey @kazekyo, sorry for letting this PR untouched for so long. I just rebased your PR and I have some questions for you: With the
Our goal would be that all new projects using GraphQL Code Generator would pick the This feature would kind of allow implementation fragment arguments and operation document optimization as you already pointed out. This is great as it would work nicely with this change 😍. See this Would you be interested in helping out by having fragment arguments and document optimization built into the client preset? Of course not part of this pull request! Here is some more feedback that I noticed while playing around with it. Right now it is impossible only possible to reference // eslint-disable-next-line import/no-extraneous-dependencies
import { type CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './src/yoga.ts',
documents: ['src/**/*.ts'],
generates: {
'./src/gql/': {
plugins: [],
preset: 'client-preset',
documentTransforms: ['./foo.js'],
},
},
};
export default config; Should we also allow inline functions? // eslint-disable-next-line import/no-extraneous-dependencies
import { type CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './src/yoga.ts',
documents: ['src/**/*.ts'],
generates: {
'./src/gql/': {
plugins: [],
preset: 'client-preset',
documentTransforms: [{
tarnsformDocuments() {}
}],
},
},
};
export default config; Let me know what you think! |
…ql-code-generator into document_transform
@n1ru4l Thanks for reviewing this pull request!
Of course, I'd be happy to help! It seems that some of our goals overlap. 😃 As for this PR, I am unsure whether it can be used as part of your project or should be re-implemented in the
It's great to allow inline functions. It would make it easier for users to experiment. Just to confirm, Plugin cannot be written as inline functions, correct? import { type CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './src/yoga.ts',
documents: ['src/**/*.ts'],
generates: {
'./src/gql/': {
plugins: [{
plugin() {}
}],
preset: 'client-preset',
},
},
};
export default config; Currently, I use Plugin to execute |
As discussed here: #8723 (comment), let's allow passing a function. That would then allow you to do something like the following: import { type CodegenConfig } from '@graphql-codegen/cli';
import { addTypenameFieldSelections, addIdFieldSelection, inlineFragments } from '@wherever'
const config: CodegenConfig = {
schema: './src/yoga.ts',
documents: ['src/**/*.ts'],
generates: {
'./src/gql/': {
preset: 'client',
transformDocuments: [addTypenameFieldSelections, addIdFieldSelection, inlineFragments]
},
},
};
export default config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, for the delayed answer. I added some comments, I am excited to get this close to getting merged!
@n1ru4l Regarding the designs, please let me know if you have any feedback on the following points:
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'https://localhost:4000/graphql',
documents: ['src/**/*.tsx'],
generates: {
'./src/gql/': {
preset: 'client',
documentTransforms: [
({ documents }) => { // Specify only the function
return documents;
},
],
},
},
};
export default config;
If you notice anything else, please let me know. I'm confident the designs have improved through our discussions, and I welcome further feedback! 😃 |
@kazekyo Can you give more context for the use case of What is the use-case of coupling the schema transform with a specific document transform? I think this is redundant with the following: import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: ['https://localhost:4000/graphql', `extend type Query { foo: String }`], // see here
documents: ['src/**/*.tsx'],
generates: {
'./src/gql/': {
preset: 'client',
},
},
};
export default config; Would be happy, if we could immediately merge the already discussed stuff without the |
My use case is to implement Relay's
If after reading the information I just provided, you still have additional discussion or questions regarding |
@kazekyo Let's keep |
@n1ru4l I have removed I don't think the failing test |
Thank you @kazekyo 🎉 |
Description
I have a preset to transform user-defined documents.
I want to use the new
client
preset, so I want to be able to transform documents with a plugin.Plugin will have the following functions:
Use it as follows:
One of the main uses of this feature is to add fields.
For example, assuming that there is a
@useIcon
directive that adds fields related to an icon:If the plugin transforms the document, we can get the following document:
Related:
Type of change
Screenshots/Sandbox (if appropriate/relevant):
Please see unit tests for usage.
How Has This Been Tested?
I added tests to the spec file.
Checklist:
Further comments
About validation
No validation of documents is executed before transforming.
Because I understand that validation before transforming often leads to errors when this feature is needed.
This decision will allow future use of plugins such as relay-operation-optimizer without the
skipDocumentsValidation
flag.About relay-operation-optimizer
We can probably remove the special implementation for
relay-operation-optimizer
and replace it with the document transform plugin.But I did not include that changes in this pull request. If there is no problem, I will do that next.