如何使 `apollo server` 与 graphql schemaObject 一起工作?
How can I make `apollo server` to work with graphql schemaObject?
我在 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
上为 ApolloServer
使用 graphqlSchema
。我想知道如何让它发挥作用。我需要将其转换为其他格式吗?我真的不想更改我的架构对象。
const server = new ApolloServer({
typeDefs: graphqlSchema,
resolvers
})
经过一番查找和调试,可以printSchema
完成。下面是代码
const { printSchema } = require('graphql')
const server = new ApolloServer({
typeDefs: printSchema(graphqlSchema),
resolvers
})
如 docs 所示,ApolloServer 构造函数接受一个 schema
参数,可以用来代替 typeDefs
和 resolvers
。架构必须是 GraphQLSchema
.
的实例
const schema = new GraphQLSchema({ ... })
const server = new ApolloServer({ schema })
请注意,如果您使用的是上传标量,则必须手动将其添加到您的架构中(如果您改为提供 typeDefs
,则会为您完成此操作。
我在 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
上为 ApolloServer
使用 graphqlSchema
。我想知道如何让它发挥作用。我需要将其转换为其他格式吗?我真的不想更改我的架构对象。
const server = new ApolloServer({
typeDefs: graphqlSchema,
resolvers
})
经过一番查找和调试,可以printSchema
完成。下面是代码
const { printSchema } = require('graphql')
const server = new ApolloServer({
typeDefs: printSchema(graphqlSchema),
resolvers
})
如 docs 所示,ApolloServer 构造函数接受一个 schema
参数,可以用来代替 typeDefs
和 resolvers
。架构必须是 GraphQLSchema
.
const schema = new GraphQLSchema({ ... })
const server = new ApolloServer({ schema })
请注意,如果您使用的是上传标量,则必须手动将其添加到您的架构中(如果您改为提供 typeDefs
,则会为您完成此操作。