GraphQL Schema 3
An implementation of GraphQL's type system in Dart, with no dependency beyond collection and
source_span. Supports any platform where Dart runs. The decisions made in the design of this library were done to make the experience as similar to the JavaScript reference implementation as possible, and to also correctly implement the official specification.
Contains functionality to build all GraphQL types:
StringIntFloatBooleanGraphQLObjectTypeGraphQLUnionTypeGraphQLEnumTypeGraphQLInputObjectTypeDate- ISO-8601 Date string, deserializes to a DartDateTimeobject
Of course, for a full description of GraphQL's type system, see the official GraphQL Specification. Mostly analogous to graphql-js; many names are verbatim.
Where this comes from
This package is a fork of the GraphQL stack maintained as part of
Angel3, which itself descends from the
graphql_* packages Tobe O wrote for Angel. The fork is taken from the 2
line; the original BSD-3-Clause licence and its copyright notice are kept
verbatim in LICENSE, and the bulk of the type system, the parser and
the execution algorithm are still that work.
Why fork at all. Two reasons, and only the second one still holds:
- Upstream had stopped moving while the projects depending on it had not. Development there has since resumed, but by then the two lines had diverged far enough that merging back would cost more than it returns.
- The stack was pinned to
angel3_*, andangel3_*decided whichanalyzerand which Dart SDK everything downstream could use. That is what held the generator sevenanalyzermajors back for months. Cutting the tie was the point of the3line.
So: the 3 line does not track upstream and does not merge from it. It is
maintained on its own, with three rules - as few dependencies as possible, no
dependency that dictates the SDK, and no behaviour without a test covering it.
The 3 is a lineage marker, not a version and not a succession
graphql_schema2 is not this package's predecessor. It is its sibling, and it is
alive: 7.0.0 as of August 2026, published by dukefirehawk.com, on its own
numbering that long ago stopped matching the 2 in its name. The 3 here says
only which line this fork was taken from.
If you want the upstream package, take
graphql_schema2. Take this one for
the smaller dependency tree and the fixes listed below. They have not been
offered upstream. The two lines were compared at upstream 7.0.0: every release
it has cut since the fork point raises the Dart SDK floor or the linter, and its
dependency set is unchanged.
What version 3 changed
Two dependencies, collection and source_span.
Six bugs, all of them reachable from a client request:
- Validating an unknown enum literal raised
Bad state: No elementinstead of returning a failedValidationResult. For a string-valued enum, built withenumTypeFromStrings, every literal took that path, so any client sending an unrecognised value crashed the resolver. - A field whose input had the wrong type raised a cast error while validating an object rather than reporting the mismatch.
Floatrejected an integer literal. The specification coerces an integer toFloat, and only in that direction.GraphQLFieldInput.operator ==comparedother.defaultValuewith itself, so two inputs differing only by their default compared as equal.- Four types hashed the identity of their field list while comparing its
contents, so two equal instances could carry different hash codes. That
breaks the
Objectcontract and, with it, anySetorMapkeyed on a type. GraphQLObjectType.hashCodeandGraphQLInputObjectType.hashCodefoldednamein twice and neverdescription.
Also: quiver removed, its hash2 / hash3 / hash4 replaced by
Object.hash; and a test suite of 41 tests, where there were none.
The full list is in CHANGELOG.md.
Installation
dart pub add graphql_schema3
Usage
It's easy to define a schema with the helper functions:
final GraphQLSchema todoSchema = GraphQLSchema(
query: objectType('Todo', [
field('text', graphQLString.nonNullable()),
field('created_at', graphQLDate)
]));
All GraphQL types are generic, in order to leverage Dart's strong typing support.
Serialization
GraphQL types can serialize and deserialize input data. The exact implementation of this depends on the type.
var iso8601String = graphQLDate.serialize(DateTime.now());
var date = graphQLDate.deserialize(iso8601String);
print(date.millisecondsSinceEpoch);
Validation
GraphQL types can validate input data.
var validation = myType.validate('@root', {...});
if (validation.successful) {
doSomething(validation.value);
} else {
print(validation.errors);
}
Helpers
graphQLSchema- Create aGraphQLSchemaobjectType- Create aGraphQLObjectTypewith fieldsfield- Create aGraphQLFieldwith a type/argument/resolverlistOf- Create aGraphQLListTypewith the providedinnerTypeinputObjectType- Creates aGraphQLInputObjectTypeinputField- Creates a field for aGraphQLInputObjectType
Types
All of the GraphQL scalar types are built in, as well as a Date type:
graphQLStringgraphQLIdgraphQLBooleangraphQLIntgraphQLFloatgraphQLDate
Non-Nullable Types
You can easily make a type non-nullable by calling its nonNullable method.
List Types
Support for list types is also included. Use the listType helper for convenience.
/// A non-nullable list of non-nullable integers
listOf(graphQLInt.nonNullable()).nonNullable();
Input values and parameters
Take the following GraphQL query:
{
anime {
characters(title: "Hunter x Hunter") {
name
age
}
}
}
And subsequently, its schema:
type AnimeQuery {
characters($title: String!): [Character!]
}
type Character {
name: String
age: Int
}
The field characters accepts a parameter, title. To reproduce this in package:graphql_schema3, use GraphQLFieldInput:
final GraphQLObjectType queryType = objectType('AnimeQuery', fields: [
field('characters',
listOf(characterType.nonNullable()),
inputs: [
new GraphQLFieldInput('title', graphQLString.nonNullable())
]
),
]);
final GraphQLObjectType characterType = objectType('Character', fields: [
field('name', graphQLString),
field('age', graphQLInt),
]);
In the majority of cases where you use GraphQL, you will be delegate the actual fetching of data to a database object, or some asynchronous resolver function.
package:graphql_schema3 includes this functionality in the resolve property, which is passed a context object and a Map<String, dynamic> of arguments.
A hypothetical example of the above might be:
var field = field(
'characters',
graphQLString,
resolve: (_, args) async {
return await myDatabase.findCharacters(args['title']);
},
);