![](/img/trans.png)
[英]How can I debug a memory leak on my Apollo GraphQL server?
[英]How can I make `apollo server` to work with graphql schemaObject?
我在 nodejs 項目中使用express-graphql
來實現 graphql。 我對我的模式有以下定義。 不過,我最近切換到apollo
,但我不知道如何讓apollo
接受graphql schemaObject
像GraphQLObjectType
。
下面是我的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
})
經過一番搜索和調試,可以通過printSchema
來完成。 所以下面是代碼
const { printSchema } = require('graphql')
const server = new ApolloServer({
typeDefs: printSchema(graphqlSchema),
resolvers
})
如文檔中所示,ApolloServer 構造函數接受可用於代替typeDefs
和resolvers
的schema
參數。 架構必須是GraphQLSchema
的實例。
const schema = new GraphQLSchema({ ... })
const server = new ApolloServer({ schema })
請注意,如果您使用的是上傳標量,則必須手動將其添加到您的架構中(如果您提供typeDefs
為您完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.