【发布时间】:2019-12-06 14:26:11
【问题描述】:
这是我第一次使用 graphQL,我有两个相关的问题。
我已经模糊地浏览了这些文档,并且正在构建自己的东西。
所以,我现在的设置非常简单。在我的应用程序的起点,我有这样的东西
const express = require('express')
const app = express();
const graphqlHTTP = require("express-graphql")
const schema = require('./schema/schema')
app.use("/graphql", graphqlHTTP({
schema: schema
}));
app.get('/', (req, res) => {
res.json({"message": "Welcome to EasyNotes application. Take notes quickly. Organize and keep track of all your notes."});
});
//Listen to specific post
app.listen(4000, () => {
console.log("Listening for request on port 4000")
});
我的 Schema 看起来像这样
const graphql = require("graphql")
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLList,
GraphQLID,
GraphQLNonNull,
GraphQLInt
} = graphql
const GenderType = new GraphQLObjectType({
name: 'Gender',
fields: () => ({
male: {
}
})
})
const UserType = new GraphQLObjectType({
name: 'User', // Importance of Name here
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
gender: {
type: GenderType // Create custom type for it later
}
})
})
在我上面的代码 sn-p 中,在 UserType 内部,我希望我的 GenderType 是男性或女性。如何编写我的自定义类型以使其仅接受值“男性”或“女性”?
【问题讨论】:
标签: javascript node.js graphql