簡體   English   中英

我怎樣才能讓`apollo server` 與 graphql schemaObject 一起工作?

[英]How can I make `apollo server` to work with graphql schemaObject?

我在 nodejs 項目中使用express-graphql來實現 graphql。 我對我的模式有以下定義。 不過,我最近切換到apollo ,但我不知道如何讓apollo接受graphql schemaObjectGraphQLObjectType

下面是我的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 構造函數接受可用於代替typeDefsresolversschema參數。 架構必須是GraphQLSchema的實例。

const schema = new GraphQLSchema({ ... })
const server = new ApolloServer({ schema })

請注意,如果您使用的是上傳標量,則必須手動將其添加到您的架構中(如果您提供typeDefs為您完成此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM