Skip to content

Commit

Permalink
chore: demo for graphql & express
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed Aug 11, 2024
1 parent f6d2993 commit de594fb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/graphql/express/.gitignore
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions examples/graphql/express/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
32 changes: 32 additions & 0 deletions examples/graphql/express/server.js
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit de594fb

Please sign in to comment.