如何在apollo graphql server中创建嵌套解析器

elb*_*rto 14 graphql apollo-server apollostack

鉴于以下apollo服务器graphql架构,我想将它们分解为单独的模块,因此我不希望根查询架构下的作者查询...并希望它分开.所以我在添加到根查询之前添加了另一个名为authorQueries的层

type Author {
    id: Int,
    firstName: String,
    lastName: String
}  
type authorQueries {
    author(firstName: String, lastName: String): Author
}

type Query {
    authorQueries: authorQueries
}

schema {
    query: Query
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下内容..您可以看到在指定作者函数之前,authorQueries已添加为另一个图层.

Query: {
    authorQueries :{
        author (root, args) {
            return {}
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Graphiql中查询时,我还添加了额外的图层..

{
    authorQueries {
        author(firstName: "Stephen") {
            id
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

"message": "Resolve function for \"Query.authorQueries\" returned undefined",

stu*_*ilo 18

要创建"嵌套"解析器,只需在父字段的返回类型上定义解析器.在这种情况下,您的authorQueries字段会返回类型authorQueries,因此您可以将解析器放在那里:

{
  Query: { authorQueries: () => ({}) },
  authorQueries: {
    author(root, args) {
      return "Hello, world!";
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

因此在技术意义上,没有嵌套解析器这样的东西 - 每个对象类型都有一个平面的字段列表,并且这些字段具有返回类型.GraphQL查询的嵌套是嵌套结果的原因.


Ezr*_*ton 5

Query.libraries()>>>Library.books()Book.author()Author.name()

Apollo 官方相关文档(里面有很好的例子):

旋转变压器链

https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-chains

/* code from:
https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-chains
*/

const { ApolloServer, gql } = require('apollo-server');

const libraries = [
  {
    branch: 'downtown'
  },
  {
    branch: 'riverside'
  },
];

// The branch field of a book indicates which library has it in stock
const books = [
  {
    title: 'The Awakening',
    author: 'Kate Chopin',
    branch: 'riverside'
  },
  {
    title: 'City of Glass',
    author: 'Paul Auster',
    branch: 'downtown'
  },
];

// Schema definition
const typeDefs = gql`

# A library has a branch and books
  type Library {
    branch: String!
    books: [Book!]
  }

  # A book has a title and author
  type Book {
    title: String!
    author: Author!
  }

  # An author has a name
  type Author {
    name: String!
  }

  # Queries can fetch a list of libraries
  type Query {
    libraries: [Library]
  }
`;

// Resolver map
const resolvers = {
  Query: {
    libraries() {

      // Return our hardcoded array of libraries
      return libraries;
    }
  },
  Library: {
    books(parent) {

      // Filter the hardcoded array of books to only include
      // books that are located at the correct branch
      return books.filter(book => book.branch === parent.branch);
    }
  },
  Book: {

    // The parent resolver (Library.books) returns an object with the
    // author's name in the "author" field. Return a JSON object containing
    // the name, because this field expects an object.
    author(parent) {
      return {
        name: parent.author
      };
    }
  }

  // Because Book.author returns an object with a "name" field,
  // Apollo Server's default resolver for Author.name will work.
  // We don't need to define one.
};

// Pass schema definition and resolvers to the
// ApolloServer constructor
const server = new ApolloServer({ typeDefs, resolvers });

// Launch the server
server.listen().then(({ url }) => {
  console.log(`  Server ready at ${url}`);
});
Run Code Online (Sandbox Code Playgroud)