Code Monkey home page Code Monkey logo

graphql-idl-parser's Introduction

graphql-idl-parser

A parser for GraphQL IDL files! This repository contains the code for both the Rust implementation, as well as the FFI dylib implementation, which allows you to generate a library for use in C projects (and other languages that play nice with C). See https://github.com/gjtorikian/graphql-idl-parser-ruby for a Ruby implementation!

Build Status

Why?

Two reasons:

  • I have plenty of experience doing Bison/LALR grammars in C, and I desperately wanted to learn some Rust.
  • Most of the GraphQL tooling, I'm sorry to say, appears to be in JavaScript. While that's cool, I really believe that when it comes to creating a tool ecosystem, base implementations ought to be in a language that can be consumed by other projects. In other words, the world doesn't need different GraphQL IDL parsers in Node, and Ruby, and Python, and Java, and Scala. The base format should be in something like C/C++/Rust, and then those higher level languages ought to make use of that one foundational library. This ensures consistency and correctness, no matter what your backend is written in.

Usage

Add this to your Cargo.toml:

[dependencies]
graphql-idl-parser = "^0.1"

and this to your crate root:

extern crate graphql_idl_parser;

After that, simply feed in a GraphQL IDL string to gqlidl::parse_schema, like:

let definitions = gqlidl::parse_schema(contents.as_str()).unwrap();
for def in definitions {
  // ...
}

Note that this library does not validate the format if your IDL. If you have multiple types with the same name, for example, they will be happily consumed as unique types. However, the parser will become irate if it comes across a malformed or unknown token.

Todo:

  • More documentation?
  • Support more of the IDL besides just types

graphql-idl-parser's People

Contributors

gjtorikian avatar

Stargazers

Michael Edelman  avatar Jeffrey Cocozziello avatar Jonian Guveli avatar Rust avatar Tom Houlé avatar fudanchii avatar Tim Voronov avatar Rick Moynihan avatar Julien Heller avatar Mark Tareshawty avatar Bo Jeanes avatar Elahn Ientile avatar Emile Bosch avatar Marc-Andre Giroux avatar Robert Mosolgo avatar Doug Tangren avatar messense avatar cybai (Haku) avatar azu avatar Masashi Fujita avatar Rushmore Mushambi avatar Charleno Pires avatar Matt Todd avatar Enrico Risa avatar

Watchers

 avatar James Cloos avatar  avatar

graphql-idl-parser's Issues

Usage from rust

I see your ffi example of matching on typename and calling the right fields for the right type, but I am interested in using this directly from rust, and this loses the benefits of the type system. Would it make sense to make the enum fields public, or structure it in some way that would avoid panicing when traversing a schema?

Types starting with __ are for internal use?

First off congratulations on your release and thank you for this amazing contribution to the Rust community 👏

Maybe I'm missing something but when I see types like __ToTriple and functions like __action0 I think these are implementation details not meant for use by third parties. Am I correct in assuming this or are they actually part of the API?

Use of undeclared type or module

Hello. New user to Rust, GraphQL, all of this basically. Wrote a quick script using some code you suggested and some code from the comments here. The code is as follows...

extern crate graphql_idl_parser;
use std::fs::File;
use std::io::prelude::*;

fn main() {

    let mut file = File::open("schema.idl").expect("Unable to open file");
    let mut contents = String::new();
    file.read_to_string(&mut contents);

    let definitions = gqlidl::parse_schema(contents.as_str()).unwrap();

    for def in definitions {
        match def {
            TypeDefinition::ScalarType(scalar) => {
                println!("{}", scalar.name);
            }
        }
    }
}

When attempting to run this I am seeing errors as follows:

C:\Users\jcocozzi\.cargo\bin\idl_parser\src>cargo build
   Compiling idl_parser v0.1.0 (file:///C:/Users/jcocozzi/.cargo/bin/idl_parser)
error[E0433]: failed to resolve. Use of undeclared type or module `gqlidl`
  --> src\main.rs:11:23
   |
11 |     let definitions = gqlidl::parse_schema(contents.as_str()).unwrap();
   |                       ^^^^^^ Use of undeclared type or module `gqlidl`

error[E0433]: failed to resolve. Use of undeclared type or module `TypeDefinition`
  --> src\main.rs:15:13
   |
15 |             TypeDefinition::ScalarType(scalar) => {
   |             ^^^^^^^^^^^^^^ Use of undeclared type or module `TypeDefinition`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `idl_parser`.

To learn more, run the command again with --verbose.

C:\Users\jcocozzi\.cargo\bin\idl_parser\src>

It would seem as it is not able to find any of these objects. Any suggestions?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.