Last updated:
0 purchases
graphql fragment builder
GraphQL Fragment Builder #
A Flutter package that enables you to build flexible and type-safe GraphQL fragments and queries with a single function call.
How to use it? #
1. Add the package to pubspec.yaml dependency:
dependencies:
graphql_fragment_builder: ^1.0.0
copied to clipboard
2. Import package:
import 'package:graphql_fragment_builder/graphql_fragment_builder.dart';
copied to clipboard
3. Create your fragments:
class BookDetailsFragment extends QueryFragment with SimpleQueryFragment {
@override
String get objectName => 'bookDetails';
@override
List<String> get fields => [
'title',
'author',
'publicationYear',
'genre',
];
}
copied to clipboard
4. Build your query:
final query = GraphQLQueryBuilder(
name: 'getBooksByAuthor',
parameters: [
QueryParameter('authorName', 'Jane Austen'),
QueryParameter('limit', 5),
],
fragments: [BookDetailsFragment()],
);
debugPrint(query.buildQuery());
debugPrint(query.variables);
copied to clipboard
Output:
getBooksByAuthor(authorName: $authorName, limit: $limit) {
bookDetails {
title
author
publicationYear
genre
}
}
{authorName: Jane Austen, limit: 5}
copied to clipboard
Advanced Usage #
You can combine multiple fragments in a single query for more complex operations:
class AuthorDetailsFragment extends QueryFragment with SimpleQueryFragment {
@override
String get objectName => 'authorDetails';
@override
List<String> get fields => [
'name',
'birthYear',
'nationality',
];
}
final complexQuery = GraphQLQueryBuilder(
name: 'getAuthorWithBooks',
parameters: [
QueryParameter('authorId', '123'),
],
fragments: [AuthorDetailsFragment(), BookDetailsFragment()],
);
copied to clipboard
Output:
getAuthorWithBooks(authorId: $authorId) {
authorDetails {
name
birthYear
nationality
}
bookDetails {
title
author
publicationYear
genre
}
}
{authorId: 123}
copied to clipboard
This flexibility allows you to build complex queries while maintaining readability and type safety.
Features #
Pattern-matching approach: Build GraphQL queries using a flexible, type-safe method.
Improved readability: Structured query building enhances code clarity.
Easy maintenance: Modular design allows for simple updates and extensions.
Fragment support: Create reusable query fragments for efficient query composition.
Parameter handling: Easily include and manage query parameters.
Developer #
By Hossein Yousefpour
© All rights reserved.
Donate #
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.