Skip to content
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

Add option to enable new flow inexact object syntax #1800

Open
bdrobinson opened this issue Feb 12, 2020 · 1 comment
Open

Add option to enable new flow inexact object syntax #1800

bdrobinson opened this issue Feb 12, 2020 · 1 comment
Labels
🤖 component - codegen related to the codegen core packages

Comments

@bdrobinson
Copy link

Currently when generating graphql types for flow, if we apply the --useFlowExactObjects flag we get:

type User = {|
  __typename: "User",
  name: string,
|}

and if we don't specify the flag (ie, we want inexact objects), we get

type User = {
  __typename: "User",
  name: string,
}

The problem is with the second one – flow is moving towards exact objects being the default, and have added an optional lint warning that triggers when an inexact object is declared implicitly (ie, without the new ... syntax.

So I'm suggesting that the default behaviour should be changed such that if this library creates an inexact object, it will do it with the required .... Ie:

type User = {
  __typename: "User",
  name: string,
  ...
}

This would be a breaking change for some users, as flow<0.84 is unable to parse the ... syntax. However, 0.84 is quite an old version at this point and we could perhaps add a --useOldFlowInexactSyntax flag to keep it working for them.

If the breaking change doesn't appeal, we could do the opposite – the default behaviour is unchanged, and you can enable the new behaviour with a --useNewFlowInexactSyntax.

Hope this makes sense. Thanks!

@JakeDawkins JakeDawkins added the 🤖 component - codegen related to the codegen core packages label Feb 12, 2020
@bdrobinson
Copy link
Author

If anyone's interested, I managed to work around this using jscodeshift. Every time I generate the graphql types, I run jscodeshift -t codeshift_makeInexactExplicit.js on each of the files generated by apollo (I had to do a bit of jiggery pokery with find to make sure I only ran it on the apollo codegen output rather than my own product code).

Source of codeshift_makeInexactExplicit.js:

module.exports = function transformer(file, api) {
    const j = api.jscodeshift
    const root = j(file.source)

    root.find(j.ObjectTypeAnnotation, { inexact: false, exact: false }).forEach(
        path => {
            path.node.inexact = true
        },
    )
    return root.toSource()
}

module.exports.parser = "flow"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 component - codegen related to the codegen core packages
Projects
None yet
Development

No branches or pull requests

2 participants