GraphQL

开发

GraphQL 支持许多不同的开发语言. 这个列表包含了一些更流行的服务器端框架,客户端库和一些有用的小东西.

服务器端库 #

除了 GraphQL 参考 JavaScript 实现, 服务器端库包括:

C# / .NET #

graphql-dotnet: GraphQL for .NET #

using System;
using GraphQL;
using GraphQL.Types;

public class Program
{
  public static void Main(string[] args)
  {
    var schema = Schema.For(@"
      type Query {
        hello: String
      }
    ");

    var json = schema.Execute(_ =>
    {
      _.Query = "{ hello }";
      _.Root = new { Hello = "Hello World!" };
    });

    Console.WriteLine(json);
  }
}
  • graphql-net: Convert GraphQL to IQueryable
  • Entity GraphQL: .NET Core GraphQL library. Compiles to IQueryable to easily expose a schema from an existing data model (E.g. from an Entity Framework data model)
  • DotNetGraphQLQueryGen: .NET Core library to generate classes from a GraphQL schema for type-safe querying in dotnet
  • Hot Chocolate: GraphQL Server for .NET core and .NET classic

Clojure #

alumbra #

适用于 Clojure 的一组可重用的 GraphQL 组件, 符合 alumbra.spec 的数据结构。

(require '[alumbra.core :as alumbra]
         '[claro.data :as data])

(def schema
  "type Person { name: String!, friends: [Person!]! }
   type QueryRoot { person(id: ID!): Person, me: Person! }
   schema { query: QueryRoot }")

(defrecord Person [id]
  data/Resolvable
  (resolve! [_ _]
    {:name    (str "Person #" id)
     :friends (map ->Person  (range (inc id) (+ id 3)))}))

(def QueryRoot
  {:person (map->Person {})
   :me     (map->Person {:id 0})})

(def app
  (alumbra/handler
    {:schema schema
     :query  QueryRoot}))

(defonce my-graphql-server
  (aleph.http/start-server #'app {:port 3000}))
$ curl -XPOST "http://0:3000" -H'Content-Type: application/json' -d'{
  "query": "{ me { name, friends { name } } }"
}'
{"data":{"me":{"name":"Person #0","friends":[{"name":"Person #1"},{"name":"Person #2"}]}}}

graphql-clj #

提供 GraphQL 实现的 Clojure 库。

graphql-clj 实现的 Hello World 代码:


(def schema "type QueryRoot {
    hello: String
  }")

(defn resolver-fn [type-name field-name]
  (get-in {"QueryRoot" {"hello" (fn [context parent & rest]
                              "Hello world!")}}
          [type-name field-name]))

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

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

lacinia #

A full implementation of the GraphQL specification that aims to maintain external compliance with the specification.

Elixir #

Erlang #

Go #

Groovy #

gorm-graphql #

Core Library - The GORM GraphQL library provides functionality to generate a GraphQL schema based on your GORM entities. In addition to mapping domain classes to a GraphQL schema, the core library also provides default implementations of "data fetchers" to query, update, and delete data through executions of the schema.

Grails Plugin - In a addition to the Core Library, the GORM GraphQL Grails Plugin:

  • Provides a controller to receive and respond to GraphQL requests through HTTP, based on their guidelines.
  • Generates the schema at startup with spring bean configuration to make it easy to extend.
  • Includes a GraphiQL browser enabled by default in development. The browser is accessible at /graphql/browser.
  • Overrides the default data binder to use the data binding provided by Grails
  • Provides a trait to make integration testing of your GraphQL endpoints easier

See the documentation for more information.

GQL #

GQL is a Groovy library for GraphQL

Java #

graphql-java #

用于构建GraphQL API的Java库.

graphql-java 实现的 hello world 代码:

import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;

public class HelloWorld {

    public static void main(String[] args) {
        String schema = "type Query{hello: String} schema{query: Query}";

        SchemaParser schemaParser = new SchemaParser();
        TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

        RuntimeWiring runtimeWiring = new RuntimeWiring()
                .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
                .build();

        SchemaGenerator schemaGenerator = new SchemaGenerator();
        GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);

        GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
        ExecutionResult executionResult = build.execute("{hello}");

        System.out.println(executionResult.getData().toString());
        // Prints: {hello=world}
    }
}

有关设置的更多信息, 请参阅 graphql-java文档.

JavaScript #

GraphQL.js (github) (npm) #

GraphQL 规范的参考实现, 专为在 Node.js 环境中运行 GraphQL 而设计.

通过 命令行 执行 GraphQL.js hello world 脚本:

npm install graphql

然后执行 node hello.js.

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) #

通过 Express 实现的 GraphQL 参考实现, 你可以联合 Express 运行或独立运行.

运行一个 express-graphql hello world 服务器:

npm install express express-graphql graphql

然后执行 node server.js.

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'));

apollo-server (github) (npm) #

一组来自 Apollo 的 GraphQL 服务器软件包, 可与各种Node.js HTTP框架(Express, Connect, Hapi, Koa等)配合工作.

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

npm install apollo-server-express express

然后运行 node server.js.

server.js源码:

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
  console.log('Now browse to http://localhost:4000' + server.graphqlPath)
);

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

Kotlin #

  • graphql-kotlin: A set of libraries for running GraphQL server in Kotlin.

Perl #

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.
  • Railt: A PHP GraphQL Framework.
  • Lighthouse: A GraphQL server for Laravel
  • GraphQLBundle: A GraphQL server for Symfony
  • WPGraphQL: A free, open-source WordPress plugin that provides an extendable GraphQL schema and API for any WordPress site

API Platform (github) #

API Platform is a fully-featured, flexible and extensible API framework built on top of Symfony. The following class is enough to create both a Relay-compatible GraphQL server and a hypermedia API supporting modern REST formats (JSON-LD, JSONAPI...):

<?php

namespace AppEntity;

use ApiPlatformCoreAnnotationApiResource;
use DoctrineORMMapping as ORM;

/**
 * Greet someone!
 *
 * @ApiResource
 * @ORMEntity
 */
class Greeting
{
    /**
     * @ORMId
     * @ORMColumn(type="guid")
     */
    public $id;

    /**
     * @var string Your nice message
     *
     * @ORMColumn
     */
    public $hello;
}

Other API Platform features include data validation, authentication, authorization, deprecations, cache and GraphiQL integration.

GraphQLite (github) #

GraphQLite is a library that offers an annotations-based syntax for GraphQL schema definition. It is framework agnostic with bindings available for Symfony and Laravel.

This code declares a "product" query and a "Product" Type:

class ProductController
{
    /**
     * @Query()
     */
    public function product(string $id): Product
    {
        // Some code that looks for a product and returns it.
    }
}

/**
 * @Type()
 */
class Product
{
    /**
     * @Field()
     */
    public function getName(): string
    {
        return $this->name;
    }
    // ...
}

Other GraphQLite features include validation, security, error handling, loading via data-loader pattern...

Siler (github) #

Siler is a PHP library powered with high-level abstractions to work with GraphQL.

To run a Siler hello world script:

type Query {
  hello: String
}
<?php
declare(strict_types=1);
require_once '/path/to/vendor/autoload.php';

use SilerDiactoros;
use SilerGraphql;
use SilerHttp;

$typeDefs = file_get_contents(__DIR__.'/schema.graphql');
$resolvers = [
    'Query' => [
        'hello' => 'world',
    ],
];
$schema = Graphqlschema($typeDefs, $resolvers);

echo "Server running at http://127.0.0.1:8080";

Httpserver(Graphqlpsr7($schema), function (Throwable $err) {
    var_dump($err);
    return Diactorosjson([
        'error'   => true,
        'message' => $err->getMessage(),
    ]);
})()->run();

It also provides functionality for the construction of a WebSocket Subscriptions Server based on how Apollo works.

Swift #

  • Graphiti: Swift library for building GraphQL schemas/types fast, safely and easily.

Python #

Graphene (github) #

用于构建 GraphQL API 的 Python 库.

运行 Graphene hello world 脚本:

pip install graphene

然后运行 python hello.py.

hello.py 源码:

import graphene

class Query(graphene.ObjectType):
  hello = graphene.String(name=graphene.String(default_value="World"))

  def resolve_hello(self, info, name):
    return 'Hello ' + name

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

能够很好的绑定: Relay, Django, SQLAlchemy, 和 Google App Engine.

Ruby #

graphql-ruby #

用于构建 GraphQL API 的 Ruby 库.

运行 graphql-ruby 的 hello world 脚本:

gem install graphql

然后执行 ruby hello.rb.

hello.rb 源码:

require 'graphql'

class QueryType < GraphQL::Schema::Object
  graphql_name 'Query'
  field :hello do
    type types.String
    resolve -> (obj, args, ctx) { 'Hello world!' }
  end
end

class Schema < GraphQL::Schema
  query QueryType
end

puts Schema.execute('{ hello }').to_json

Relay 和 Rails 也很好的支持绑定.

Agoo #

A high performance web server with support for GraphQL. Agoo strives for a simple, easy to use API for GraphQL.

require 'agoo'

class Query
  def hello
    'hello'
  end
end

class Schema
  attr_reader :query

  def initialize
    @query = Query.new()
  end
end

Agoo::Server.init(6464, 'root', thread_count: 1, graphql: '/graphql')
Agoo::Server.start()
Agoo::GraphQL.schema(Schema.new) {
  Agoo::GraphQL.load(%^type Query { hello: String }^)
}
sleep

# To run this GraphQL example type the following then go to a browser and enter
# a URL of localhost:6464/graphql?query={hello}
#
# ruby hello.rb

Rust #

Scala #

Sangria (github): 支持 Relay 的 Scala GraphQL 库. #

sangria 实现的一个模型和查询 hello world:

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

GraphQL Clients #

C# / .NET #

Clojurescript #

  • re-graph: A GraphQL client implemented in Clojurescript with support for websockets.

Elm #

Flutter #

  • graphql: A GraphQL client implementation in Flutter.

Go #

  • graphql: A GraphQL client implementation in Go.

Java / Android #

  • Apollo Android: Java 实现支持强类型和缓存的 GraphQL 客户端.

  • Nodes: A GraphQL JVM Client designed for constructing queries from standard model definitions. By American Express.

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.
  • graphql-request: A simple and flexible JavaScript GraphQL client that works in all JavaScript environments (the browser, Node.js, and React Native) - basically a lightweight wrapper around fetch.
  • Lokka: A simple JavaScript GraphQL client that works in all JavaScript environments (the browser, Node.js, and React Native).
  • nanogql: Tiny GraphQL client library using template strings.
  • gq-loader: A simple JavaScript GraphQL client,Let the *.gql file be used as a module through webpack loader.
  • AWS Amplify: A JavaScript library for application development using cloud services, which supports GraphQL backend and React components for working with GraphQL data.
  • Grafoo: An all purpose GraphQL client with view layer integrations for multiple frameworks in just 1.6kb.
  • urql (github): A highly customizable and versatile GraphQL client for React.
  • graphqurl (npm): curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a dead-simple universal javascript GraphQL client.

Julia #

  • Diana.jl: A Julia GraphQL server implementation.

Swift / Objective-C 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.
  • GraphQL iOS: An Objective-C GraphQL client for iOS.
  • Graphaello: A Tool for Writing Declarative, Type-Safe and Data-Driven Applications in SwiftUI using GraphQL and Apollo

Python #

  • GQL: A GraphQL client in Python.
  • python-graphql-client: Simple GraphQL client for Python 2.7+.
  • sgqlc: A simple Python GraphQL client. Supports generating code generation for types defined in a GraphQL schema.

R #

  • ghql: General purpose GraphQL R client.
  • graphiql (npm): An interactive in-browser GraphQL IDE.
  • libgraphqlparser: A GraphQL query language parser in C++ with C and C++ APIs.
  • Graphql Language Service: An interface for building GraphQL language services for IDEs (diagnostics, autocomplete etc).
  • quicktype (github): Generate types for GraphQL queries in TypeScript, Swift, golang, C#, C++, and more.
  • GraphQL Code Generator: GraphQL code generator with flexible support for custom plugins and templates like Typescript (frontend and backend), React Hooks, resolvers signatures and more.
  • GraphQL Inspector: Compare schemas, validate documents, find breaking changes, find similar types, schema coverage, and more.
  • GraphQL Config: One configuration for all your GraphQL tools (supported by most tools, editors & IDEs).
  • GraphQL CLI: A command line tool for common GraphQL development workflows.
  • GraphQL Scalars: A library of custom GraphQL scalar types for creating precise, type-safe GraphQL schemas.
  • GraphQL Toolkit: A set of utils for faster development of GraphQL tools (Schema and documents loading, Schema merging and more).
  • SOFA: Generate REST API from your GraphQL API.

Services #

  • Apollo Graph Manager: A cloud service for monitoring the performance and usage of your GraphQL backend.
  • GraphCMS: A BaaS (Backend as a Service) that sets you up with a GraphQL backend as well as tools for content editors to work with the stored data.
  • Prisma (github): A BaaS (Backend as a Service) providing a GraphQL backend for your applications with a powerful web ui for managing your database and stored data.
  • Tipe (github): A SaaS (Software as a Service) content management system that allows you to create your content with powerful editing tools and access it from anywhere with a GraphQL or REST API.
  • AWS AppSync: Fully managed GraphQL service with realtime subscriptions, offline programming & synchronization, and enterprise security features as well as fine grained authorization controls.
  • Elide: A Java library that can expose a JPA annotated data model as a GraphQL service over any relational database.
  • Hasura (github): Hasura connects to your databases & microservices and instantly gives you a production-ready GraphQL API.
  • FaunaDB: Create an instant GraphQL backend by importing a gql schema. The database will create relations and indexes for you, so you'll be ready to query in seconds, without writing any database code. Serverless pricing, free to get started.

更多 #

  • awesome-graphql: 一个梦幻般的社区维护图书馆, 资源等的收藏.