Build Dart Types from GraphQL Schemas and Queries

Jul 31, 2021 | Programming

If you’re stepping into the enchanting realm of Dart and GraphQL, the **Artemis** library can provide a seamless path to construct data types directly from GraphQL schemas and queries. However, keep in mind that Artemis has been discontinued. But fear not, as the community has provided alternatives like **Ferry** and **graphql**, which also offer interesting features for development.

Installation

To install Artemis (or its alternatives), add the following dependencies to your pubspec.yaml file:

dev_dependencies:
  artemis: ^8.0.0 
  build_runner: ^2.1.4 
  json_serializable: ^6.0.1
dependencies:
  artemis: ^8.0.0 # only if using ArtemisClient!
  json_annotation: ^4.3.0 
  equatable: ^2.0.3
  gql: ^0.13.1-alpha

After saving this, run one of the following commands in the terminal:

dart pub get
flutter pub get

Generating the API Files

Once installed, you can generate the API files by executing:

dart run build_runner build
flutter pub run build_runner build

Configuration

Artemis allows you to adjust several configuration options in a build.yaml file located in the root of your project. Here’s an illustration:

targets:
  $default:
    builders:
      artemis:
        options:
          # custom configuration options!
          generate_helpers: true
          scalar_mapping: []
          schema_mapping: []
          fragments_glob: null
          ignore_for_file: []
          generate_queries: true

Understanding Schema Mapping

To link your queries and mutations to GraphQL schemas, configure a schema mapping in build.yaml. Here’s how:

targets:
  $default:
    builders:
      artemis:
        options:
          schema_mapping:
            - output: lib/graphql_api.graphql.dart
              schema: lib/my_graphql_schema.graphql
              queries_glob: lib/**/*.graphql

Think of schemas as the blueprint of your building (application), where each room (query) needs to follow the rules (schemas) to ensure everything fits perfectly!

Working with Custom Scalars

If your GraphQL schema involves custom scalars, they must also be defined in your build.yaml file. Here is how:

targets:
  $default:
    builders:
      artemis:
        options:
          scalar_mapping:
            - custom_parser_import: package:graphbrainz_example/coercers.dart
              graphql_type: Date
              dart_type: DateTime

This is akin to adding your own flavors to a recipe; the custom parser creates a bridge between the GraphQL custom scalar and Dart types!

Troubleshooting Tips

  • If you encounter errors while generating files, ensure your build.yaml file is correctly structured and validated.
  • Make sure the paths specified in your configuration file point to the right locations.
  • If the generated code doesn’t behave as expected, check for missing dependencies or incorrect mappings.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox