Skip to content

Commit db4d085

Browse files
author
Alex Stanciu
committed
update
1 parent 9c1fc01 commit db4d085

File tree

7 files changed

+113
-45
lines changed

7 files changed

+113
-45
lines changed

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
### GraphQL Example
2+
3+
1. Install dependencies with `npm install`
4+
2. Run the server with `node server.js`
5+
3. Open the GraphiQL UI at `http://localhost:3000/graphiql`
6+
7+
#### Schemas
8+
There are other schema files that demonstrate different things:
9+
<table>
10+
<tr>
11+
<td>
12+
schema.js
13+
</td>
14+
<td>
15+
Default schema, just returns a value. Sample query:
16+
<pre lang="graphql">
17+
query {
18+
serverStatus
19+
}
20+
</pre>
21+
</td>
22+
</tr>
23+
<tr>
24+
<td>
25+
schema1.js
26+
</td>
27+
<td>
28+
Plain query with resolvers
29+
<pre lang="graphql">
30+
query {
31+
getUsers {
32+
name
33+
email
34+
age
35+
}
36+
}
37+
</pre>
38+
</td>
39+
</tr>
40+
<tr>
41+
<td>
42+
schema2.js
43+
</td>
44+
<td>
45+
Query With nested objects
46+
<pre lang="graphql">
47+
query {
48+
getUsers {
49+
name
50+
email
51+
age
52+
posts {
53+
id
54+
title
55+
}
56+
}
57+
}
58+
</pre>
59+
</td>
60+
</tr>
61+
<tr>
62+
<td>
63+
schema3.js
64+
</td>
65+
<td>
66+
Query with parameters
67+
<pre lang="graphql">
68+
query {
69+
getUser(name: "alex") {
70+
name
71+
email
72+
age
73+
}
74+
}
75+
</pre>
76+
</td>
77+
</tr>
78+
<tr>
79+
<td>
80+
schema4.js
81+
</td>
82+
<td>
83+
Query with Mutations
84+
<pre lang="graphql">
85+
mutation{
86+
addUser(newUser: {name: "steve", email: "[email protected]"}) {
87+
name
88+
email
89+
age
90+
}
91+
}
92+
</pre>
93+
94+
or using Variable parameters:
95+
Query:
96+
<pre lang="graphql">
97+
mutation addAUser($user: UserInput){
98+
addUser(newUser: $user ) {
99+
name
100+
email
101+
age
102+
}
103+
}
104+
</pre>
105+
Parameters:
106+
<pre lang="graphql">
107+
{"user":{"name": "steve", "email": "[email protected]"}}
108+
</pre>
109+
</td>
110+
</tr>

schema.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Starting Schema
21
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
32

43
const typeDefs = `

schema1.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// Plain query with resolvers
2-
// query {
3-
// getUsers {
4-
// name
5-
// email
6-
// age
7-
// }
8-
// }
91
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
102
let users = [
113
{name: 'alex', email: '[email protected]', age: '99'},

schema2.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
// With nested objects
2-
// query {
3-
// getUsers {
4-
// name
5-
// email
6-
// age
7-
// posts {
8-
// id
9-
// title
10-
// }
11-
// }
12-
// }
13-
141
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
152
let users = [
163
{name: 'justin', email: '[email protected]', age: '99'},

schema3.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
// Query with parameters
2-
// query {
3-
// getUser(name: "alex") {
4-
// name
5-
// email
6-
// age
7-
// }
8-
// }
9-
101
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
112
let users = [
123
{ name: 'justin', email: '[email protected]', age: '99' },

schema4.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
// With Mutations
2-
// mutation{
3-
// addUser(newUser: {name: "steve", email: "steve@auth0.com"}) {
4-
// name
5-
// email
6-
// age
7-
// }
8-
// }
9-
10-
111
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
122
let users = [
133
{ name: 'justin', email: '[email protected]', age: '99' },

server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ const bodyParser = require('body-parser');
33
const graphqlExpress = require('apollo-server-express').graphqlExpress
44
const graphiqlExpress = require('apollo-server-express').graphiqlExpress
55

6-
7-
8-
const myGraphQLSchema = require('./schema.js')
6+
const schema = require('./schema4.js')
97

108
const app = express();
119

12-
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema}));
10+
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: schema}));
1311
// app.use('/graphql', bodyParser.json(), graphqlExpress(req => ({ schema: myGraphQLSchema, context: {req} })));
1412

1513
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
1614
app.listen(3000);
15+
1716
console.log('open http://localhost:3000/graphiql')

0 commit comments

Comments
 (0)