forked from gql-dart/gql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request gql-dart#86 from micimize/feat-gql-types
gql/schema.dart and gql/operation.dart
- Loading branch information
Showing
23 changed files
with
2,576 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import "package:gql/language.dart" as lang; | ||
import "package:gql/schema.dart" as gql_schema; | ||
import "package:gql/operation.dart" as gql_operation; | ||
|
||
final schemaDefinition = lang.parseString(r""" | ||
schema { | ||
query: StarWarsQuery | ||
} | ||
interface Character { | ||
id: String | ||
name: String | ||
} | ||
type Droid implements Character { | ||
id: String | ||
name: String | ||
primaryFunction: String | ||
} | ||
type StarWarsQuery { | ||
droids: [Droid!] | ||
} | ||
"""); | ||
|
||
void inspectSchema() { | ||
final schema = gql_schema.GraphQLSchema.fromNode(schemaDefinition); | ||
|
||
final character = | ||
schema.getType("Character") as gql_schema.InterfaceTypeDefinition; | ||
final droid = schema.getType("Droid") as gql_schema.ObjectTypeDefinition; | ||
|
||
print(character.isImplementedBy(droid)); | ||
// prints "true" | ||
|
||
print(schema.query.getField("droids").type.toString()); | ||
// prints "[Droid!]" | ||
} | ||
|
||
final fragmentDefinitions = [ | ||
lang.parseString(r""" | ||
fragment droidName on Droid { | ||
name | ||
} | ||
"""), | ||
]; | ||
|
||
final queryDefinition = lang.parseString(r""" | ||
query AllDroids { | ||
droids { | ||
...droidName | ||
primaryFunction | ||
} | ||
} | ||
"""); | ||
|
||
void inspectQuery() { | ||
final schema = gql_schema.GraphQLSchema.fromNode(schemaDefinition); | ||
|
||
final document = gql_operation.ExecutableDocument( | ||
queryDefinition, | ||
schema.getType, | ||
// necessary for dereferencing schema definitions | ||
fragmentDefinitions, | ||
); | ||
|
||
final importedFragment = document.getFragment("droidName"); | ||
print(importedFragment); | ||
// prints the fagment above | ||
|
||
final query = document.operations.first; | ||
final droids = query.selectionSet.fields.first; | ||
final spreadDroidName = droids.selectionSet.fragmentSpreads.first; | ||
|
||
print( | ||
// dereference fragment spread into fragment definition | ||
spreadDroidName.fragment == importedFragment, | ||
); | ||
} | ||
|
||
void main() { | ||
inspectSchema(); | ||
inspectQuery(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// AST-based GraphQL Executable Definitions (operations). | ||
/// | ||
/// Adds type resolution and useful helper methods for working with executable graphql documents, | ||
/// such as is useful for code generation. | ||
/// | ||
/// The primary entrypoint is usually [ExecutableDocument] | ||
/// which accepts an [gql.ast.DocumentNode], | ||
/// a tearoff of [gql.schema.GraphQLSchema.getType()], | ||
/// and an optional list of imported ASTs. | ||
/// | ||
/// **NOTE**: This library is currently only for working with executable _definitions_. | ||
library operation; | ||
|
||
export "package:gql/src/operation/definitions.dart"; | ||
export "package:gql/src/operation/executable.dart"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// AST-based GraphQL Schema Type Definitions. | ||
/// | ||
/// Adds type resolution and useful helper methods for working with graphql schema definitions, | ||
/// such as is useful for code generation. | ||
/// | ||
/// The primary entrypoint is usually [GraphQLSchema.fromNode()], which accepts an [gql.ast.DocumentNode] | ||
/// | ||
/// **NOTE**: This library is currently only for working with schema _definitions_, | ||
/// and is not a package for implementing servers like [`graphql_server`](https://pub.dev/packages/graphql_server). | ||
library schema; | ||
|
||
export "package:gql/src/schema/definitions.dart"; | ||
export "package:gql/src/schema/schema.dart"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export "package:gql/src/operation/definitions/type_resolver.dart"; | ||
export "package:gql/src/operation/definitions/base_types.dart"; | ||
export "package:gql/src/operation/definitions/selections.dart"; | ||
export "package:gql/src/operation/definitions/definitions.dart"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import "package:meta/meta.dart"; | ||
import "package:collection/collection.dart"; | ||
import "package:gql/src/schema/definitions.dart"; | ||
|
||
import "package:gql/src/operation/definitions/type_resolver.dart"; | ||
|
||
@immutable | ||
abstract class ExecutableGraphQLEntity extends GraphQLEntity { | ||
const ExecutableGraphQLEntity(); | ||
} | ||
|
||
@immutable | ||
abstract class ExecutableWithResolver extends ExecutableGraphQLEntity | ||
implements ExecutableTypeResolver { | ||
const ExecutableWithResolver([GetExecutableType getType]) | ||
: getType = getType ?? GetExecutableType.withoutContext, | ||
super(); | ||
|
||
@override | ||
final GetExecutableType getType; | ||
|
||
@override | ||
bool operator ==(Object o) { | ||
if (identical(this, o)) return true; | ||
|
||
if (o.runtimeType == runtimeType) { | ||
final _o = o as ExecutableWithResolver; | ||
return astNode == _o.astNode && getType == _o.getType; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@override | ||
int get hashCode => const ListEquality<Object>( | ||
DeepCollectionEquality(), | ||
).hash([astNode, getType]); | ||
} |
Oops, something went wrong.