graphql_parser 1.1.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

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 was originally 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 ( June 2018).

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.4 #

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.4

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]
70
Health:
Code health derived from static analysis. [more]
74
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 Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Document public APIs. (-0.79 points)

282 out of 284 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. (-25 points)

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

line 63 col 12: Unnecessary new keyword.

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

line 72 col 14: Unnecessary new keyword.

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

line 83 col 18: Unnecessary new keyword.

Fix lib/src/language/lexer.dart. (-4.89 points)

Analysis of lib/src/language/lexer.dart reported 10 hints, including:

line 7 col 25: Unnecessary new keyword.

line 8 col 28: Unnecessary new keyword.

line 10 col 24: Unnecessary new keyword.

line 11 col 24: Unnecessary new keyword.

line 13 col 29: Unnecessary new keyword.

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

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

line 12 col 54: Use = to separate a named parameter from its default value.

line 27 col 15: Unnecessary new keyword.

line 38 col 27: Unnecessary new keyword.

line 40 col 21: Unnecessary new keyword.

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

Fix additional 3 files with analysis or formatting issues. (-3.49 points)

Additional issues in the following files:

  • lib/src/language/ast/field.dart (4 hints)
  • lib/src/language/token.dart (2 hints)
  • lib/src/language/ast/number_value.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
charcode ^1.0.0 1.1.3
source_span ^1.0.0 1.7.0
string_scanner ^1.0.0 1.0.5
Transitive dependencies
collection 1.14.12
meta 1.1.8
path 1.6.4
term_glyph 1.1.0
Dev dependencies
matcher any
test >=0.12.0 <2.0.0