-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
45 lines (40 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
// The data below is mocked.
const data = require('./data/pokemon.js');
// The schema should model the full data object available.
const schema = buildSchema(`
type Pokemon {
id: String
name: String!
}
type Query {
getPokemons: [Pokemon]
getPokemon(name: String): Pokemon
}
`);
// The root provides the resolver functions for each type of query or mutation.
const root = {
getPokemons: () => {
return data;
},
getPokemon: (request) => {
return data.find(pokemon => pokemon.name === request.name);
}
};
// Start your express server!
const app = express();
/*
The only endpoint for your server is `/graphql`– if you are fetching a resource,
you will need to POST your query to that endpoint. Suggestion: check out Apollo-Fetch
or Apollo-Client. Note below where the schema and resolvers are connected. Setting graphiql
to 'true' gives you an in-browser explorer to test your queries.
*/
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');