为什么当我称其为父级时,该GraphQL属性没有按预期返回?

时间:2019-03-18 22:00:24

标签: graphql apollo

因此,我正在尝试构建一个应用程序以迭代存储在Google Firestore中的书签。我正在通过Apollo服务器访问Firestore。

这是我的解析器和类型定义。

import { gql } from 'apollo-server-express';
import * as admin from 'firebase-admin';

const serviceAccount = require('./service-account.json')
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

// Locations of bookmarks in database
const bookmarks = admin.firestore().collection("bookmarks");

export const typeDefs = gql`
type Bookmark {
    name: String
    url: String
    preview: Preview
}

type Preview {
    bookmark: Bookmark
    description: String
    image: String
}

type Query {
    bookmarks: [Bookmark]
    hello: String
}

schema {
    query: Query
}
`;

export const resolvers = {
    Query: {
        hello(obj, args, context, info) {
            return 'Hello world!'
        },
        async bookmarks() {
            const firebaseList= await bookmarks.get();
            return firebaseList.docs.map(bookmark => bookmark.data());
        }
    },

    Preview: {
        description: () => "this is a description",
    }
};

当我发送此查询时:

{bookmarks{
  name
  url
  preview {
    description
  }
}}

我希望它返回预览的描述(很明显),但是我得到了

{
  "data": {
    "bookmarks": [
      {
        "name": "Google",
        "url": "https://google.com",
        "preview": null
      },
      {
        "name": "Facebook",
        "url": "https://facebook.com",
        "preview": null
      },
      {
        "name": "Instagram",
        "url": "https://instagram.com",
        "preview": null
      },
      {
        "name": "Pinterest",
        "url": "https://pinterest.com",
        "preview": null
      }
    ]
  }
}

我对GraphQL的工作方式有什么误解?

0 个答案:

没有答案