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 GraphQL switch #135

Merged
merged 1 commit into from
Aug 23, 2022
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
18 changes: 9 additions & 9 deletions .github/workflows/check_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
channel:
- beta
- stable
- dev
# - dev
steps:
- uses: actions/checkout@v2
- uses: subosito/flutter-action@v2
Expand All @@ -62,7 +62,7 @@ jobs:
- name: Bootstrap melos
run: melos bs
- name: Analyze package
run: melos run analyze
run: melos run analyze --no-select

build:
runs-on: ubuntu-latest
Expand All @@ -71,7 +71,7 @@ jobs:
channel:
- beta
- stable
- dev
# - dev

timeout-minutes: 5
steps:
Expand All @@ -84,13 +84,13 @@ jobs:
- name: Bootstrap melos
run: melos bs
- name: Build dart
run: melos run build_dart
run: melos run build_dart --no-select
- name: Build fluter
run: melos run build_flutter
run: melos run build_flutter --no-select
- name: Bootstrap melos
run: melos bs
- name: Check if changed
run: melos run check
run: git diff --exit-code ':!**/pubspec.lock'

test:
runs-on: ubuntu-latest
Expand All @@ -99,7 +99,7 @@ jobs:
channel:
- beta
- stable
- dev
# - dev

steps:
- uses: actions/checkout@v2
Expand All @@ -111,6 +111,6 @@ jobs:
- name: Bootstrap melos
run: melos bs
- name: Run tests dart
run: melos run test_dart
run: melos run test_dart --no-select
- name: Run tests flutter
run: melos run test_flutter
run: melos run test_flutter --no-select
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ doc/api/
/.dart_tool
.idea

pubspec_overrides.yaml
pubspec_overrides.yaml.DS_Store
.DS_Store
29 changes: 22 additions & 7 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ packages:
- packages/**

scripts:
analyze: melos exec -- "dart analyze --fatal-infos ."
test_dart: melos exec --ignore=*example* --dir-exists=test -- "dart test"
test_flutter: melos exec --scope=*example* --dir-exists=test -- "flutter test"
build_dart: melos exec --ignore=*example* -- "dart run build_runner build --delete-conflicting-outputs"
build_flutter: melos exec --scope=*example* -- "flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs"
format: melos exec -- "dart format"
check: git diff --exit-code
analyze:
run: melos exec -- "dart analyze --fatal-infos ."
test_dart:
exec: dart test
select-package:
flutter: false
dir-exists: "test"
test_flutter:
exec: flutter test
select-package:
flutter: true
dir-exists: "test"
build_dart:
exec: dart run build_runner build --delete-conflicting-outputs
select-package:
flutter: false
build_flutter:
exec: flutter pub run build_runner build --delete-conflicting-outputs
select-package:
flutter: true
format:
exec: dart format

command:
bootstrap:
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql_codegen/example/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# melos_managed_dependency_overrides: graphql_codegen
dependency_overrides:
graphql_codegen:
path: ..
2 changes: 2 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class GraphQLCodegenConfig {
final String namingSeparator;
final List<String> extraKeywords;
final String outputDirectory;
final bool disableContextReplacement;

GraphQLCodegenConfig({
this.clients = const {},
this.disableContextReplacement = false,
this.scalars = const {},
this.addTypename = true,
this.assetsPath = "lib/**.graphql",
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 30 additions & 6 deletions packages/graphql_codegen/lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ContextFragment<TKey extends Object>
type: type,
name: argument.name,
path: path,
directives: [],
),
);
}
Expand Down Expand Up @@ -293,6 +294,17 @@ class Schema<TKey extends Object> {
return lookupType(typeName);
}

TypeDefinitionNode? lookupTypeDefinitionNodeFromField(
TypeDefinitionNode onType,
NameNode node,
) {
final type = lookupTypeNodeFromField(onType, node);
if (type == null) {
return null;
}
return lookupTypeDefinitionFromTypeNode(type);
}

TypeNode? lookupTypeNodeFromField(
TypeDefinitionNode onType,
NameNode node,
Expand Down Expand Up @@ -356,6 +368,7 @@ class ContextProperty {
final NameNode _name;
final NameNode? alias;
final Name? path;
final List<DirectiveNode> directives;

NameNode get name => alias ?? _name;

Expand All @@ -364,33 +377,38 @@ class ContextProperty {
this.alias,
required this.type,
required NameNode name,
required this.directives,
}) : _name = name;

ContextProperty.fromFieldNode(
FieldNode node, {
this.path,
required this.type,
}) : _name = node.name,
alias = node.alias;
alias = node.alias,
directives = node.directives;

ContextProperty.fromInputValueDefinitionNode(
InputValueDefinitionNode node, {
this.path,
}) : _name = node.name,
type = node.type,
alias = null;
alias = null,
directives = node.directives;

ContextProperty.fromVariableDefinitionNode(
VariableDefinitionNode node, {
this.path,
}) : _name = node.variable.name,
type = node.type,
alias = null;
alias = null,
directives = node.directives;

ContextProperty merge(ContextProperty other) => ContextProperty(
type: _mergeTypes(type, other.type),
name: name,
path: path,
directives: directives,
);

String get _key => name.value;
Expand Down Expand Up @@ -458,6 +476,7 @@ abstract class Context<TKey extends Object, TType extends TypeDefinitionNode> {
final Set<Name> _fragments = {};
final Set<Context<TKey, TypeDefinitionNode>> _possibleTypes = {};
final Map<String, ContextProperty> _properties = {};
final Map<String, ContextProperty> _ownProperties = {};
final Map<String, ContextProperty> _variables;
final Set<FragmentDefinitionNode> _fragmentDependencies;
final Set<SelectionNode> _selections = {};
Expand Down Expand Up @@ -614,10 +633,12 @@ abstract class Context<TKey extends Object, TType extends TypeDefinitionNode> {
}

void addProperty(ContextProperty property) {
if (_properties[property._key] != null) {
return;
if (_properties[property._key] == null) {
_properties[property._key] = property;
}
if (_ownProperties[property._key] == null && _inFragment.isEmpty) {
_ownProperties[property._key] = property;
}
_properties[property._key] = property;
}

void addVariable(ContextProperty property) {
Expand Down Expand Up @@ -648,6 +669,8 @@ abstract class Context<TKey extends Object, TType extends TypeDefinitionNode> {

Iterable<ContextProperty> get properties => _properties.values;

Iterable<ContextProperty> get ownProperties => _ownProperties.values;

Iterable<ContextProperty> get variables => _variables.values;

bool get hasVariables => variables.isNotEmpty;
Expand Down Expand Up @@ -689,6 +712,7 @@ abstract class Context<TKey extends Object, TType extends TypeDefinitionNode> {
replacementContext ?? this;

Context<TKey, TypeDefinitionNode>? get replacementContext {
if (config.disableContextReplacement) return null;
if (isDefinitionContext) return null;
final parentReplace = parent?.replacementContext;
if (parentReplace != null) {
Expand Down
22 changes: 15 additions & 7 deletions packages/graphql_codegen/lib/src/printer/base/equality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'package:built_collection/built_collection.dart';
import 'package:code_builder/code_builder.dart';
import 'package:gql/ast.dart';
import 'package:graphql_codegen/src/context.dart';
import 'package:graphql_codegen/src/printer/base/constants.dart';
import 'package:graphql_codegen/src/printer/context.dart';

typedef DataObjResolver = Expression Function();

Method printEqualityOperator(
PrintContext c,
String name,
Iterable<ContextProperty> properties, {
bool dataObjectCheck = false,
DataObjResolver? dataObjectCheckResolver,
}) =>
Method((b) => b
..name = "operator=="
Expand Down Expand Up @@ -42,9 +43,15 @@ Method printEqualityOperator(
.property(c.namePrinter.printPropertyName(e.name))
.assignFinal(localOtherName)
.statement,
if (dataObjectCheck && !e.isRequired)
if (dataObjectCheckResolver != null && !e.isRequired) ...[
Code('if ('),
dataObjectCheckResolver().code,
Code('.containsKey(\'${e.name.value}\') != other.'),
dataObjectCheckResolver().code,
Code(
'if (${kDataVariableName}.containsKey(\'${e.name.value}\') != other.${kDataVariableName}.containsKey(\'${e.name.value}\')) {return false;}'),
'.containsKey(\'${e.name.value}\')) {return false;}',
),
],
_printPropertyEqualityCheck(
e.type,
localThisName,
Expand Down Expand Up @@ -95,7 +102,7 @@ Code _printPropertyEqualityCheck(
Method printHashCodeMethod(
PrintContext context,
Iterable<ContextProperty> properties, {
bool dataObjectCheck = false,
DataObjResolver? dataObjectCheckResolver,
}) =>
Method(
(b) => b
Expand All @@ -122,8 +129,9 @@ Method printHashCodeMethod(
property.type,
refer(localProp),
);
if (dataObjectCheck && !property.isRequired) {
return refer(kDataVariableName)
if (dataObjectCheckResolver != null &&
!property.isRequired) {
return dataObjectCheckResolver()
.property('containsKey')
.call([
literalString(property.name.value)
Expand Down
12 changes: 7 additions & 5 deletions packages/graphql_codegen/lib/src/printer/base/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ List<Spec> _printInputClasses(
),
])
..fields = ListBuilder([
Field((b) => b
..name = kDataVariableName
..type = dynamicMap)
Field(
(b) => b
..name = kDataVariableName
..type = dynamicMap,
)
])
..methods = ListBuilder([
...properties.map((e) => Method(
Expand Down Expand Up @@ -139,12 +141,12 @@ List<Spec> _printInputClasses(
context,
name(context.path),
properties,
dataObjectCheck: true,
dataObjectCheckResolver: () => refer(kDataVariableName),
),
printHashCodeMethod(
context,
properties,
dataObjectCheck: true,
dataObjectCheckResolver: () => refer(kDataVariableName),
),
]),
),
Expand Down
Loading