teamwalnut / graphql-ppx

GraphQL language primitives for ReScript/ReasonML written in ReasonML
https://graphql-ppx.com
MIT License
258 stars 53 forks source link

Use import instead of require for template string tag when module mode is es6 #242

Closed gaku-sei closed 3 years ago

gaku-sei commented 3 years ago

Currently when setting the template* options as follow:

    "templateTagReturnType": "ApolloClient.GraphQL_PPX.templateTagReturnType",
    "templateTagImport": "gql",
    "templateTagLocation": "@apollo/client"

The PPX will generate something like:

var query = (require("@apollo/client").gql`
...`);

The problem with the above code is that, when the package-specs/module option is set to es6 the compiled file will contain a mix of commonjs and es module imports, which some bundlers like Rollup don't really support. Typically, it makes the code raise an exception with Vite.

An ideal solution could be to read the bsconfig.json file to get the module mode, but it could be complex since it's possible to have several entries in the package-specs setting 😕

A temporary option could be to add a "templateTagModule": "es6" | "commonjs" option.

(The code https://github.com/reasonml-community/graphql-ppx/blob/5d92a6bd8802049141511c809b75087ecd121823/src/bucklescript/output_bucklescript_module.re#L219)

jfrolich commented 3 years ago

https://github.com/reasonml-community/rescript-apollo-client/issues/70

Use templateTagIsFunction for now. To use import we need a functionality in the ReScript compiler to be able to inject the imported gql (imports cannot be inline).