GraphQL

Code

Many different programming languages support GraphQL. This list contains some of the more popular server-side frameworks, client libraries, and other useful stuff.

Server Libraries #

JavaScript #

GraphQL.js (github) (npm) #

The reference implementation of the GraphQL specification, designed for running GraphQL in a Node.js environment.

To run a GraphQL.js hello world script from the command line:

npm install graphql

Then run node hello.js with this code in hello.js:

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

var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

var root = { hello: () => 'Hello world!' };

graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});

express-graphql (github) (npm) #

The reference implementation of a GraphQL API server over an Express webserver. You can use this to run GraphQL in conjunction with a regular Express webserver, or as a standalone GraphQL server.

To run an express-graphql hello world server:

npm install express express-graphql graphql

Then run node server.js with this code in server.js:

var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

var root = { hello: () => 'Hello world!' };

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));

graphql-server (github) (npm) #

A set of GraphQL server packages from Apollo that work with various Node.js HTTP frameworks (Express, Connect, Hapi, Koa etc).

To run a hello world server with graphql-server-express:

npm install graphql-server-express body-parser express graphql graphql-tools

Then run node server.js with this code in server.js:

var express = require('express');
var bodyParser = require('body-parser');
var { graphqlExpress, graphiqlExpress } = require('graphql-server-express');
var { makeExecutableSchema } = require('graphql-tools');

var typeDefs = [`
type Query {
  hello: String
}

schema {
  query: Query
}`];

var resolvers = {
  Query: {
    hello(root) {
      return 'world';
    }
  }
};

var schema = makeExecutableSchema({typeDefs, resolvers});
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphiql'));

GraphQL Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI and Koa.

Ruby #

graphql-ruby #

A Ruby library for building GraphQL APIs.

To run a hello world script with graphql-ruby:

gem install graphql

Then run ruby hello.rb with this code in hello.rb:

require 'graphql'

QueryType = GraphQL::ObjectType.define do
  name 'Query'
  field :hello do
    type types.String
    resolve -> (obj, args, ctx) { 'Hello world!' }
  end
end

Schema = GraphQL::Schema.define do
  query QueryType
end

puts Schema.execute('{ hello }')

There are also nice bindings for Relay and Rails.

Python #

Graphene (github) #

A Python library for building GraphQL APIs.

To run a Graphene hello world script:

pip install graphene

Then run python hello.py with this code in hello.py:

import graphene

class Query(graphene.ObjectType):
  hello = graphene.String()

  def resolve_hello(self, args, context, info):
    return 'Hello world!'

schema = graphene.Schema(query=Query)
result = schema.execute('{ hello }')
print(result.data['hello'])

There are also nice bindings for Relay, Django, SQLAlchemy, and Google App Engine.

Scala #

Sangria (github): A Scala GraphQL library that supports Relay. #

An example of a hello world GraphQL schema and query with sangria:

import sangria.schema._
import sangria.execution._
import sangria.macros._

val QueryType = ObjectType("Query", fields[Unit, Unit](
  Field("hello", StringType, resolve = _ ⇒ "Hello world!")
))

val schema = Schema(QueryType)

val query = graphql"{ hello }"

Executor.execute(schema, query) map println

Java #

graphql-java #

A Java library for building GraphQL APIs.

Code that executes a hello world GraphQL query with graphql-java:

import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

public class HelloWorld {

    public static void main(String[] args) {

        GraphQLObjectType queryType = newObject()
                        .name("helloWorldQuery")
                        .field(newFieldDefinition()
                                .type(GraphQLString)
                                .name("hello")
                                .staticValue("world"))
                        .build();

        GraphQLSchema schema = GraphQLSchema.newSchema()
                        .query(queryType)
                        .build();
        Map<String, Object> result = new GraphQL(schema).execute("{hello}").getData();

        System.out.println(result);
        // Prints: {hello=world}
    }
}

See the graphql-java docs for more information on setup.

Clojure #

graphql-clj #

A Clojure library that provides a GraphQL implementation.

Code that executes a hello world GraphQL query with graphql-clj:

(require '[graphql-clj.parser :as parser])
(require '[graphql-clj.type :as type])

(def parsed-schema (parser/parse "type Query {
    hello: String
  }"))

(def type-schema (type/create-schema parsed-schema))

(defn resolver-fn [type-name field-name]
  (cond
    (and (= "Query" type-name) (= "hello" field-name)) (fn [context parent & rest]
                                                         "Hello world!")))

(require '[graphql-clj.executor :as executor])

(executor/execute nil type-schema resolver-fn "{ hello }")

Go #

PHP #

  • graphql-php: A PHP port of GraphQL reference implementation
  • graphql-relay-php: A library to help construct a graphql-php server supporting react-relay.

C# / .NET #

Elixir #

GraphQL Clients #

JavaScript #

  • Relay (github) (npm): Facebook's framework for building React applications that talk to a GraphQL backend.
  • Apollo Client (github): A powerful JavaScript GraphQL client, designed to work well with React, React Native, Angular 2, or just plain JavaScript.
  • Lokka: A simple JavaScript GraphQL client that works in all JavaScript environments - the browser, Node.js, and React Native.

Swift / iOS #

  • Apollo iOS (github): A GraphQL client for iOS that returns results as query-specific Swift types, and integrates with Xcode to show your Swift source and GraphQL side by side, with inline validation errors.

Tools #

More Stuff #

  • awesome-graphql: A fantastic community maintained collection of libraries, resources, and more.