├── .gitignore ├── GraphSchema.Examples ├── AuthorPosts │ ├── AuthorPosts.graphql │ ├── GraphSchema.Translation.MostLikedPost │ ├── README.md │ ├── graphschema.sh │ ├── mutations │ └── queries └── scripts │ └── dgraph.sh ├── README.md └── graphschema.sh /.gitignore: -------------------------------------------------------------------------------- 1 | GraphSchema*.tar.gz 2 | dgraph 3 | -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/AuthorPosts.graphql: -------------------------------------------------------------------------------- 1 | type Author { 2 | id: ID! 3 | name: String! 4 | posts: [Post!]! 5 | level: Level! 6 | } 7 | 8 | type Post { 9 | id: ID! 10 | title: String 11 | text: String 12 | author: Author! 13 | } 14 | 15 | type Like { 16 | likedPost: Post! 17 | likedBy: Author! 18 | } 19 | 20 | enum Level { 21 | NEWB 22 | BEGINNER 23 | ADVANCED 24 | EXPERT 25 | } 26 | 27 | input AuthorInput { 28 | name: String! 29 | password: String! 30 | level: Level! 31 | } 32 | 33 | input PostInput { 34 | title: String 35 | text: String 36 | author: Author 37 | } 38 | 39 | input LikeInput { 40 | likedPost: Post! 41 | likedBy: Author! 42 | } 43 | 44 | directive @filter(filter: String!) on FIELD 45 | directive @paging(paging: String!) on FIELD 46 | directive @order(order: String!) on FIELD 47 | 48 | type Query { 49 | GetAuthor(id: ID!, secret: String): Author 50 | QueryAuthor(func: String): [Author!]! 51 | 52 | GetPost(id: ID!): Post 53 | QueryPost(func: String): [Post!]! 54 | 55 | MostLikedPost(query: String!): Post 56 | } 57 | 58 | type Mutation { 59 | AddAuthor(inputData: AuthorInput!): Author 60 | AddPost(inputData: PostInput!): Post 61 | AddLike(inputData: LikeInput!): Like 62 | } 63 | 64 | 65 | ### GraphSchema 66 | # [GraphSchema] author INV posts 67 | # [GraphSchema] AuthorInput . password -> Secret 68 | # [GraphSchema/Dgraph] Author . name @index(hash) 69 | # [GraphSchema/Dgraph] Post . title @index(term) 70 | # [GraphSchema/Dgraph] Post . text @index(fulltext) 71 | # [GraphSchema/Dgraph] MostLikedPost -> GraphSchema.Translation.MostLikedPost 72 | 73 | -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/GraphSchema.Translation.MostLikedPost: -------------------------------------------------------------------------------- 1 | posts as var(func: anyofterms(title, $query)) { 2 | uid 3 | } 4 | 5 | var(func: has(likedPost)) { 6 | initScore as math(1) 7 | likedPost @filter(uid(posts)) { 8 | postScore as math(initScore) 9 | } 10 | } 11 | 12 | mostLikedPost as var(func: uid(postScore), orderdesc: val(postScore), first: 1) 13 | -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/README.md: -------------------------------------------------------------------------------- 1 | GraphQL schema example modeling a social networking app with authors posting and liking content. 2 | 3 | If you're new to GraphSchema or just landed on this page, you might want to start by checking out the [docs](https://github.com/MichaelJCompton/GraphSchemaTools/wiki). 4 | 5 | Full explaination of this example is [here](https://github.com/MichaelJCompton/GraphSchemaTools/wiki/Example) 6 | -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/graphschema.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | if [ $1 == 'init-dgraph' ]; then 5 | if [ "$#" -le 3 ]; then 6 | docker run mjcomp/graphschema init-dgraph "--help" 7 | else 8 | folder="$(cd "$(dirname "$3")"; pwd -P)" 9 | file="$(basename "$3")" 10 | docker run --mount type=bind,source="$folder",destination=/data,readonly -p 10550:10550 mjcomp/graphschema init-dgraph "--schema-file" "/data/$file" "${@:4}" 11 | fi 12 | else 13 | docker run -p 10550:10550 mjcomp/graphschema "$@" 14 | fi 15 | -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/mutations: -------------------------------------------------------------------------------- 1 | ############################## 2 | # Add a new Author 3 | ############################## 4 | 5 | # Query body 6 | 7 | mutation addAuthor($input: AuthorInput!) { 8 | addAuthor(inputData: $input) { 9 | id, 10 | name 11 | } 12 | } 13 | 14 | 15 | # Query variables 16 | 17 | { 18 | "input": { 19 | "name": "A. Author", 20 | "password": "aPaSSwoRd" 21 | } 22 | } 23 | 24 | 25 | ############################## 26 | # Add a new Post 27 | ############################## 28 | 29 | # Query body 30 | 31 | mutation addPost($input: PostInput!) { 32 | addPost(inputData: $input) { 33 | id, 34 | title, 35 | text, 36 | author { name } 37 | } 38 | } 39 | 40 | # Query variables 41 | 42 | { 43 | "input": { 44 | "title": "GraphQL in Dgraph", 45 | "text": "Conventions based API from declarative schema", 46 | "author": { 47 | "uid": "0x2" 48 | } 49 | } 50 | } 51 | 52 | ############################## 53 | # Like a post 54 | ############################## 55 | 56 | # Query body 57 | 58 | # Query variables -------------------------------------------------------------------------------- /GraphSchema.Examples/AuthorPosts/queries: -------------------------------------------------------------------------------- 1 | ############################## 2 | # Get an author 3 | ############################## 4 | 5 | # Query body 6 | 7 | query getAuthor($inp: ID!) { 8 | getAuthor(id: $inp) { 9 | id, 10 | name, 11 | posts { title } 12 | } 13 | } 14 | 15 | # Query variables 16 | 17 | { 18 | "inp": "0x2" 19 | } 20 | 21 | 22 | ############################## 23 | # Get all authors 24 | ############################## 25 | 26 | # Query body 27 | 28 | query queryAuthor { 29 | queryAuthor { 30 | id, 31 | name 32 | } 33 | } 34 | 35 | 36 | ############################## 37 | # Check password 38 | ############################## 39 | 40 | # check if a password matches and 41 | # return the Author's data. If 42 | # the password check fails, null 43 | # is returned 44 | 45 | # Query body 46 | 47 | query checkAuthor($inp: ID!, $password: String) { 48 | getAuthor(id: $inp, secret: $password) { 49 | id, 50 | name 51 | } 52 | } 53 | 54 | # Query variables (to succeed) 55 | 56 | { 57 | "inp": "0x2", 58 | "password": "aPaSSwoRd" 59 | } 60 | 61 | # Query variables (to fail) 62 | 63 | { 64 | "inp": "0x2", 65 | "password": "Wr0nG" 66 | } 67 | 68 | 69 | ############################## 70 | # Search for posts 71 | ############################## 72 | 73 | # All posts with title 74 | # containing word `GraphQL` 75 | 76 | # Query body 77 | 78 | query queryPost($inp: String!) { 79 | queryPost(func: $inp) { 80 | id, 81 | title, 82 | text, 83 | author { name } 84 | } 85 | } 86 | 87 | # Query variables 88 | 89 | { 90 | "inp": "anyofterms(title, \"GraphQL\")" 91 | } 92 | 93 | ############################## 94 | # Most liked post 95 | ############################## 96 | 97 | # A query translation allows you 98 | # have pretty much any query, not 99 | # just the conventions. 100 | # 101 | # This query gets the most liked 102 | # post with a given term in the title 103 | 104 | # Query body 105 | 106 | query mostliked($query: String!) { 107 | mostLikedPost(query: $query) { 108 | title 109 | text 110 | } 111 | } 112 | 113 | # Query variables 114 | 115 | { 116 | "query": "GraphQL" 117 | } 118 | -------------------------------------------------------------------------------- /GraphSchema.Examples/scripts/dgraph.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | # Stand up a Dgraph instance with data stored in ./dgraph 5 | # Assumes dgraph is installed separately. 6 | # Run `dgraph-ratel` if you want to navigate to http://localhost:8000 to query data loaded by the examples. 7 | 8 | 9 | start() { 10 | if [ ! -d "dgraph" ]; then 11 | mkdir "dgraph" 12 | fi 13 | 14 | dgraph zero -w dgraph/wz > dgraph/zero.log 2>&1 & 15 | 16 | sleep 5s 17 | 18 | dgraph alpha -p dgraph/p -w dgraph/w --lru_mb 2048 --zero localhost:5080 > dgraph/server.log 2>&1 & 19 | } 20 | 21 | stop() { 22 | curl -s localhost:8080/admin/shutdown 23 | 24 | sleep 5s 25 | 26 | pkill "dgraph" 27 | 28 | echo "" 29 | echo "Shutdown complete" 30 | } 31 | 32 | clean() { 33 | rm -rf dgraph 34 | } 35 | 36 | if [ $# -gt 0 ]; then 37 | if [ $1 == 'start' ]; then 38 | start 39 | elif [ $1 == 'stop' ]; then 40 | stop 41 | elif [ $1 == 'clean' ]; then 42 | clean 43 | else 44 | echo "start, stop or clean" 45 | fi 46 | else 47 | echo "start, stop or clean" 48 | fi 49 | 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GraphSchema GraphQL tools 2 | 3 | GraphSchema is a tool that can run a GraphQL API from just the declarative GraphQL schema. It serves the GraphQL API over http and uses Dgraph as a backend graph database. 4 | 5 | GraphSchema is currently released as an early alpha in a container image in the docker hub at [mjcomp/graphschema](https://hub.docker.com/r/mjcomp/graphschema) it works with Dgraph v1.0.13. 6 | 7 | Main features 8 | 9 | - Running GraphQL API directly from schema 10 | - Graph database backend for a graph query langauge 11 | - Exposes all the Dgraph query power, plus GraphQL 12 | - Stateless API endpoint made for containers 13 | - Easily run GraphQL API replicas against a single backend DB 14 | 15 | Read more in the [docs](https://github.com/MichaelJCompton/GraphSchemaTools/wiki) and explore the [examples](https://github.com/MichaelJCompton/GraphSchemaTools/tree/master/GraphSchema.Examples). 16 | 17 | See [these notes](https://github.com/MichaelJCompton/GraphSchemaTools/wiki/Roadmap-and-Issues) about current status and future directions. 18 | 19 | Please add issues, requests and suggestions [here](https://github.com/MichaelJCompton/GraphSchemaTools/issues). 20 | -------------------------------------------------------------------------------- /graphschema.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | if [ $1 == 'init-dgraph' ]; then 5 | if [ "$#" -le 3 ]; then 6 | docker run mjcomp/graphschema init-dgraph "--help" 7 | else 8 | folder="$(cd "$(dirname "$3")"; pwd -P)" 9 | file="$(basename "$3")" 10 | docker run --mount type=bind,source="$folder",destination=/data,readonly -p 10550:10550 mjcomp/graphschema init-dgraph "--schema-file" "/data/$file" "${@:4}" 11 | fi 12 | else 13 | docker run -p 10550:10550 mjcomp/graphschema "$@" 14 | fi 15 | --------------------------------------------------------------------------------