I'm writing a Dart types generator and I had to use a custom document-finder to find queries inside .dart files.
To do it I forked this lib but would there be a different way to do this?
Check this diff to see the changes I had to make to make it work. -- (it's an ugly hack)
That's interesting. Currently we are thinking about creating a different way to load the documents, and use require instead of fs.readFile, it will also be a solution for this issue: https://github.com/dotansimha/graphql-code-generator/issues/528
I'm not familiar with Dart, @degroote22 , can you please explain how Dart files are different from JS or TS files? And how we should load and search them? (as text of require)
Currently the only implementation that uses GraphQL on Dart is one for Flutter (a framework for building mobile apps).
There isn't a function used to parse all queries as there is in JS, but we could tell people to use an identity function (Dart is compiled, it won't add any cost) to anotate which strings are GraphQL operations, like I did here.
With that being said, Dart strings are between " (double quotes), ' (single quote), or """ (three double quotes for multiline string).
And there is a caveat. String interpolation in Dart uses the same sintax as GraphQL variables.
This in Typescript
const a = `${variable}`
is the same as
var a = "$variable"
in Dart.
So we need to escape the $ char in the Dart string and strip it away from the string on the document-finder.
@degroote22 what about writing .gql files and reading them as strings from dart as a stopgap?
Aside from that, I think making operation extraction language-agnostic would best be done through a regex option to match by (like /gql`[^`*]`/ in js) and a string post processing option.
Am also interested in dart in particular, after having had considerable frustrations with react-native
I'd rather have separate front-ends, not a case where one fits all.
One interface where the user would specify a document loader just like we do templates could be a good abstraction.
You'd implement a function that get's the content of a file and can return documents.
If you're using Flutter you can use the .gql file as an asset though. https://flutter.io/assets-and-images/ , no problems.
So how could I generate Classes and other types for dart using remote graphql schema?
@sandangel remote schema usage is in the readme :
gql-gen --schema http://localhost:3010/graphql --header "Authorization: MY_KEY" ....
As for template options, idk what state @degroote22's is in, but you could try
graphql-to-dart - still pretty rough around the edges though
thanks, I found graphql-code-generator-dart package on npm and the github link is too this repo but there was nothing relating to dart in the readme. So I just want to know the command i need to type to generate dart code
@degroote22 in the new API we have a new feature called loader.
You can specify a custom loader that will load your schema/document files.
It's not well documented yet, but it's tested and you can see how to use it here:
https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/tests/codegen.spec.ts#L534-L547
Most helpful comment
@degroote22 in the new API we have a new feature called
loader.You can specify a custom loader that will load your schema/document files.
It's not well documented yet, but it's tested and you can see how to use it here:
https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/tests/codegen.spec.ts#L534-L547