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

feat: Add custom scalar parsing #349

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scalar DateTime

type Query {
time: DateTime
}

query FetchScalars {
time
}
289 changes: 289 additions & 0 deletions packages/graphql_codegen/test/assets/issue_348/document.graphql.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
import 'package:gql/ast.dart';
import 'scalar_import.dart';

enum Enum$__TypeKind {
SCALAR,
OBJECT,
INTERFACE,
UNION,
ENUM,
INPUT_OBJECT,
LIST,
NON_NULL,
$unknown;

factory Enum$__TypeKind.fromJson(String value) =>
fromJson$Enum$__TypeKind(value);

String toJson() => toJson$Enum$__TypeKind(this);
}

String toJson$Enum$__TypeKind(Enum$__TypeKind e) {
switch (e) {
case Enum$__TypeKind.SCALAR:
return r'SCALAR';
case Enum$__TypeKind.OBJECT:
return r'OBJECT';
case Enum$__TypeKind.INTERFACE:
return r'INTERFACE';
case Enum$__TypeKind.UNION:
return r'UNION';
case Enum$__TypeKind.ENUM:
return r'ENUM';
case Enum$__TypeKind.INPUT_OBJECT:
return r'INPUT_OBJECT';
case Enum$__TypeKind.LIST:
return r'LIST';
case Enum$__TypeKind.NON_NULL:
return r'NON_NULL';
case Enum$__TypeKind.$unknown:
return r'$unknown';
}
}

Enum$__TypeKind fromJson$Enum$__TypeKind(String value) {
switch (value) {
case r'SCALAR':
return Enum$__TypeKind.SCALAR;
case r'OBJECT':
return Enum$__TypeKind.OBJECT;
case r'INTERFACE':
return Enum$__TypeKind.INTERFACE;
case r'UNION':
return Enum$__TypeKind.UNION;
case r'ENUM':
return Enum$__TypeKind.ENUM;
case r'INPUT_OBJECT':
return Enum$__TypeKind.INPUT_OBJECT;
case r'LIST':
return Enum$__TypeKind.LIST;
case r'NON_NULL':
return Enum$__TypeKind.NON_NULL;
default:
return Enum$__TypeKind.$unknown;
}
}

enum Enum$__DirectiveLocation {
QUERY,
MUTATION,
SUBSCRIPTION,
FIELD,
FRAGMENT_DEFINITION,
FRAGMENT_SPREAD,
INLINE_FRAGMENT,
VARIABLE_DEFINITION,
SCHEMA,
SCALAR,
OBJECT,
FIELD_DEFINITION,
ARGUMENT_DEFINITION,
INTERFACE,
UNION,
ENUM,
ENUM_VALUE,
INPUT_OBJECT,
INPUT_FIELD_DEFINITION,
$unknown;

factory Enum$__DirectiveLocation.fromJson(String value) =>
fromJson$Enum$__DirectiveLocation(value);

String toJson() => toJson$Enum$__DirectiveLocation(this);
}

String toJson$Enum$__DirectiveLocation(Enum$__DirectiveLocation e) {
switch (e) {
case Enum$__DirectiveLocation.QUERY:
return r'QUERY';
case Enum$__DirectiveLocation.MUTATION:
return r'MUTATION';
case Enum$__DirectiveLocation.SUBSCRIPTION:
return r'SUBSCRIPTION';
case Enum$__DirectiveLocation.FIELD:
return r'FIELD';
case Enum$__DirectiveLocation.FRAGMENT_DEFINITION:
return r'FRAGMENT_DEFINITION';
case Enum$__DirectiveLocation.FRAGMENT_SPREAD:
return r'FRAGMENT_SPREAD';
case Enum$__DirectiveLocation.INLINE_FRAGMENT:
return r'INLINE_FRAGMENT';
case Enum$__DirectiveLocation.VARIABLE_DEFINITION:
return r'VARIABLE_DEFINITION';
case Enum$__DirectiveLocation.SCHEMA:
return r'SCHEMA';
case Enum$__DirectiveLocation.SCALAR:
return r'SCALAR';
case Enum$__DirectiveLocation.OBJECT:
return r'OBJECT';
case Enum$__DirectiveLocation.FIELD_DEFINITION:
return r'FIELD_DEFINITION';
case Enum$__DirectiveLocation.ARGUMENT_DEFINITION:
return r'ARGUMENT_DEFINITION';
case Enum$__DirectiveLocation.INTERFACE:
return r'INTERFACE';
case Enum$__DirectiveLocation.UNION:
return r'UNION';
case Enum$__DirectiveLocation.ENUM:
return r'ENUM';
case Enum$__DirectiveLocation.ENUM_VALUE:
return r'ENUM_VALUE';
case Enum$__DirectiveLocation.INPUT_OBJECT:
return r'INPUT_OBJECT';
case Enum$__DirectiveLocation.INPUT_FIELD_DEFINITION:
return r'INPUT_FIELD_DEFINITION';
case Enum$__DirectiveLocation.$unknown:
return r'$unknown';
}
}

Enum$__DirectiveLocation fromJson$Enum$__DirectiveLocation(String value) {
switch (value) {
case r'QUERY':
return Enum$__DirectiveLocation.QUERY;
case r'MUTATION':
return Enum$__DirectiveLocation.MUTATION;
case r'SUBSCRIPTION':
return Enum$__DirectiveLocation.SUBSCRIPTION;
case r'FIELD':
return Enum$__DirectiveLocation.FIELD;
case r'FRAGMENT_DEFINITION':
return Enum$__DirectiveLocation.FRAGMENT_DEFINITION;
case r'FRAGMENT_SPREAD':
return Enum$__DirectiveLocation.FRAGMENT_SPREAD;
case r'INLINE_FRAGMENT':
return Enum$__DirectiveLocation.INLINE_FRAGMENT;
case r'VARIABLE_DEFINITION':
return Enum$__DirectiveLocation.VARIABLE_DEFINITION;
case r'SCHEMA':
return Enum$__DirectiveLocation.SCHEMA;
case r'SCALAR':
return Enum$__DirectiveLocation.SCALAR;
case r'OBJECT':
return Enum$__DirectiveLocation.OBJECT;
case r'FIELD_DEFINITION':
return Enum$__DirectiveLocation.FIELD_DEFINITION;
case r'ARGUMENT_DEFINITION':
return Enum$__DirectiveLocation.ARGUMENT_DEFINITION;
case r'INTERFACE':
return Enum$__DirectiveLocation.INTERFACE;
case r'UNION':
return Enum$__DirectiveLocation.UNION;
case r'ENUM':
return Enum$__DirectiveLocation.ENUM;
case r'ENUM_VALUE':
return Enum$__DirectiveLocation.ENUM_VALUE;
case r'INPUT_OBJECT':
return Enum$__DirectiveLocation.INPUT_OBJECT;
case r'INPUT_FIELD_DEFINITION':
return Enum$__DirectiveLocation.INPUT_FIELD_DEFINITION;
default:
return Enum$__DirectiveLocation.$unknown;
}
}

class Query$FetchScalars {
Query$FetchScalars({this.time});

factory Query$FetchScalars.fromJson(Map<String, dynamic> json) {
final l$time = json['time'];
return Query$FetchScalars(
time: l$time == null ? null : customDateTimeFromJson(l$time));
}

final DateTime? time;

Map<String, dynamic> toJson() {
final _resultData = <String, dynamic>{};
final l$time = time;
_resultData['time'] = l$time == null ? null : customDateTimeToJson(l$time);
return _resultData;
}

@override
int get hashCode {
final l$time = time;
return Object.hashAll([l$time]);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (!(other is Query$FetchScalars) || runtimeType != other.runtimeType) {
return false;
}
final l$time = time;
final lOther$time = other.time;
if (l$time != lOther$time) {
return false;
}
return true;
}
}

extension UtilityExtension$Query$FetchScalars on Query$FetchScalars {
CopyWith$Query$FetchScalars<Query$FetchScalars> get copyWith =>
CopyWith$Query$FetchScalars(
this,
(i) => i,
);
}

abstract class CopyWith$Query$FetchScalars<TRes> {
factory CopyWith$Query$FetchScalars(
Query$FetchScalars instance,
TRes Function(Query$FetchScalars) then,
) = _CopyWithImpl$Query$FetchScalars;

factory CopyWith$Query$FetchScalars.stub(TRes res) =
_CopyWithStubImpl$Query$FetchScalars;

TRes call({DateTime? time});
}

class _CopyWithImpl$Query$FetchScalars<TRes>
implements CopyWith$Query$FetchScalars<TRes> {
_CopyWithImpl$Query$FetchScalars(
this._instance,
this._then,
);

final Query$FetchScalars _instance;

final TRes Function(Query$FetchScalars) _then;

static const _undefined = <dynamic, dynamic>{};

TRes call({Object? time = _undefined}) => _then(Query$FetchScalars(
time: time == _undefined ? _instance.time : (time as DateTime?)));
}

class _CopyWithStubImpl$Query$FetchScalars<TRes>
implements CopyWith$Query$FetchScalars<TRes> {
_CopyWithStubImpl$Query$FetchScalars(this._res);

TRes _res;

call({DateTime? time}) => _res;
}

const documentNodeQueryFetchScalars = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
name: NameNode(value: 'FetchScalars'),
variableDefinitions: [],
directives: [],
selectionSet: SelectionSetNode(selections: [
FieldNode(
name: NameNode(value: 'time'),
alias: null,
arguments: [],
directives: [],
selectionSet: null,
)
]),
),
]);
const possibleTypesMap = <String, Set<String>>{};
11 changes: 11 additions & 0 deletions packages/graphql_codegen/test/assets/issue_348/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"scalars": {
"DateTime": {
"type": "DateTime",
"fromJsonFunctionName": "customDateTimeFromJson",
"toJsonFunctionName": "customDateTimeToJson",
"import": "scalar_import.dart"
}
},
"addTypename": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DateTime? customDateTimeFromJson(dynamic dynamic) =>
dynamic is String ? DateTime.parse(dynamic) : null;

dynamic customDateTimeToJson(DateTime date) => date.toIso8601String();
36 changes: 18 additions & 18 deletions packages/graphql_codegen/test/assets/scalars/options.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"scalars": {
"ID": {
"type": "int"
},
"Custom1": {
"type": "String"
},
"Custom2": {
"type": "DateTime"
},
"Custom3": {
"type": "Fobbob",
"fromJsonFunctionName": "fobbobFromJson",
"toJsonFunctionName": "fobbobToJson",
"import": "scalar_import.dart"
}
"scalars": {
"ID": {
"type": "int"
},
"addTypename": false
}
"Custom1": {
"type": "String"
},
"Custom2": {
"type": "DateTime"
},
"Custom3": {
"type": "Fobbob",
"fromJsonFunctionName": "fobbobFromJson",
"toJsonFunctionName": "fobbobToJson",
"import": "scalar_import.dart"
}
},
"addTypename": false
}
Loading