npm i express express-graphql graphql mongoose
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
address: String,
commune: String,
country: String,
latitude: Number,
longitude: Number,
name: String,
phone: String,
region: String
})
module.exports = mongoose.model('Bombero', Schema)
const { GraphQLString, GraphQLFloat, GraphQLObjectType } = require('graphql')
module.exports = new GraphQLObjectType({
name: 'Bombero',
fields: () => ({
address: { type: GraphQLString },
commune: { type: GraphQLString },
country: { type: GraphQLString },
latitude: { type: GraphQLFloat },
longitude: { type: GraphQLFloat },
name: { type: GraphQLString },
phone: { type: GraphQLString },
region: { type: GraphQLString }
})
})
const { GraphQLSchema, GraphQLObjectType } = require('graphql')
const { bomberos } = require('./mutations')
const Query = new GraphQLObjectType({
name: 'BomberoSchema',
fields: () => ({
bomberos: bomberos
})
})
module.exports = new GraphQLSchema({ query: Query })
const { GraphQLList, GraphQLString } = require('graphql')
const Bombero = require('./model')
const BomberoType = require('./type')
const bomberos = {
type: new GraphQLList(BomberoType),
args: {
commune: { type: GraphQLString },
region: { type: GraphQLString },
country: { type: GraphQLString }
},
resolve: (root, args) => Bombero.find(args).exec()
}
module.exports = { bomberos: bomberos }
const express = require('express')
const graphqlHTTP = require('express-graphql')
const schema = require('./schema')
const app = express()
...
app.use('/graphql', graphqlHTTP(req => ({schema, graphiql: true})))
...
curl -XGET -H "Content-Type:application/graphql" \
-d 'query { bomberos { name } }' \
https://example-graphql-glmgpqnytt.now.sh/graphql
{
"data": {
"bomberos": [
...
{ "name": "Cuerpo de Bomberos de Romeral" },
...
]
}
}
curl -XGET -H "Content-Type:application/graphql" \
-d 'query { bomberos(region: "X Región") { name } }' \
https://example-graphql-glmgpqnytt.now.sh/graphql
{
"data": {
"bomberos": [
...
{ "name": "Cuerpo de Bomberos de San Juan De La Costa" },
...
]
}
}