File tree 9 files changed +1366
-64
lines changed
typeorm-basic-integration
9 files changed +1366
-64
lines changed Original file line number Diff line number Diff line change @@ -48,22 +48,14 @@ const compiledSchema = compileSchema(SuperSchema)
48
48
49
49
` compiledSchema ` is a regular GQL executable schema compatible with ` graphql-js ` library.
50
50
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 :
52
52
53
53
``` ts
54
- import express from ' express'
55
- import graphqlHTTP from ' express-graphql'
54
+ import ApolloServer from ' apollo-server'
56
55
57
- const app = express ( )
56
+ const server = new ApolloServer ({ schema , graphiql: true } )
58
57
59
- app .use (
60
- ' /graphql' ,
61
- graphqlHTTP ({
62
- schema: compiledSchema ,
63
- graphiql: true
64
- })
65
- )
66
- app .listen (3000 , () =>
58
+ server .listen (3000 , () =>
67
59
console .log (' Graphql API ready on http://localhost:3000/graphql' )
68
60
)
69
61
```
Original file line number Diff line number Diff line change 1
- ## Basic express example
1
+ ## Basic apollo-server example
2
2
3
3
Example of basic graphql api able to resolve such query
4
4
@@ -11,9 +11,8 @@ query {
11
11
Here is all the server code required:
12
12
13
13
``` ts
14
- import express from ' express'
15
14
import { SchemaRoot , Query , compileSchema } from ' decapi'
16
- import graphqlHTTP from ' express-graphql '
15
+ import ApolloServer from ' apollo-server '
17
16
18
17
@SchemaRoot ()
19
18
class SuperSchema {
@@ -25,16 +24,11 @@ class SuperSchema {
25
24
26
25
const compiledSchema = compileSchema (SuperSchema )
27
26
28
- const app = express ( )
27
+ const server = new ApolloServer ({ schema , graphiql: true } )
29
28
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' )
36
31
)
37
- app .listen (3000 )
38
32
```
39
33
40
34
To start this example, in this folder run ` yarn install ` and ` yarn start ` . Server will be running under ` http://localhost:3000/graphql `
Original file line number Diff line number Diff line change 1
- import express from 'express'
2
- import graphqlHTTP from 'express-graphql'
3
-
4
1
import { schema } from './schema'
5
- import cors from 'cors'
6
2
7
- const app = express ( )
8
- app . use ( cors ( ) )
3
+ import Fastify from 'fastify'
4
+ import mercurius from 'mercurius'
5
+
6
+ const app = Fastify ( )
9
7
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
19
11
} )
12
+
13
+ app . listen ( 3003 )
Original file line number Diff line number Diff line change 1
1
{
2
- "name" : " basic-express-server " ,
2
+ "name" : " nested-mutation-or-query " ,
3
3
"version" : " 1.0.0" ,
4
4
"main" : " index.js" ,
5
5
"license" : " MIT" ,
6
6
"dependencies" : {
7
7
"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"
14
17
},
15
18
"scripts" : {
16
- "start" : " ts-node index.ts"
19
+ "start" : " ts-node -C ttypescript index.ts"
17
20
}
18
21
}
Original file line number Diff line number Diff line change 8
8
} from 'decapi'
9
9
10
10
@ObjectType ( )
11
- class Book {
11
+ class BookQuery {
12
12
@Field ( )
13
13
id : number
14
14
@Field ( )
@@ -18,9 +18,12 @@ class Book {
18
18
this . id = id
19
19
this . name = name
20
20
}
21
+ }
21
22
23
+ @ObjectType ( )
24
+ class BookMutation extends BookQuery {
22
25
@Field ( )
23
- edit ( name : string ) : Book {
26
+ edit ( name : string ) : BookQuery {
24
27
this . name = name
25
28
return this
26
29
}
@@ -31,20 +34,20 @@ class Book {
31
34
}
32
35
}
33
36
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' } )
37
40
]
38
41
39
42
@SchemaRoot ( )
40
43
class MySchema {
41
- @Mutation ( { type : Book } )
42
- book ( bookId : number ) : Book {
44
+ @Mutation ( )
45
+ book ( bookId : number ) : BookMutation | undefined {
43
46
return booksDb . find ( ( { id } ) => id === bookId )
44
47
}
45
48
46
- @Query ( { type : [ Book ] } )
47
- books ( ) : Book [ ] {
49
+ @Query ( { type : [ BookQuery ] } )
50
+ books ( ) : BookQuery [ ] {
48
51
// just a utility to cast our POJOs into a class of Book
49
52
return booksDb
50
53
}
Original file line number Diff line number Diff line change 1
1
{
2
2
"compilerOptions" : {
3
3
"module" : " commonjs" ,
4
- "lib" : [" es2017" ],
5
- "target" : " es5" ,
4
+ "lib" : [" ES2017" ],
5
+ "target" : " ES2018" ,
6
+ "strictNullChecks" : true ,
6
7
"emitDecoratorMetadata" : true ,
7
8
"experimentalDecorators" : true ,
8
9
"allowSyntheticDefaultImports" : true ,
9
- "esModuleInterop" : true
10
+ "esModuleInterop" : true ,
11
+ "plugins" : [{ "transform" : " typescript-rtti/dist/transformer" }]
10
12
}
11
13
}
You can’t perform that action at this time.
0 commit comments