Crate apollo_encoder

Source
Expand description

apollo-encoder

A library to generate GraphQL Code, SDL.

Crates.io Download docs.rs docs

§Getting started

Add the dependency to start using apollo-encoder:

cargo add apollo-encoder

Or add this to your Cargo.toml for a manual installation:

# Just an example, change to the necessary package version.
[dependencies]
apollo-encoder = "0.8.0"

§Rust versions

apollo-encoder is tested on the latest stable version of Rust. Older version may or may not be compatible.

§Examples

§Create executable definitions

  use apollo_encoder::{
      Document, Field, FragmentSpread, InlineFragment, OperationDefinition, OperationType,
      Selection, SelectionSet, TypeCondition,
  };
  use indoc::indoc;

  let mut document = Document::new();

  let mut droid_selection_set = SelectionSet::new();
  let primary_function_field = Selection::Field(Field::new(String::from("primaryFunction")));
  droid_selection_set.selection(primary_function_field);

  let mut droid_inline_fragment = InlineFragment::new(droid_selection_set);
  droid_inline_fragment.type_condition(Some(TypeCondition::new(String::from("Droid"))));

  let comparison_fields_fragment_spread =
      FragmentSpread::new(String::from("comparisonFields"));

  let name_field = Field::new(String::from("name"));

  let hero_selection_set = vec![
      Selection::Field(name_field),
      Selection::FragmentSpread(comparison_fields_fragment_spread),
      Selection::InlineFragment(droid_inline_fragment),
  ];

  let mut hero_field = Field::new(String::from("hero"));
  hero_field.selection_set(Some(SelectionSet::with_selections(hero_selection_set)));

  let hero_for_episode_selection_set = vec![Selection::Field(hero_field)];
  let mut hero_for_episode_operation = OperationDefinition::new(
      OperationType::Query,
      SelectionSet::with_selections(hero_for_episode_selection_set),
  );
  hero_for_episode_operation.name(Some(String::from("HeroForEpisode")));

  document.operation(hero_for_episode_operation);

  assert_eq!(
      document.to_string(),
      indoc! {r#"
          query HeroForEpisode {
            hero {
              name
              ...comparisonFields
              ... on Droid {
                primaryFunction
              }
            }
          }
      "#}
  );

§Create type system definitions

use apollo_encoder::{
    Argument, Directive, Document, DirectiveDefinition, EnumValue,
    EnumDefinition, UnionDefinition, Value
};
use indoc::indoc;

let mut document = Document::new();

// Create a Directive Definition.
let mut directive_def = DirectiveDefinition::new("provideTreat".to_string());
directive_def.description("Ensures cats get treats.".to_string());
directive_def.location("OBJECT".to_string());
directive_def.location("FIELD_DEFINITION".to_string());
directive_def.location("INPUT_FIELD_DEFINITION".to_string());
document.directive(directive_def);

// Create an Enum Definition
let mut enum_ty_1 = EnumValue::new("CatTree".to_string());
enum_ty_1.description("Top bunk of a cat tree.".to_string());
let enum_ty_2 = EnumValue::new("Bed".to_string());
let mut enum_ty_3 = EnumValue::new("CardboardBox".to_string());
let mut directive = Directive::new(String::from("deprecated"));
directive.arg(Argument::new(
    String::from("reason"),
    Value::String("Box was recycled.".to_string()),
));
enum_ty_3.directive(directive);

let mut enum_def = EnumDefinition::new("NapSpots".to_string());
enum_def.description("Favourite cat\nnap spots.".to_string());
enum_def.value(enum_ty_1);
enum_def.value(enum_ty_2);
enum_def.value(enum_ty_3);
document.enum_(enum_def);
// Union Definition
let mut union_def = UnionDefinition::new("Pet".to_string());
union_def.description(
    "A union of all pets represented within a household.".to_string(),
);
union_def.member("Cat".to_string());
union_def.member("Dog".to_string());
document.union(union_def);

assert_eq!(
    document.to_string(),
    indoc! {r#"
        "A union of all pets represented within a household."
        union Pet = Cat | Dog
        """
        Favourite cat
        nap spots.
        """
        enum NapSpots {
          "Top bunk of a cat tree."
          CatTree
          Bed
          CardboardBox @deprecated(reason: "Box was recycled.")
        }
        "Ensures cats get treats."
        directive @provideTreat on OBJECT | FIELD_DEFINITION | INPUT_FIELD_DEFINITION
    "#},
);

§License

Licensed under either of

at your option.

Structs§

Argument
The Argument type represents an argument
ArgumentsDefinition
The ArgumentsDefinition type represents an arguments definition
Directive
The Directive type represents a Directive, it provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
DirectiveDefinition
The DirectiveDefinition type represents a Directive definition.
Document
The Document type represents a GraphQL document. A GraphQL Document describes a complete file or request string operated on by a GraphQL service or client. A document contains multiple definitions, either executable or representative of a GraphQL type system.
EnumDefinition
Enums are special scalars that can only have a defined set of values.
EnumValue
The EnumValue type represents one of possible values of an enum.
Field
The __Field type represents each field in an Object or Interface type.
FieldDefinition
The FieldDefinition type represents each field definition in an Object definition or Interface type definition.
FragmentDefinition
The FragmentDefinition type represents a fragment definition
FragmentSpread
The FragmentSpread type represents a named fragment used in a selection set.
InlineFragment
The InlineFragment type represents an inline fragment in a selection set that could be used as a field
InputField
Input Field in a given Input Object. A GraphQL Input Object defines a set of input fields; the input fields are either scalars, enums, or other input objects. Input fields are similar to Fields, but can have a default value.
InputObjectDefinition
Input objects are composite types used as inputs into queries defined as a list of named input values..
InputValueDefinition
The InputValueDefinition type represents field and directive arguments.
InterfaceDefinition
InterfaceDefinition is an abstract type where there are common fields declared.
ObjectDefinition
ObjectDefinition types represent concrete instantiations of sets of fields.
OperationDefinition
The OperationDefinition type represents an operation definition
ScalarDefinition
Represents scalar types such as Int, String, and Boolean. Scalars cannot have fields.
SchemaDefinition
A GraphQL service’s collective type system capabilities are referred to as that service’s “schema”.
SelectionSet
The SelectionSet type represents a selection_set type in a fragment spread, an operation or a field
TypeCondition
The TypeCondition type represents where a fragment could be applied
UnionDefinition
UnionDefinitions are an abstract type where no common fields are declared.
VariableDefinition
The VariableDefinition type represents a variable definition

Enums§

FromError
Errors that can occur when converting an apollo-parser CST to an apollo-encoder one. These errors don’t give a lot of context at the moment. Before converting CSTs, you should make sure that your parse tree is complete and valid.
OperationType
The OperationType type represents the kind of operation
Selection
The Selection type represents a selection in a selection set Selection: Field | FragmentSpread | InlineFragment
StringValue
Convenience enum to create a Description. Can be a Top level, a Field level or an Input level. The variants are distinguished by the way they get displayed, e.g. number of leading spaces.
Type_
Convenience Type_ implementation used when creating a Field. Can be a NamedType, a NonNull or a List.
Value
The Value type represents available values you could give as an input.