Graphql服务器端如何使用架构

时间:2019-07-23 12:34:00

标签: graphql apollo-server

我将apollo服务器用于graphql服务器。

我拥有的数据

const books = [
{
    id: 1,
    title: "Harry Potter and the Chamber of Secrets",
    author: 1
  },
  {
    id: 2,
    title: "Jurassic Park",
    author: 2
  }
];
const authors = [
  {
    id: 1,
    name: "J.K. Rowling",
    age: "26",
    bookId: 1
  },
  {
    id: 2,
    name: "Michael Crichton",
    age: "27",
    bookId: 1
  }
];

我定义的模式

const typeDefs = gql`
  type Book {
    title: String
    author: Author
  }
  type Author {
    name: String
    age: String
  }
  type Query {
    books(id: ID!): [Book]
    author: [Author]
  }
`;

我编写的解析器:

const resolvers = {
  Query: {
    books: (_, { id }) => books
    author: () => authors
  }
};

在GrahqlTool中进行的请求

query {
  books(id: 1){
    title
    author {
        name
        age
    }
  }
}

我回来的回复

{
  "data": {
    "books": []
  }
}

我期望的是

{
  "data": {
    "books": [
      {
        "title": "Harry Potter and the Chamber of Secrets",
        "author": {
           name: "J.K. Rowling",
           age: "26"
        }
      }
    ]
  }
}

如何为此方案定义架构?帮助我编写架构以实现上述结果。 (我已编辑问题,请重新考虑)

0 个答案:

没有答案