4 releases

Uses new Rust 2021

new 0.1.3 May 12, 2022
0.1.2 Apr 28, 2022
0.1.1 Apr 1, 2022
0.1.0 Feb 18, 2022

#65 in Testing

Download history 13/week @ 2022-02-14 8/week @ 2022-02-21 2/week @ 2022-02-28 4/week @ 2022-03-14 4/week @ 2022-03-21 19/week @ 2022-03-28 5/week @ 2022-04-04 97/week @ 2022-04-11 212/week @ 2022-04-18 82/week @ 2022-04-25 72/week @ 2022-05-02 56/week @ 2022-05-09

423 downloads per month

MIT/Apache

345KB
8K SLoC

apollo-smith

A test case generator for GraphQL language.

Crates.io Download docs.rs docs

About

The goal of apollo-smith is to generate valid GraphQL documents by sampling from all available possibilities of GraphQL grammar.

We've written apollo-smith to use in fuzzing, but you may wish to use it for anything that requires GraphQL document generation.

apollo-smith is inspired by bytecodealliance's wasm-smith crate, and the article written by Nick Fitzgerald on writing test case generators in Rust.

This is still a work in progress, for outstanding issues, checkout out the apollo-smith label in our issue tracker.

Using apollo-smith with cargo fuzz

Define a new target with cargo fuzz,

$ cargo fuzz add my_apollo_smith_fuzz_target

and add apollo-smith to your Cargo.toml:

## fuzz/Cargo.toml

[dependencies]
apollo-smith = "0.1.3"

It can then be used in a fuzz_target along with the arbitrary crate,

// fuzz/fuzz_targets/my_apollo_smith_fuzz_target.rs

#![no_main]

use libfuzzer_sys::fuzz_target;
use arbitrary::Unstructured;
use apollo_smith::DocumentBuilder;

fuzz_target!(|input: &[u8]| {
    let mut u = Unstructured::new(input);
    let gql_doc = DocumentBuilder::new(&mut u)?;
    let document = gql_doc.finish();
    let document_str = String::from(document);


});

and fuzzed with the following command:

$ cargo +nightly fuzz run my_apollo_smith_fuzz_target

Using apollo-smith with apollo-parser

You can use apollo-parser to generate valid operations in apollo-smith. This can be done with the parser-impl feature flag.

## Cargo.toml

[dependencies]
apollo-smith = { version = "0.1.1", features = ["parser-impl"] }
use std::fs;

use apollo_parser::Parser;
use apollo_smith::{Document, DocumentBuilder};

use libfuzzer_sys::arbitrary::{Result, Unstructured};

/// This generate an arbitrary valid GraphQL operation
pub fn generate_valid_operation(input: &[u8]) {

    let parser = Parser::new(&fs::read_to_string("supergraph.graphql").expect("cannot read file"));

    let tree = parser.parse();
    if !tree.errors().is_empty() {
        panic!("cannot parse the graphql file");
    }

    let mut u = Unstructured::new(input);

    // Convert `apollo_parser::Document` into `apollo_smith::Document`.
    let apollo_smith_doc = Document::from(tree.document());

    // Create a `DocumentBuilder` given an existing document to match a schema.
    let mut gql_doc = DocumentBuilder::with_document(&mut u, apollo_smith_doc)?;
    let operation_def = gql_doc.operation_definition()?.unwrap();

    Ok(operation_def.into())
}

Feature flags

Enable parser-impl feature in apollo-smith is used to convert apollo-parser types to apollo-smith types. This is useful when you require the test-case generator to generate documents based on a given schema.

## Cargo.toml

[dependencies]
apollo-smith = { version = "0.1.1", features = ["parser-impl"] }

Limitations

  • Recursive object type not yet supported (example : myType { inner: myType })

License

Licensed under either of

at your option.

Dependencies

~0.4–0.9MB
~20K SLoC