Skip to content

Commit 5da015d

Browse files
committed
convert nested-mutation-or-query to mercurius
1 parent 1213ebf commit 5da015d

File tree

9 files changed

+1366
-64
lines changed

9 files changed

+1366
-64
lines changed

README.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,14 @@ const compiledSchema = compileSchema(SuperSchema)
4848

4949
`compiledSchema` is a regular GQL executable schema compatible with `graphql-js` library.
5050

51-
To use it with `express`, you'd have to simply:
51+
To use it with `apollo-server`, you'd have to use like this:
5252

5353
```ts
54-
import express from 'express'
55-
import graphqlHTTP from 'express-graphql'
54+
import ApolloServer from 'apollo-server'
5655

57-
const app = express()
56+
const server = new ApolloServer({ schema, graphiql: true })
5857

59-
app.use(
60-
'/graphql',
61-
graphqlHTTP({
62-
schema: compiledSchema,
63-
graphiql: true
64-
})
65-
)
66-
app.listen(3000, () =>
58+
server.listen(3000, () =>
6759
console.log('Graphql API ready on http://localhost:3000/graphql')
6860
)
6961
```
+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Basic express example
1+
## Basic apollo-server example
22

33
Example of basic graphql api able to resolve such query
44

@@ -11,9 +11,8 @@ query {
1111
Here is all the server code required:
1212

1313
```ts
14-
import express from 'express'
1514
import { SchemaRoot, Query, compileSchema } from 'decapi'
16-
import graphqlHTTP from 'express-graphql'
15+
import ApolloServer from 'apollo-server'
1716

1817
@SchemaRoot()
1918
class SuperSchema {
@@ -25,16 +24,11 @@ class SuperSchema {
2524

2625
const compiledSchema = compileSchema(SuperSchema)
2726

28-
const app = express()
27+
const server = new ApolloServer({ schema, graphiql: true })
2928

30-
app.use(
31-
'/graphql',
32-
graphqlHTTP({
33-
schema: compiledSchema,
34-
graphiql: true
35-
})
29+
server.listen(3000, () =>
30+
console.log('Graphql API ready on http://localhost:3000/graphql')
3631
)
37-
app.listen(3000)
3832
```
3933

4034
To start this example, in this folder run `yarn install` and `yarn start`. Server will be running under `http://localhost:3000/graphql`
+9-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import express from 'express'
2-
import graphqlHTTP from 'express-graphql'
3-
41
import { schema } from './schema'
5-
import cors from 'cors'
62

7-
const app = express()
8-
app.use(cors())
3+
import Fastify from 'fastify'
4+
import mercurius from 'mercurius'
5+
6+
const app = Fastify()
97

10-
app.use(
11-
'/graphql',
12-
graphqlHTTP({
13-
schema,
14-
graphiql: true
15-
})
16-
)
17-
app.listen(3000, () => {
18-
console.log('Api ready on port 3000')
8+
app.register(mercurius, {
9+
schema,
10+
graphiql: true
1911
})
12+
13+
app.listen(3003)
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
2-
"name": "basic-express-server",
2+
"name": "nested-mutation-or-query",
33
"version": "1.0.0",
44
"main": "index.js",
55
"license": "MIT",
66
"dependencies": {
77
"class-transformer": "^0.2.3",
8-
"cors": "^2.8.5",
9-
"decapi": "^0.8.10",
10-
"express": "^4.17.1",
11-
"express-graphql": "^0.9.0",
12-
"ts-node": "^8.3.0",
13-
"typescript": "^3.6.2"
8+
"decapi": "^2.0.0-beta.4",
9+
"fastify": "^3.28.0",
10+
"graphql": "^16.3.0",
11+
"mercurius": "^9.3.6",
12+
"ts-node": "^10.7.0"
13+
},
14+
"devDependencies": {
15+
"ttypescript": "^1.5.13",
16+
"typescript": "^4.6.3"
1417
},
1518
"scripts": {
16-
"start": "ts-node index.ts"
19+
"start": "ts-node -C ttypescript index.ts"
1720
}
1821
}

examples/nested-mutation-or-query/schema.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from 'decapi'
99

1010
@ObjectType()
11-
class Book {
11+
class BookQuery {
1212
@Field()
1313
id: number
1414
@Field()
@@ -18,9 +18,12 @@ class Book {
1818
this.id = id
1919
this.name = name
2020
}
21+
}
2122

23+
@ObjectType()
24+
class BookMutation extends BookQuery {
2225
@Field()
23-
edit(name: string): Book {
26+
edit(name: string): BookQuery {
2427
this.name = name
2528
return this
2629
}
@@ -31,20 +34,20 @@ class Book {
3134
}
3235
}
3336

34-
const booksDb: Book[] = [
35-
new Book({ id: 1, name: 'Lord of the Rings' }),
36-
new Book({ id: 2, name: 'Harry Potter' })
37+
const booksDb: BookMutation[] = [
38+
new BookMutation({ id: 1, name: 'Lord of the Rings' }),
39+
new BookMutation({ id: 2, name: 'Harry Potter' })
3740
]
3841

3942
@SchemaRoot()
4043
class MySchema {
41-
@Mutation({ type: Book })
42-
book(bookId: number): Book {
44+
@Mutation()
45+
book(bookId: number): BookMutation | undefined {
4346
return booksDb.find(({ id }) => id === bookId)
4447
}
4548

46-
@Query({ type: [Book] })
47-
books(): Book[] {
49+
@Query({ type: [BookQuery] })
50+
books(): BookQuery[] {
4851
// just a utility to cast our POJOs into a class of Book
4952
return booksDb
5053
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"lib": ["es2017"],
5-
"target": "es5",
4+
"lib": ["ES2017"],
5+
"target": "ES2018",
6+
"strictNullChecks": true,
67
"emitDecoratorMetadata": true,
78
"experimentalDecorators": true,
89
"allowSyntheticDefaultImports": true,
9-
"esModuleInterop": true
10+
"esModuleInterop": true,
11+
"plugins": [{ "transform": "typescript-rtti/dist/transformer" }]
1012
}
1113
}

0 commit comments

Comments
 (0)