graphqld: A graphql backend written in D

Robert Schadek rschadek at symmetryinvestments.com
Wed Mar 20 17:44:40 UTC 2019


At Symmetry [6] we needed a graphql [1] backend.
So I wrote one.

Grapqhl is a query language, initially developed by facebook, 
that can be considered to be a replacement for REST.
Grapqhl allows you efficiently query an endpoint and select what 
data you actually want.
Clients are trivial to created, all they need to be able to do is 
a http get/post (curl)
Graphql endpoint are introspectable.
If all you know is the ip of the endpoint you can query for all 
possible types, queries, and how they relate to each other.
The tool graphiql-app [4] uses this information to implement 
typeahead.
Look at [5] to see what is possible with that.

Using graphqld [2,3] you can now do the following.

So given a schema:
```D
interface Query {
	Nullable!Starship starship(long id);
	Starship[] starships(float overSize = 100.0);
}

class Schema {
	Query queryType;
}

abstract class Character {
	long id;
	string name;
	Nullable!Starship ship;
}

abstract class Humanoid : Character {
	string species;
}

abstract class Android : Character {
	string primaryFunction;
}

class Starship {
	string name;
	string designation;
	double size;

	Character[] crew;
}
```

you can send a query like:
```
query a {
   starships {
     name
     crew {
       ...charac
     }
   }
}

fragment hyooman on Humanoid {
   species
}

fragment robot on Android {
   primaryFunction
}

fragment charac on Character {
   id
   ...robot
   ...hyooman
   name
   series
}
```

and get back json like that:
```json
{
   "error": [],
   "data": {
     "starships": [
       {
         "name": "Enterprise"
         "crew": [
           {
             "species": "Human",
             "series": [
               "TheNextGeneration",
               "DeepSpaceNine"
             ],
             "id": 0,
             "name": "Jean-Luc Picard"
           },
           {
             "species": "Klingon",
             "series": [
               "TheNextGeneration",
               "DeepSpaceNine",
               "DeepSpaceNine"
             ],
             "id": 1,
             "name": "Worf"
           },
           {
             "primaryFunction": "Becoming Human",
             "series": [
               "TheNextGeneration"
             ],
             "id": 5,
             "name": "Data"
           },
   ...
}
```

Graphqld is still somewhat rough around the edges, but should be 
in a usable state.
The example in the test folder gives a good impression of how to 
work with
resolvers.
To play with the example, I suggest the tool graphiql-app [4].

The parser is build using darser [7].

[1] https://graphql.org/
[2] https://github.com/burner/graphqld
[3] https://code.dlang.org/packages/graphqld
[4] https://github.com/skevy/graphiql-app
[5] 
https://github.com/burner/graphqld/blob/master/test/introspectionquery.gql
[6] http://symmetryinvestments.com/
[7] https://code.dlang.org/packages/darser


More information about the Digitalmars-d-announce mailing list