graphql_parser 1.1.3

graphql_parser #

Pub build status

Parses GraphQL queries and schemas.

This library is merely a parser/visitor. Any sort of actual GraphQL API functionality must be implemented by you, or by a third-party package.

Angel framework users should consider package:angel_graphql as a dead-simple way to add GraphQL functionality to their servers.

Installation #

Add graphql_parser as a dependency in your pubspec.yaml file:

dependencies:
  graphql_parser: ^1.0.0

Usage #

The AST featured in this library is directly based off this ANTLR4 grammar created by Joseph T. McBride: https://github.com/antlr/grammars-v4/blob/master/graphql/GraphQL.g4

It has since been updated to reflect upon the grammar in the official GraphQL specification.

import 'package:graphql_parser/graphql_parser.dart';

doSomething(String text) {
  var tokens = scan(text);
  var parser = new Parser(tokens);
  
  if (parser.errors.isNotEmpty) {
    // Handle errors...
  }
  
  // Parse the GraphQL document using recursive descent
  var doc = parser.parseDocument();
  
  // Do something with the parsed GraphQL document...
}

1.1.3 #

  • Add Parser.nextName, and remove all formerly-reserved words from the lexer. Resolves #19.

1.1.2 #

  • Parse the subscription keyword.

1.1.1 #

  • Pubspec updates for Dart 2.

1.1.0 #

  • Removed GraphQLVisitor.
  • Enable parsing operations without an explicit name.
  • Parse null.
  • Completely ignore commas.
  • Ignore Unicode BOM, as per the spec.
  • Parse object values.
  • Parse enum values.

example/example.dart

import 'package:graphql_parser/graphql_parser.dart';

final String text = '''
{
  project(name: "GraphQL") {
    tagline
  }
}
  '''
    .trim();

main() {
  var tokens = scan(text);
  var parser = new Parser(tokens);
  var doc = parser.parseDocument();

  var operation = doc.definitions.first as OperationDefinitionContext;

  var projectField = operation.selectionSet.selections.first.field;
  print(projectField.fieldName.name); // project
  print(projectField.arguments.first.name); // name
  print(projectField.arguments.first.valueOrVariable.value.value); // GraphQL

  var taglineField = projectField.selectionSet.selections.first.field;
  print(taglineField.fieldName.name); // tagline
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  graphql_parser: ^1.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:graphql_parser/graphql_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
63
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:graphql_parser/graphql_parser.dart.

Health issues and suggestions

Document public APIs. (-0.79 points)

277 out of 279 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/language/parser.dart. (-17.34 points)

Analysis of lib/src/language/parser.dart reported 38 hints, including:

line 72 col 7: DO use curly braces for all flow control structures.

line 83 col 11: DO use curly braces for all flow control structures.

line 93 col 9: DO use curly braces for all flow control structures.

line 109 col 15: DO use curly braces for all flow control structures.

line 137 col 7: DO use curly braces for all flow control structures.

Fix lib/src/language/ast/misc_value.dart. (-2.96 points)

Analysis of lib/src/language/ast/misc_value.dart reported 6 hints, including:

line 10 col 25: Avoid types as parameter names.

line 22 col 25: Avoid types as parameter names.

line 36 col 27: Avoid types as parameter names.

line 36 col 53: Avoid types as parameter names.

line 67 col 27: Avoid types as parameter names.

Fix lib/src/language/ast/directive.dart. (-2.48 points)

Analysis of lib/src/language/ast/directive.dart reported 5 hints:

line 12 col 25: Avoid types as parameter names.

line 12 col 38: Avoid types as parameter names.

line 12 col 49: Avoid types as parameter names.

line 12 col 61: Avoid types as parameter names.

line 12 col 74: Avoid types as parameter names.

Fix additional 21 files with analysis or formatting issues. (-20.46 points)

Additional issues in the following files:

  • lib/src/language/ast/field.dart (4 hints)
  • lib/src/language/ast/alias.dart (3 hints)
  • lib/src/language/ast/fragment_definition.dart (3 hints)
  • lib/src/language/ast/string_value.dart (3 hints)
  • lib/src/language/ast/argument.dart (2 hints)
  • lib/src/language/ast/array_value.dart (2 hints)
  • lib/src/language/ast/fragment_spread.dart (2 hints)
  • lib/src/language/ast/inline_fragment.dart (2 hints)
  • lib/src/language/ast/list_type.dart (2 hints)
  • lib/src/language/ast/number_value.dart (2 hints)
  • lib/src/language/ast/operation_definition.dart (2 hints)
  • lib/src/language/ast/selection_set.dart (2 hints)
  • lib/src/language/ast/variable.dart (2 hints)
  • lib/src/language/ast/variable_definitions.dart (2 hints)
  • lib/src/language/token.dart (2 hints)
  • lib/src/language/ast/boolean_value.dart (1 hint)
  • lib/src/language/ast/default_value.dart (1 hint)
  • lib/src/language/ast/field_name.dart (1 hint)
  • lib/src/language/ast/type.dart (1 hint)
  • lib/src/language/ast/type_name.dart (1 hint)
  • lib/src/language/ast/variable_definition.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
charcode ^1.0.0 1.1.2
source_span ^1.0.0 1.5.5
string_scanner ^1.0.0 1.0.4
Transitive dependencies
meta 1.1.7
path 1.6.2
term_glyph 1.1.0
Dev dependencies
matcher any
test >=0.12.0 <2.0.0

Admin