diff --git a/examples/graphql/express/.gitignore b/examples/graphql/express/.gitignore new file mode 100644 index 0000000..16c4ef2 --- /dev/null +++ b/examples/graphql/express/.gitignore @@ -0,0 +1,28 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* + + +pnpm-lock.yaml +yarn.lock +package-lock.json diff --git a/examples/graphql/express/package.json b/examples/graphql/express/package.json new file mode 100644 index 0000000..352e943 --- /dev/null +++ b/examples/graphql/express/package.json @@ -0,0 +1,16 @@ +{ + "name": "express", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.19.2", + "express-graphql": "^0.12.0", + "graphql": "^15.9.0" + } +} diff --git a/examples/graphql/express/server.js b/examples/graphql/express/server.js new file mode 100644 index 0000000..5cf4788 --- /dev/null +++ b/examples/graphql/express/server.js @@ -0,0 +1,32 @@ +const { buildSchema } = require('graphql') +const express = require('express') +const { graphqlHTTP } = require('express-graphql') + +// 使用 GraphQL schema language 构建一个 schema +const schema = buildSchema(` + type Query { + hello: String + } +`) + +// 根节点为每个 API 入口端点提供一个 resolver 函数 +const resolver = { + hello: () => { + return 'Hello world!' + }, +} + +const app = express() + +app.use( + '/graphql', + graphqlHTTP({ + schema: schema, + rootValue: resolver, + graphiql: true, + }), +) + +app.listen(4000) + +console.log('Running a GraphQL API server at http://localhost:4000/graphql')