Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from comigor/feat/schema-sdl
Browse files Browse the repository at this point in the history
sdl schema support
  • Loading branch information
comigor authored Feb 21, 2020
2 parents 4e32f63 + 9f5e995 commit 6090680
Show file tree
Hide file tree
Showing 32 changed files with 1,917 additions and 2,546 deletions.
25 changes: 22 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## 5.0.0
**MAJOR BREAKING CHANGE**
In this version we moved from `json` to `graphql` (SDL) schema parsing.
This allowed us to get rid off ±1200 lines of code which makes the
project support much easier. The test files with schema definitions
became more clear and human readable.

If you already have your schema in SDL format, just point to it in `build.yaml`.
If not, use this [snippet][introspection-to-sdl-snippet]
(from [this Apollo article][apollo-3-ways-schema]) or online helpers like
[this one][introspection-to-sdl-online] to convert from one to another.

## 4.0.2
- Only add unknownEnumValue on non-list enums
- Consider all classes to include reference to meta package
Expand Down Expand Up @@ -61,7 +73,7 @@ avoiding breaking/crashing the client.
- Allow to dispose `ArtemisClient` underlining http client when possible

## 3.0.0
- BREAKING: Marks non nullable input field as `@required` [#68](https://github.com/comigor/artemis/pull/68)
- BREAKING: Marks non nullable input field as `@required` [#68][pr-68]

## 2.2.2
- Make lists as input objects work again
Expand All @@ -73,7 +85,7 @@ avoiding breaking/crashing the client.
- Add "Articles and videos" category on README

## 2.2.0
- Share fragments between queries and schemas (see `fragments_glob`) [#65](https://github.com/comigor/artemis/pull/65)
- Share fragments between queries and schemas (see `fragments_glob`) [#65][pr-65]

## 2.1.4
- Add missing prefix to generated enums
Expand Down Expand Up @@ -189,7 +201,7 @@ Set HTTP headers only when using default HTTP client.
## 0.2.0 BREAKING
Completely overhaul how this works.

Artemis won't generate a full schema typing anymore. Instead, it will use the schema to generate typings from a specific query or mutation. It will also create helper functions to execute those queries. See [README](./README.md) for more info.
Artemis won't generate a full schema typing anymore. Instead, it will use the schema to generate typings from a specific query or mutation. It will also create helper functions to execute those queries. See [README][readme] for more info.

This is totally a breaking change but as this library is still on alpha, I should keep it under 1.0.

Expand Down Expand Up @@ -218,3 +230,10 @@ This is totally a breaking change but as this library is still on alpha, I shoul
- Consider custom scalars
- Not even compile from scratch
- Lot of bugs

[readme]: ./README.md
[pr-65]: https://github.com/comigor/artemis/pull/65
[pr-68]: https://github.com/comigor/artemis/pull/68
[apollo-3-ways-schema]: https://blog.apollographql.com/three-ways-to-represent-your-graphql-schema-a41f4175100d
[introspection-to-sdl-snippet]: https://gist.github.com/stubailo/041999ba5b8b15cede60b93ff9a38f53
[introspection-to-sdl-online]: https://codesandbox.io/s/pnmoxolx4
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
[![Pub Package](https://img.shields.io/pub/v/artemis.svg)](https://pub.dev/packages/artemis)
[![GitHub Actions](https://github.com/comigor/artemis/workflows/test/badge.svg)](https://github.com/comigor/artemis/actions)

Artemis is a code generator that looks for `schema.json` (GraphQL Introspection Query response data) and `*.graphql` files and builds `.dart` files typing that query, based on the schema. That's similar to what [Apollo](https://github.com/apollographql/apollo-client) does (Artemis is his sister anyway).
Artemis is a code generator that looks for `schema.graphql` (GraphQL SDL - Schema Definition Language) and `*.graphql` files and builds `.dart` files typing that query, based on the schema. That's similar to what [Apollo](https://github.com/apollographql/apollo-client) does (Artemis is his sister anyway).

---

## **Installation**
Add the following to your `pubspec.yaml` file to be able to do code generation:
```yaml
dev_dependencies:
artemis: '>=2.0.0 <3.0.0'
artemis: '>=5.0.0 <6.0.0'
build_runner: ^1.5.0
json_serializable: ^3.0.0
```
The generated code uses the following packages in run-time:
```yaml
dependencies:
artemis: '>=2.0.0 <3.0.0' # only if you're using ArtemisClient!
artemis: '>=5.0.0 <6.0.0' # only if you're using ArtemisClient!
json_serializable: ^3.0.0
equatable: ^0.6.1
meta: '>=1.0.0 <2.0.0' # only if you have non nullable fields
Expand Down Expand Up @@ -75,7 +75,7 @@ targets:
- lib/**
- graphql/**
- data/**
- schema.json
- schema.graphql
```
### **Schema mapping**
Expand All @@ -89,7 +89,7 @@ targets:
options:
schema_mapping:
- output: lib/graphql_api.dart
schema: lib/my_graphql_schema.json
schema: lib/my_graphql_schema.graphql
queries_glob: lib/**.graphql
```

Expand Down
37 changes: 21 additions & 16 deletions lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import 'package:gql/language.dart';

import './generator.dart';
import './generator/data.dart';
import './generator/graphql_helpers.dart';
import './generator/print_helpers.dart';
import './schema/graphql.dart';
import './schema/options.dart';

/// [GraphQLQueryBuilder] instance, to be used by `build_runner`.
Expand Down Expand Up @@ -55,17 +53,6 @@ class GraphQLQueryBuilder implements Builder {
r'$lib$': expectedOutputs,
};

Future<GraphQLSchema> _readSchemaFromPath(
BuildStep buildStep, SchemaMap schemaMap) async {
final assetStream = buildStep.findAssets(Glob(schemaMap.schema));
final schemaFile = await assetStream.single.catchError((e) {
throw Exception('''Schema `${schemaMap.schema}` was not found!
Make sure the file exists and you've typed it conrrectly on build.yaml.
''');
});
return schemaFromJsonString(await buildStep.readAsString(schemaFile));
}

@override
Future<void> build(BuildStep buildStep) async {
if (options.fragmentsGlob != null) {
Expand All @@ -85,7 +72,6 @@ Make sure the file exists and you've typed it conrrectly on build.yaml.
for (final schemaMap in options.schemaMapping) {
final buffer = StringBuffer();
final outputFileId = AssetId(buildStep.inputId.package, schemaMap.output);
final schema = await _readSchemaFromPath(buildStep, schemaMap);

// Loop through all files in glob
if (schemaMap.queriesGlob == null) {
Expand All @@ -103,8 +89,27 @@ Make sure that `queries_glob` your build.yaml file include GraphQL queries files
)
.toList();

final libDefinition = generateLibrary(schema, schemaMap.output, gqlDocs,
options, schemaMap, fragmentsCommon);
final schemaAssetStream = buildStep.findAssets(Glob(schemaMap.schema));

DocumentNode gqlSchema;

try {
gqlSchema = await schemaAssetStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.first;
} catch (e) {
throw Exception('''Schema `${schemaMap.schema}` was not found!
Make sure the file exists and you've typed it correctly on build.yaml.
''');
}

final libDefinition = generateLibrary(schemaMap.output, gqlDocs, options,
schemaMap, fragmentsCommon, gqlSchema);
if (onBuild != null) {
onBuild(libDefinition);
}
Expand Down
Loading

0 comments on commit 6090680

Please sign in to comment.