首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React/GraphQL:只能创建可为空的NonNull,但获得“类型”

React/GraphQL:只能创建可为空的NonNull,但获得“类型”
EN

Stack Overflow用户
提问于 2016-05-29 22:27:31
回答 1查看 1.3K关注 0票数 1

我正在编写我的第一个GraphQL模式。我以前从来没有这样做过,我对我所犯的错误感到困惑。例如:

代码语言:javascript
复制
  /Users/lorm/projects/aggregated_centralized_api/node_modules/graphql/jsutils/invariant.js:20
      throw new Error(message);
      ^

  Error: Can only create NonNull of a Nullable GraphQLType but got: function GraphQLList(type) {
      _classCallCheck(this, GraphQLList);

      (0, _jsutilsInvariant2['default'])(isType(type), 'Can only create List of a GraphQLType but got: ' + type + '.');
      this.ofType = type;
    }.
      at invariant (/Users/lorm/projects/aggregated_centralized_api/node_modules/graphql/jsutils/invariant.js:20:11)
      at new GraphQLNonNull (/Users/lorm/projects/aggregated_centralized_api/node_modules/graphql/type/definition.js:761:39)
      at Object.<anonymous> (/Users/lorm/projects/aggregated_centralized_api/server/schema.js:121:19)
      at Module._compile (module.js:413:34)
      at normalLoader (/Users/lorm/projects/aggregated_centralized_api/node_modules/babel-core/lib/api/register/node.js:199:5)
      at Object.require.extensions.(anonymous function) [as .js] (/Users/lorm/projects/aggregated_centralized_api/node_modules/babel-core/lib/api/register/node.js:216:7)
      at Module.load (module.js:357:32)
      at Function.Module._load (module.js:314:12)
      at Module.require (module.js:367:17)
      at require (internal/module.js:16:19)

我的schemaTypes.js文件如下所示:

代码语言:javascript
复制
  export const BookType = new GraphQLObjectType({
      name: 'Book',
      description: 'A Book',
      fields: () => ({
    book_id: {
        type: GraphQLString,
        description: 'UUID for book',
    },
    title: {
        type: GraphQLString,
        description: 'The name of the book',
    },
    author: {
        type: GraphQLList,
        description: 'List of authors, each author an object',
    },
    isbn: {
        type: GraphQLString,
        description: 'The primary way of identifying the book both domestically and internationally',
    },
    agency_price: {
        type: GraphQLFloat,
        description: 'The price set by the agency',
    },
    pub_date: {
        type: GraphQLString,
        description: 'The publication date of the book',
    },
    amazon_rank: {
        type: GraphQLInt,
        description: "The book's current selling rank on Amazon, updated daily",
    },
    reviews: {
        type: GraphQLFloat,
        description: "The book's current average review rating on Amazon, can 1 to 5, accurate to 1 decimal, updated daily",
    },
    book_img: {
        type: GraphQLString,
        description: 'Absolute URL for the image used for the book cover',
    },
    asin: {
        type: GraphQLString,
        description: "Amazon Standard Identification Numbers (ASINs) are unique blocks of 10 letters and/or numbers that identify items",
    },
    publisher: {
        type: GraphQLString,
        description: "The publisher name. There is only one publisher per book.",
    },
    bisacs: {
        type: GraphQLList,
        description: 'A list of Book Industry Standards and Communications subject headings as strings',
    },
    series_name: {
        type: GraphQLString,
        description: "If the book belongs to a series, the name of the series",
    },
    volume: {
        type: GraphQLString,
        description: "If the book is part of a series, this is its location in the sequence",
    },
    formats: {
        type: GraphQLList,
        description: 'Open Road has 20 standard formats',
    },
    keywords: {
        type: GraphQLList,
        description: 'Open Road puts mulitiple keywords (separated by a semi-colon) into the keyword1 field in Firebrand',
    },
    description: {
        type: GraphQLString,
        description: "Open Road's summary of the book",
    },
    campaigns: {
        type: GraphQLList,
        description: "A list full of marketing Campaigns we've done for the author",
    },
    retailers: {
        type: GraphQLList,
        description: 'A list full of Retailers, holding data specific to this book',
    },
    nominations: {
        type: GraphQLList,
        description: 'A list full of nominations to which this book belongs',
    },
    created: {
        type: GraphQLInt,
        description: 'The creation timestamp of this book'
    }
      })
  });

从schema.js的角度来看,部分原因是这样的:

代码语言:javascript
复制
  let schema = new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'RootQueryType',
      fields: {
        book: {
          type: new GraphQLList(BookType),
          resolve: () => ""
        },
        book_retailer_summary: {
          type: BookRetailerSummaryType,
          args: {
            name: {
              description: 'Summary of books sales information, via various retailers',
              type: new GraphQLNonNull(GraphQLString)
            }
          },
          resolve: (root, {name}) => {
            return mongo()
              .then(db => {
                let deferred = Q.defer();

                let collection = db.collection('users');
                collection.find({ name })
                  .toArray((err, docs) => {
                    if (err) {
                      deferred.reject(err);
                      return;
                    }

                    db.close();
                    deferred.resolve(docs.length ? docs[0] : null);
                  });

                return deferred.promise;
              });

          }
        }
      }
    }),

所以我很好奇我犯了什么错误?

[最新情况]

在回答helfer的评论时,我不得不怀疑GraphQLList的这种用法:

代码语言:javascript
复制
  nomination: {
    type: NominationType,
    args: {
      name: {
        description: 'The name of the list of books',
        type: new GraphQLNonNull(GraphQLString)
      },
      books: {
        description: 'The name of the books belonging to this nomination',
        type: new GraphQLNonNull(GraphQLList)
      }
    },

也许这条线是不允许的?

代码语言:javascript
复制
        type: new GraphQLNonNull(GraphQLList)

有什么方法可以让我得到更好的错误信息,从而缩小真正的问题是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-29 22:37:47

GraphQLListGraphQLNonNull是包装类型,这意味着必须按照以下方式创建它们:

代码语言:javascript
复制
const someListType = new GraphQLList(anotherType);
const someNonNullType = new GraphQLNonNull(anotherType);

您的图书类型有许多字段,其类型为GraphQLListType,而不实际创建实例(即不调用new,也不传递要包装的类型)。错误消息表明您试图在某个地方将GraphQLListType包装在NonNull中,但由于没有正确创建列表类型,它无法工作。

顺便说一句,在Javascript中,模式可能会变得很长而且很难阅读,这就是为什么我创建了一个包,允许您用模式语言编写它们,因此阅读起来容易得多:

代码语言:javascript
复制
type Book {
  book_id: String
  title: String
  author: [Author]
  # etc.
}

您可以在这里找到文档:http://docs.apollostack.com/apollo-server/generate-schema.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37515564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档