PMunch / protobuf-nim

Protobuf implementation in pure Nim that leverages the power of the macro system to not depend on any external tools
MIT License
171 stars 14 forks source link

[TODO] [question] repurposing protobuf-nim for a native graphql-nim parser? #13

Closed timotheecour closed 6 years ago

timotheecour commented 6 years ago

@PMunch just curious what are your thoughts on this, sorry it's not related to protobuf proper.

graphql seems like the future for web services, advantageously replacing REST in most use cases; unfortunately all I could find is this project https://github.com/samdmarshall/GraphQL.nim that wraps around C++ libgraphqlparser wrapping around libgraphqlparser doesn't lead to ease of use for user code, leading to lots of boilerplate code; ideally using graphql and parsing schema and queries should be as simple as in javascript via a DSL (eg https://graphql.org/graphql-js/).

I'm wondering whether a new protobuf-nim could serve as basis for a new graphql-nim project that would have same principles and ease of use as protobuf-nim (in particular compile time parsing). eg:

var { graphql, buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => {
    return 'Hello world!';
  },
};

// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});
import graphql

# Construct a schema, using GraphQL schema language
const schema = """
  type Query {
    hello: String
  }
""".buildSchema

# The root provides a resolver function for each API endpoint
proc hello(): auto=
    return "Hello world!"

# Run the GraphQL query '{ hello }' and print out the response
let resp = graphql(schema, """{ hello }""")
assert resp.errors == nil
assert resp.data.$ == """{ data: { hello: 'Hello world!' } }"""

links

https://forum.nim-lang.org/t/3321 GraphQL? https://github.com/samdmarshall/GraphQL.nim/issues/1 https://github.com/graphql-python/graphene/blob/master/examples/simple_example.py python API https://graphql.org/graphql-js/ javascript API

PMunch commented 6 years ago

Hmm, it would definitely be possible, but I'm not sure if there's any benefit to it. If you look at how the json module in the stdlib is implemented, something similar could be done for GraphQL. If that doesn't make the usage easy enough then you could of course write a DSL that set up more stuff, but I think that would be a good place to start.