如何在 apollo graphql 服务器中创建嵌套解析器
How to create a nested resolver in apollo graphql server
给定以下 apollo 服务器 graphql 模式
我想将它们分解成单独的模块,所以我不想在根查询模式下进行作者查询……并希望将其分开。所以我在将它添加到 Root Query
之前添加了另一个名为 authorQueries 的层
type Author {
id: Int,
firstName: String,
lastName: String
}
type authorQueries {
author(firstName: String, lastName: String): Author
}
type Query {
authorQueries: authorQueries
}
schema {
query: Query
}
我尝试了以下操作。您可以看到在指定 author 函数之前将 authorQueries 添加为另一层。
Query: {
authorQueries :{
author (root, args) {
return {}
}
}
}
在 Graphiql 中查询时,我还添加了额外的层..
{
authorQueries {
author(firstName: "Stephen") {
id
}
}
}
我收到以下错误。
"message": "Resolve function for \"Query.authorQueries\" returned undefined",
要创建 "nested" 解析器,只需在父字段的 return 类型上定义解析器。在这种情况下,您的 authorQueries
字段 return 类型为 authorQueries
,因此您可以将解析器放在那里:
{
Query: { authorQueries: () => ({}) },
authorQueries: {
author(root, args) {
return "Hello, world!";
}
}
}
所以在技术意义上,没有嵌套解析器这样的东西——每个对象类型都有一个简单的字段列表,并且这些字段有 return 类型。 GraphQL 查询的嵌套使结果嵌套。
我发现 returning 父字段上的函数 return 类型导致 this
arg 被绑定,并破坏解析器接口 b/c 嵌套解析器父项不是第一个参数。
对于内联类型定义
import {
graphql,
} from 'graphql';
import {
makeExecutableSchema, IResolverObject
} from 'graphql-tools';
const types = `
type Query {
person: User
}
type User {
id: ID
name: String,
dog(showCollar: Boolean): Dog
}
type Dog {
name: String
}
`;
const User: IResolverObject = {
dog(obj, args, ctx) {
console.log('Dog Arg 1', obj);
return {
name: 'doggy'
};
}
};
const resolvers = {
User,
Query: {
person(obj) {
console.log('Person Arg 1', obj);
return {
id: 'foo',
name: 'bar',
};
}
}
};
const schema = makeExecutableSchema({
typeDefs: [types],
resolvers
});
const query = `{
person {
name,
dog(showCollar: true) {
name
}
}
}`;
graphql(schema, query).then(result => {
console.log(JSON.stringify(result, null, 2));
});
// Person Arg 1 undefined
// Dog Arg 1 { id: 'foo', name: 'bar' }
// {
// "data": {
// "person": {
// "name": "bar",
// "dog": {
// "name": "doggy"
// }
// }
// }
// }
您也可以使用 addResolveFunctionsToSchema
,如下面的要点所示。
https://gist.github.com/blugavere/4060f4bf2f3d5b741c639977821a254f
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}`);
});
给定以下 apollo 服务器 graphql 模式 我想将它们分解成单独的模块,所以我不想在根查询模式下进行作者查询……并希望将其分开。所以我在将它添加到 Root Query
之前添加了另一个名为 authorQueries 的层type Author {
id: Int,
firstName: String,
lastName: String
}
type authorQueries {
author(firstName: String, lastName: String): Author
}
type Query {
authorQueries: authorQueries
}
schema {
query: Query
}
我尝试了以下操作。您可以看到在指定 author 函数之前将 authorQueries 添加为另一层。
Query: {
authorQueries :{
author (root, args) {
return {}
}
}
}
在 Graphiql 中查询时,我还添加了额外的层..
{
authorQueries {
author(firstName: "Stephen") {
id
}
}
}
我收到以下错误。
"message": "Resolve function for \"Query.authorQueries\" returned undefined",
要创建 "nested" 解析器,只需在父字段的 return 类型上定义解析器。在这种情况下,您的 authorQueries
字段 return 类型为 authorQueries
,因此您可以将解析器放在那里:
{
Query: { authorQueries: () => ({}) },
authorQueries: {
author(root, args) {
return "Hello, world!";
}
}
}
所以在技术意义上,没有嵌套解析器这样的东西——每个对象类型都有一个简单的字段列表,并且这些字段有 return 类型。 GraphQL 查询的嵌套使结果嵌套。
我发现 returning 父字段上的函数 return 类型导致 this
arg 被绑定,并破坏解析器接口 b/c 嵌套解析器父项不是第一个参数。
对于内联类型定义
import {
graphql,
} from 'graphql';
import {
makeExecutableSchema, IResolverObject
} from 'graphql-tools';
const types = `
type Query {
person: User
}
type User {
id: ID
name: String,
dog(showCollar: Boolean): Dog
}
type Dog {
name: String
}
`;
const User: IResolverObject = {
dog(obj, args, ctx) {
console.log('Dog Arg 1', obj);
return {
name: 'doggy'
};
}
};
const resolvers = {
User,
Query: {
person(obj) {
console.log('Person Arg 1', obj);
return {
id: 'foo',
name: 'bar',
};
}
}
};
const schema = makeExecutableSchema({
typeDefs: [types],
resolvers
});
const query = `{
person {
name,
dog(showCollar: true) {
name
}
}
}`;
graphql(schema, query).then(result => {
console.log(JSON.stringify(result, null, 2));
});
// Person Arg 1 undefined
// Dog Arg 1 { id: 'foo', name: 'bar' }
// {
// "data": {
// "person": {
// "name": "bar",
// "dog": {
// "name": "doggy"
// }
// }
// }
// }
您也可以使用 addResolveFunctionsToSchema
,如下面的要点所示。
https://gist.github.com/blugavere/4060f4bf2f3d5b741c639977821a254f
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}`);
});