【发布时间】:2020-03-27 15:00:22
【问题描述】:
我在 nodejs 项目中使用 express-graphql 来实现 graphql。我的架构有以下定义。但是,我最近切换到apollo,但我不知道如何让apollo 像GraphQLObjectType 一样接受graphql schemaObject。
下面是我的schemaObject。
var graphql = require('graphql');
var fakeDatabase = {
'a': {
id: 'a',
name: 'alice',
},
'b': {
id: 'b',
name: 'bob',
},
};
// Define the User type
var userType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLString },
name: { type: GraphQLString },
}
});
// Define the Query type
var queryType = new GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: userType,
// `args` describes the arguments that the `user` query accepts
args: {
id: { type: GraphQLString }
},
resolve: (_, {id}) => {
return fakeDatabase[id];
}
}
}
});
const graphqlSchema = new GraphQLSchema({ query: queryType });
在apollo 中,我的这段代码不起作用。可能是因为我不能在typeDefs 上使用graphqlSchema 来代替ApolloServer。我想知道我怎样才能让它工作。我需要将其转换为其他格式吗?我真的不想更改我的架构对象。
const server = new ApolloServer({
typeDefs: graphqlSchema,
resolvers
})
【问题讨论】: