Node Express GraphQL server
GraphiQL IDE
進行測試mkdir graphql-example cd graphql-example npm init
npm install --save apollo-server-express graphql-tools graphql express body-parser
爲了突出關鍵點,省略完整代碼完整代碼移步 quick-start.jshtml
quick-start.js
vi quick-start.js
// GraphQL schema const typeDefs = ` type Query { books: [Book] } type Book { title: String, author: String } `
// resolvers const resolvers = { Query: {books: () => books} }
const schema = makeExecutableSchema({ typeDefs, resolvers })
GraphQL 數據服務
app.use('/graphql', bodyParser.json(), graphqlExpress({schema}))
GraphIQL IDE
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}))
node quick-start.js
GraphiQL IDE
編輯器輸入網址 http://localhost:3000/graphiql
你能夠試着改變查詢條件
{ books { title author } }
URL
是代碼中定義的路由 Request URL: http://localhost:3000/graphql?
GraphiQL IDE
默認用 POST
方式{ "query": "{\n books {\n title\n author\n }\n}\n", "variables": null, "operationName": null }
query
查詢體
variables
參數
operationName
操做名稱