graphql

2018-10-13 admin

graphql是什么

什么是graphql,A Query Language and Runtime which can target any service.

graphql使用教程帮助文档

GraphQL.js

The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.

npm version Build Status Coverage Status

See more complete documentation at http://graphql.org/ and http://graphql.org/graphql-js/.

Looking for help? Find resources from the community.

Getting Started

An overview of GraphQL in general is available in the README for the Specification for GraphQL. That overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started with this repository is to walk through that README and the corresponding tests in parallel.

Using GraphQL.js

Install GraphQL.js from npm

With yarn:

yarn add graphql

or alternatively using npm:

npm install --save graphql

GraphQL.js provides two important capabilities: building a type schema, and serving queries against that type schema.

First, build a GraphQL type schema which maps to your code base.

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString
} from 'graphql';

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        }
      }
    }
  })
});

This defines a simple schema with one type and one field, that resolves to a fixed value. The resolve function can return a value, a promise, or an array of promises. A more complex example is included in the top level tests directory.

Then, serve the result of a query against that type schema.

var query = '{ hello }';

graphql(schema, query).then(result => {

  // Prints
  // {
  //   data: { hello: "world" }
  // }
  console.log(result);

});

This runs a query fetching the one field defined. The graphql function will first ensure the query is syntactically and semantically valid before executing it, reporting errors otherwise.

var query = '{ boyhowdy }';

graphql(schema, query).then(result => {

  // Prints
  // {
  //   errors: [
  //     { message: 'Cannot query field boyhowdy on RootQueryType',
  //       locations: [ { line: 1, column: 3 } ] }
  //   ]
  // }
  console.log(result);

});

Want to ride the bleeding edge?

The npm branch in this repository is automatically maintained to be the last commit to master to pass all tests, in the same form found on npm. It is recommended to use builds deployed to npm for many reasons, but if you want to use the latest not-yet-released version of graphql-js, you can do so by depending directly on this branch:

npm install graphql@git://github.com/graphql/graphql-js.git#npm

Using in a Browser

GraphQL.js is a general purpose library and can be used both in a Node server and in the browser. As an example, the GraphiQL tool is built with GraphQL.js!

Building a project using GraphQL.js with webpack or rollup should just work and only include the portions of the library you use. This works because GraphQL.js is distributed with both CommonJS (require()) and ESModule (import) files. Ensure that any custom build configurations look for .mjs files!

Contributing

We actively welcome pull requests, learn how to contribute.

Changelog

Changes are tracked as GitHub releases.

License

GraphQL.js is MIT-licensed.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-42574.html

文章标题:graphql

相关文章
【译】关于GraphQL,你需要知道这些
作者:Weblab Technology | 译:kongfanjia 原文地址: https://medium.com/@weblab_tech/graphql-everything-you-need-to-know-58756ff25...
2018-08-31
在vue项目中集成graphql 即vue-ApolloClient
1.什么是graphql GraphQL 是一个用于 API 的查询语言,是一个使用基于类型系统来执行查询的服务端运行时 下图展示graphql所处的位置 2.优点 1.GraphQL API 有强类型 schema GraphQ...
2018-09-08
graphql-request
graphql-request是什么 什么是graphql-request, graphql-request官网:官网 graphql-request源码仓库:源码仓库 graphql-request下载地址:点此下载 点此下载2 g...
2018-10-15
前端每周清单第 52 期: Webpack 4.0,GraphQL 安全加固,去中心化的 Web
作者:王下邀月熊 编辑:徐川 前端每周清单专注前端领域内容,以对外文资料的搜集为主,帮助开发者了解一周前端热点;分为新闻热点、开发教程、工程实践、深度阅读、开源项目、巅峰人生等栏目。欢迎关注【前端之巅】微信公众号(ID: frontsho...
2018-02-27
graphql-config
graphql-config是什么 什么是graphql-config,The easiest way to configure your development environment with your GraphQL schema (...
2018-10-13
graphql-tools
graphql-tools是什么 什么是graphql-tools,Useful tools to create and manipulate GraphQL schemas. graphql-tools官网:官网 graphql-too...
2018-10-13
graphql-import
graphql-import是什么 什么是graphql-import, graphql-import官网:官网 graphql-import源码仓库:源码仓库 graphql-import下载地址:点此下载 点此下载2 graphq...
2018-10-13
graphql-type-json
graphql-type-json是什么 什么是graphql-type-json,JSON scalar type for GraphQL.js graphql-type-json官网:官网 graphql-type-json源码仓库:...
2018-10-13
eslint-plugin-graphql
eslint-plugin-graphql是什么 什么是eslint-plugin-graphql,GraphQL ESLint plugin. eslint-plugin-graphql官网:官网 eslint-plugin-graph...
2018-10-13
【译】解析 GraphQL 的查询语法
原文链接:The Anatomy of a GraphQL Query GraphQL 日渐成为数据查询的主流标准之一,整个生态圈也蓬勃发展。本文则由浅入深地详细介绍基础的 GraphQL 格式与关键字,有助于初学者对于 GraphQL ...
2018-12-06
回到顶部