我正在尝试构建一个小型的Next.js应用程序,该应用程序通过GraphQL和通过Mongoose连接的MongoDB在Google地图上的输入中存储路线。我正在关注the docs on Mongoose for using GeoJSON,但不确定如何更新TypeDef以反映Mongoose模式。随附的代码段和指向Github存储库的链接。谢谢!
(https://github.com/thomasharbin/rollerbladenashville)
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ["Point"],
required: true,
},
coordinates: {
type: [Number],
required: true,
},
});
const routeSchema = new mongoose.Schema({
name: String,
skill: Number,
date: Date,
neighborhood: String,
description: String,
location: {
type: pointSchema,
required: true,
},
});
export let Route =
mongoose.models.Route || mongoose.model("Route", routeSchema);
const Mutations = {
createRoute: async (obj, { route }, context) => {
try {
const newRoute = await Route.create({ ...route });
// pubSub.publish(ROUTE_ADDED, { routeAdded: newRoute });
const allRoutes = await Route.find();
return allRoutes;
} catch (e) {
return [];
}
},
};
export default Mutations;
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ["Point"],
required: true,
},
coordinates: {
type: [Number],
required: true,
},
});
const routeSchema = new mongoose.Schema({
name: String,
skill: Number,
date: Date,
neighborhood: String,
description: String,
location: {
type: pointSchema,
required: true,
},
});
export let Route =
mongoose.models.Route || mongoose.model("Route", routeSchema);
const Query = {
routes: async () => {
try {
const allRoutes = await Route.find();
return allRoutes;
} catch (e) {
console.log(`e`, e);
return [];
}
},
route: (obj, { id }) => {
try {
const foundRoute = routes.findById(id);
return foundRoute;
} catch (e) {
console.log(e);
}
},
users: () => {
return users;
},
};
export default Query;
const typeDefs = gql`
scalar Date
scalar PointSchema
type Route {
id: ID
name: String!
neighborhood: String
description: String
skill: Int
date: Date
location: PointSchema
}
input RouteInput {
name: String
id: ID
date: Date
skill: Int
neighborhood: String
description: String
location: PointSchema
}
type Query {
routes: [Route]
route(id: ID): Route
users: [User]
}
type Mutation {
createRoute(route: RouteInput): [Route]
}
type Map {
start: String!
end: String!
}
type Fountain {
lat: String!
long: String!
desc: String!
}
type User {
id: ID
first: String!
last: String
email: String!
skates: String
}
`;
export default typeDefs;