artemis 6.12.3-beta.1

Dart native js
Flutter Android iOS web

Build dart types from GraphQL schemas and queries (using Introspection Query).


Build dart types from GraphQL schemas and queries

View at Test PRs Welcome Star on GitHub Fork on GitHub Discord

Check the beta branch for the bleeding edge (and breaking) stuff.

Artemis is a code generator that looks for schema.graphql (GraphQL SDL - Schema Definition Language) and *.graphql files and builds .graphql.dart files typing that query, based on the schema. That's similar to what Apollo does (Artemis is his sister anyway).

Installation #

Add the following to your pubspec.yaml file to be able to do code generation:

  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:

  artemis: '>=5.0.0 <6.0.0' # only if you're using ArtemisClient!
  json_annotation: ^3.0.0
  equatable: ^0.6.1
  meta: '>=1.0.0 <2.0.0' # only if you have non nullable fields
  gql: '>=0.7.3 <1.0.0'

Then run:

pub packages get


flutter packages get

Now Artemis will generate the API files for you by running:

pub run build_runner build


flutter pub run build_runner build

Configuration #

Artemis offers some configuration options to generate code. All options should be included on build.yaml file on the root of the project:

          # custom configuration options!
OptionDefault valueDescription
generate_helperstrueIf Artemis should generate query/mutation helper GraphQLQuery subclasses.
scalar_mapping[]Mapping of GraphQL and Dart types. See Custom scalars.
schema_mapping[]Mapping of queries and which schemas they will use for code generation. See Schema mapping.
fragments_globnullImport path to the file implementing fragments for all queries mapped in schema_mapping. If it's assigned, fragments defined in schema_mapping will be ignored.
ignore_for_file[]The linter rules to ignore for artemis generated files.

It's important to remember that, by default, build will follow Dart's package layout conventions, meaning that only some folders will be considered to parse the input files. So, if you want to reference files from a folder other than lib/, make sure you've included it on sources:

      - lib/**
      - graphql/**
      - data/**
      - schema.graphql

Schema mapping #

By default, Artemis won't generate anything. That's because your queries/mutations should be linked to GraphQL schemas. To configure it, you need to point a schema_mapping to the path of those queries and schemas:

            - output: lib/graphql_api.dart
              schema: lib/my_graphql_schema.graphql
              queries_glob: lib/**.graphql

Each SchemaMap is configured this way:

OptionDefault valueDescription
outputRelative path to output the generated code. It should end with .graphql.dart or else the generator will need to generate one more file.
schemaRelative path to the GraphQL schema.
queries_globGlob that selects all query files to be used with this schema.
naming_schemepathedWithTypesThe naming scheme to be used on generated classes names. pathedWithTypes is the default for retrocompatibility, where the names of previous types are used as prefix of the next class. This can generate duplication on certain schemas. With pathedWithFields, the names of previous fields are used as prefix of the next class and with simple, only the actual GraphQL class nameis considered.
type_name_field__typenameThe name of the field used to differentiatiate interfaces and union types (commonly __typename or __resolveType). Note that __typename field are not added automatically to the query. If you want interface/union type resolution, you need to manually add it there.

See examples for more information and configuration options.

Custom scalars #

If your schema uses custom scalars, they must be defined on build.yaml. If it needs a custom parser (to decode from/to json), the custom_parser_import path must be set and the file must implement both fromGraphQL___ToDart___ and fromDart___toGraphQL___ constant functions.

            - custom_parser_import: 'package:graphbrainz_example/coercers.dart'
              graphql_type: Date
              dart_type: DateTime

If your custom scalar needs to import Dart libraries, you can provide it in the config as well:

            - custom_parser_import: 'package:graphbrainz_example/coercers.dart'
              graphql_type: BigDecimal
                name: Decimal
                  - 'package:decimal/decimal.dart'

Each ScalarMap is configured this way:

OptionDefault valueDescription
graphql_typeThe GraphQL custom scalar name on schema.
dart_typeThe Dart type this custom scalar should be converted from/to.
custom_parser_importnullImport path to the file implementing coercer functions for custom scalars. See Custom scalars.

See examples for more information and configuration options.

Articles and videos #

  1. Ultimate toolchain to work with GraphQL in Flutter
  2. Awesome GraphQL

ArtemisClient #

If you have generate_helpers, Artemis will create a subclass of GraphQLQuery for you, this class can be used in conjunction with ArtemisClient.

final client = ArtemisClient('/graphql');
final gitHubReposQuery = MyGitHubReposQuery();
final response = await client.execute(gitHubReposQuery);

ArtemisClient adds type-awareness around Link from package:gql/link. You can create ArtemisClient from any Link using ArtemisClient.fromLink.

Check the examples to see how to use it in details.