/gql_gapher

Tired of writing your GraphQL queries & mutation as raw strings `r""" """` in Dart and Flutter? 👋

Primary LanguageDartMIT LicenseMIT

gql_gapher

Dart CI

Tired of writing your queries & mutation as raw strings r""" """ in Dart and Flutter? 👋

Validate and generate typed request classes for all your queries and mutations defined in GraphQL (.graphql|.gql) files. It also supports Fragments.

Usage

Add gql_gapher to your dev dependencies

dev_dependencies:
  gql_gapher: any   // required dart >=2.12.0
  build_runner:

Write your queries & mutations

mutation AuthenticateUser(
  $first: String!
  $second: Int!
  $third: Boolean
  $fourth: Float
) {
  authenticate(
    input: {
      firebase: { token: $first }
      data: { attempt: $second }
      amount: $third
    }
    rememberMe: $fourth
  ) {
    user {
      id
      email
      phone
    }
  }
}

then run the generator

# dart
dart pub run build_runner build

# flutter
flutter pub run build_runner build

Use it

import 'authenticate.g.dart';

void main() {

  final authPreq = AuthenticateUserRequest('First', 2, third: true, fourth: 2.3);

  print(authPreq.query); // query

  print(authPreq.variables); // variables

  print(authPreq.operation); // AuthenticateUser
}

More

Fragments Support

Fragment Imports are supported as seen in the sample below.

#import "../fragments/user_fragment.graphql"
#import "../fragments/location_fragment.graphql"

query GetUser($token: String!) {
  authenticate(input: { firebase: { token: $token } }, rememberMe: true) {
    location {
      name
      latitude
      longitude
      ...LocationFragment
    }
    ...UserFragment
  }
}

Contributors ✨

Contributions of any kind welcome!