Comments (7)
@vanelizarov is the underlying issue that some scalars may not be strings (over JSON)?
from gql.
@klavs yeah, that's the case. For example Uint
which is actually int
in Dart. It comes as number from server, but I cannot simply print()
such property because of typecasting exception, of course other operations with these properties are unavailable for the same reason
Also it would be cool to map other scalars like Email
(which is actually a plain string and comes from server as string) to Dart's String
just for optimization sake 🙂
from gql.
Also it would be cool to map other scalars like Email (which is actually a plain string and comes from server as string) to Dart's String just for optimization sake
It could be added as an option, but I do not think it's good to optimize for performance here. All the custom scalars are wrapped to avoid unintentional assignments. If email: Email
is treated as a String
, developer is able to assign any String
to email
without any static warning. Wrapped types force developer to explicitly cast primitive types to scalars. I'm actually considering adding a wrapper to ID
type, because ids are supposed to be opaque.
For example Uint which is actually int in Dart. It comes as number from server, but I cannot simply print() such property because of typecasting exception, of course other operations with these properties are unavailable for the same reason
Yes, such functionality should probably be added. For the reasons explained above, I think it would be first implemented as a map of custom scalar to JSON-compatible dart type (int, double, String, bool).
from gql.
It could be added as an option, but I do not think it's good to optimize for performance here. All the custom scalars are wrapped to avoid unintentional assignments. If
email: Email
is treated as aString
, developer is able to assign anyString
toID
type, because ids are supposed to be opaque.
Oh, now I understand, thanks for the explanation!
Yes, such functionality should probably be added. For the reasons explained above, I think it would be first implemented as a map of custom scalar to JSON-compatible dart type (int, double, String, bool).
So, can you please point me where to start working on such option? I'm interested in implementing it and making PR in case of success 🙂
All I've found for now is defaultTypeMap
constant here and that it gets passed through typeRef()
to code builder operations, but I cannot understand, how to pass down custom map from build.yaml
and how to do it for all builders at once without copy-pasting this map for each builder
from gql.
It's going to be affecting many files. gql_build
would be responsible of reading in the custom map and passing it to gq_code_builder
. Every builder interested in schema types would receive the custom map and pass it down every function call till the accessor is reached.
Unfortunately, it would most likely not be implementable once without copy-pasting.
As you noticed, defaultTypeMap
and typeRef
were initial attempt to solve this problem, but I lowered it's priority and did not finish it.
from gql.
@klavs ok, I understand what you mean, thanks!
Finally I ended up with temp solution by copying gql_build
and gql_code_builder
to my project and changing some sources:
- Defined
customScalarMap
constant ingql_code_builder/lib/src/common.dart
const defaultTypeMap = <String, Reference>{
// ...
};
const customTypeMap = <String, Reference>{
"Uint": Reference("int"),
"Time": Reference("String"),
"Upload": Reference("File", "dart:io"),
"Email": Reference("String"),
// ...
};
- Added condition to check if current
typeName
exists incustomTypeMap
togql_code_builder/lib/src/operation/data.dart
for further checks to not add.value
in getters/setters:
Method _buildGetter(
DocumentNode schema,
String schemaUrl,
SelectionNode node,
String prefix,
String type,
) {
if (node is FieldNode) {
// ...
final typeName = unwrappedTypeNode.name.value;
TypeDefinitionNode fieldTypeDef;
if (!customTypeMap.containsKey(typeName)) {
fieldTypeDef = _getTypeDefinitionNode(
schema,
typeName,
);
}
final typeMap = {
...defaultTypeMap,
...customTypeMap, // also added this one
if (node.selectionSet != null)
typeName: refer("$prefix\$$name")
else if (fieldTypeDef != null)
typeName: refer(typeName, schemaUrl)
};
// ...
}
Similarly did it for gql_code_builder/lib/src/operation/var.dart
Method _buildSetter(
VariableDefinitionNode node,
DocumentNode schema,
String schemaUrl,
) {
final unwrappedTypeNode = _unwrapTypeNode(node.type);
final typeName = unwrappedTypeNode.name.value;
TypeDefinitionNode argTypeDef;
if (!customTypeMap.containsKey(typeName)) {
argTypeDef = _getTypeDefinitionNode(
schema,
typeName,
);
}
final typeMap = {
...defaultTypeMap,
...customTypeMap, // also added this one
if (argTypeDef != null) typeName: refer(typeName, schemaUrl),
};
// ...
}
And for gql_code_builder/lib/src/schema/input.dart
:
Method _buildSetter(
InputValueDefinitionNode node,
DocumentNode doc,
) {
final unwrappedTypeNode = _unwrapTypeNode(node.type);
final typeName = unwrappedTypeNode.name.value;
TypeDefinitionNode argTypeDef;
if (!customTypeMap.containsKey(typeName)) {
argTypeDef = _getTypeDefinitionNode(
doc,
typeName,
);
}
final typeMap = {
...defaultTypeMap,
...customTypeMap, // also added this one
if (argTypeDef != null) typeName: refer(typeName),
};
// ...
}
- Changed
pubspec.yaml
imports in locally copiedgql_build
:
dependencies:
# ...
gql_code_builder:
path: ../gql_code_builder
And in my project:
dev_dependencies:
# ...
gql_build:
path: ../gql_build
I know that this solution doesn't quite correlate with what you wrote, but it works for me. Hope sometimes you will release "native" version of it 🙂
from gql.
@vanelizarov initial custom scalar support has been added, although docs still need to be updated.
You will need to provide a "type_overrides" yaml map in your build.yaml. Please note that you need to duplicate the type_overrides map for each builder that depends on the schema in your build.yaml.
gql_build|data_builder:
enabled: true
options:
schema: pokemon_explorer|lib/schema.graphql
type_overrides:
MyScalar:
name: MyScalar
import: "path/to/package"
from gql.
Related Issues (20)
- [gql_websocket_link] TransportWebSocketLink does not reconnect when graphql server disappears HOT 6
- [graphql_transport_ws] The new subprotocol doesn't auto-reconnect HOT 2
- OperationException(linkException: ResponseFormatException(originalException: type 'Null' is not a subtype of type 'String' in type cast HOT 1
- [Begging for support] Can the url be transformed? HOT 2
- [Support request] How can the client complete a subscription HOT 1
- [gql_websocket_link] Crash events on host lookup HOT 3
- LinkException as a sealed class HOT 2
- [WebSocketLink] inactivityTimeout causes a reconnect HOT 5
- More maintainers wanted
- gql_code_builder: allow adding custom builder factories for nested collections
- [gql_websocket_link] uuid package version conflict HOT 1
- [Question] Dio interceptor with retry not triggered when timeout() is invoked HOT 5
- dependencies issue HOT 1
- FR: Dynamic header HOT 1
- Issue with - distinguishing between null and absent values HOT 3
- How to make reconnect TransportWebSocketLink(TransportWsClientOptions())
- [BUG] Printer fails with string blocks HOT 5
- Type exception in gql_websocket_link with decoding incoming messages. HOT 1
- extract serializers from gql_code_builder to another package
- how to set headers in first http1.1 request before connection upgrade for TransportWebSocketLink?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gql.