0

我正在构建一个 Apollo 服务器。我有一个与 Mongo 通信的简单端点。有一组公告。

export const typeDefs = gql`
  type Query {
    announcements: [Announcement]
    announcementsByAuthor(author: String!): [Announcement]
  }

  type Announcement {
    _id: ID!
    msg: String!
    author: String!
    title: String
  }
`;

export const resolvers = {
  Query: {
    announcements: () => {
      return new AnnouncementController().getAnnouncements();
    },
    announcementsByAuthor: (author: string) => {
      console.log('RESOLVER: ', author);
      return new AnnouncementController().getAnnouncementsByAuthor(author);
    }
  },
}

在我的 graphiql 界面中,公告查询正常工作:

{
  announcements {
    msg
    author
    }
}

AdvertisementsByAuthor 查询似乎不接受来自变量或硬编码到查询中的字符串参数。

query($author: String!){
  announcementsByAuthor(author: $author) {
    msg
    author
  }
}

变量:

{
  "author":"Nate"
}

我已经从解析器中注销,并且传入的是一个空字符串,而不是作者变量的指定值。我是graphql的新手,我希望有人能告诉我我确信这是一个简单的疏忽。

4

1 回答 1

1

试试这个:

announcementsByAuthor: (doc, {author}) => {

于 2018-07-12T15:50:47.380 回答