我使用 graphql-tag 和 apollo-server-express 来连接为 api 构建 graphQL 端点。目前我创建这样的架构定义:
属性.js
let model = {}
const typeDefs = gql`
extend type Query {
property(id: Int!): [Property]
}
type Property {
title: String
street: String
postalcode: String
city: String
country: String
}
`
const resolvers = {
Query: {
property: getTheEntriesFromDB,
},
}
model.typeDefs = typeDefs;
model.resolvers = resolvers;
module.exports = model;
和 server.js
const server = new ApolloServer({
modules: [
require('./modules/properties'),
],
}):
但是我想根据数据库模式自动创建类型“属性”。我发现我可以像这样创建一个 GraphQLObjectType:
const propertyType = new graphql.GraphQLObjectType({
name: 'Property',
fields: {
title: { type: graphql.GraphQLString },
street: { type: graphql.GraphQLString },
postalcode: { type: graphql.GraphQLString },
city: { type: graphql.GraphQLString },
country: { type: graphql.GraphQLString },
}
});
但我不知道如何将其与
let model = {}
const typeDefs = gql`
extend type Query {
property(id: Int!): [Property]
}
`
const resolvers = {
Query: {
property: getTheEntriesFromDB,
},
}
model.typeDefs = typeDefs;
model.resolvers = resolvers;
module.exports = model;
创建最终架构。有人可以指出我正确的方向吗?非常感谢任何帮助。提前非常感谢!