數據查詢語言:GraphQL.js
GraphQL.js (GraphQLJS)是 JavaScript 參考實現 GraphQL 的一個技術預覽,非死book 開發的一種查詢語言,用于在復雜的應用程序的數據模型中,描述數據要求。
使用示例:
從 npm 安裝 GraphQL.js
npm install graphql
首先,建立GraphQL 型架構映射到你的代碼庫。
import { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';var schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'RootQueryType', fields: { hello: { type: GraphQLString, resolve: () => 'world' } } }) });
然后,服務針對該類型架構的查詢結果。
var query = '{ hello }'; graphql(schema, query).then(result => { // Prints // { // data: { hello: "world" } // } console.log(result); });
這將運行一個查詢獲取定義一個字段。graphql功能將首先確保查詢語法和語義有效執行,否則報告錯誤。
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); });
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!