无法在GraphQL中返回关系数据

时间:2018-05-24 12:32:54

标签: javascript graphql graphql-js

我正在创建Reddit克隆,我首先设置后端,但无法创建关系数据。

当我使用此查询时:

query {
  subreddit(id: 1) {
    name
    posts {
      title
    }
  }
}

我期待:

{
  "data": {
    "subreddit": {
      "name": "javascript"
      "posts": [
        {
          "title": "JS Post"
        }
      ]
    }
  }
}

我得到了什么:

{
  "data": null,
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Subreddit.posts.",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "subreddit",
        "posts"
      ]
    }
  ]
}

这是架构:

type Query {
  subreddits: [Subreddit!]!
  subreddit(id: ID!): Subreddit!
  posts: [Post!]!
  post(id: ID!): Post!
}

type Mutation {
  createSubreddit(
    name: String!
    description: String!
    contentType: String!
    ageRestriction: Boolean!
  ): Subreddit!
}

type Subreddit {
  id: ID!
  name: String!
  description: String!
  contentType: String!
  ageRestriction: Boolean!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  body: String!
  subredditId: ID!
  # userId: ID!
}

以下是server/index.js

const { GraphQLServer } = require('graphql-yoga');

let dummySubreddits = [
  {
    name: 'javascript',
    description: 'all things javascript',
    contentType: 'any',
    ageRestriction: false,
    id: 1
  },
  {
    name: 'react',
    description: 'all things react',
    contentType: 'any',
    ageRestriction: false,
    id: 2
  },
  {
    name: 'primsa',
    description: 'all things prisma',
    contentType: 'any',
    ageRestriction: false,
    id: 3
  }
];
let idCountSubreddit = dummySubreddits.length;

let dummyPosts = [
  { title: 'JS Post', body: 'Body of post one', id: 1, subredditId: 1 },
  { title: 'React Post', body: 'Body of post two', id: 2, subredditId: 2 },
  {
    title: 'Prisma Post',
    body: 'Body of post three',
    id: 3,
    subredditId: 3
  }
];
let idCountPost = dummyPosts.length;

const resolvers = {
  Query: {
    subreddits: () => dummySubreddits,
    subreddit: (parent, args) => {
      return dummySubreddits.find(obj => obj.id == args.id);
    },
    posts: () => (parent, args) => {
      return dummyPosts.find(obj => obj.subredditId == parent.id);
    },
    post: (parent, args) => {
      return dummyPosts.find(obj => obj.id == args.id);
    }
  },
  Mutation: {
    createSubreddit: (parent, args) => {
      let subreddit = {
        id: idCountSubreddit++,
        name: args.name,
        description: args.description,
        contentType: args.contentType,
        ageRestriction: args.ageRestriction
      };
      return subreddit;
    }
  }
};

const server = new GraphQLServer({ typeDefs: './schema.graphql', resolvers });
server.start(() => console.log('Server is running on localhost:4000'));

我正在使用GraphQL桌面应用程序进行查询,但我没有grapql-yoga配置文件。

我哪里错了?我想指出正确的方向,所以我可以自己弄清楚。这是我第一次单独使用GraphQL,在YouTube上做了一些教程,然而他们使用了graphql-express而我正在使用graphql-yoga

2 个答案:

答案 0 :(得分:1)

将您为Query posts撰写的解析器移至Subreddit以解决其中的帖子字段。如果您的解析器不符合默认的解析器实现:

(parent) => parent[fieldName]

就像你的情况一样

(parent) => parent.posts

您必须自己指定。如果posts上的字段Query应显示您可能希望用于以下实施的所有帖子:

const resolvers = {
  Query: {
    subreddits: () => dummySubreddits,
    subreddit: (parent, args) => {
      return dummySubreddits.find(obj => obj.id == args.id);
    },
    posts: () => dummyPosts,
    post: (parent, args) => {
      return dummyPosts.find(obj => obj.id == args.id);
    }
  },
  Subreddit: {
    posts: () => (parent, args) =>
      dummyPosts.filter(obj => obj.subredditId == parent.id),
  },
  Mutation: {
    createSubreddit: (parent, args) => {
      let subreddit = {
        id: idCountSubreddit++,
        name: args.name,
        description: args.description,
        contentType: args.contentType,
        ageRestriction: args.ageRestriction
      };
      return subreddit;
    }
  }
};

答案 1 :(得分:0)

我必须为subreddit添加一个解析器来处理posts

const resolvers = {
  Query: {
    subreddits: () => dummySubreddits,
    subreddit: (parent, args) => {
      return dummySubreddits.find(obj => obj.id == args.id);
    },
    posts: (parent, args) => {
      return dummyPosts;
    },
    post: (parent, args) => {
      return dummyPosts.find(obj => obj.id == args.id);
    }
  },
  Mutation: {
    createSubreddit: (parent, args) => {
      let subreddit = {
        id: idCountSubreddit++,
        name: args.name,
        description: args.description,
        contentType: args.contentType,
        ageRestriction: args.ageRestriction
      };
      return subreddit;
    }
  },

  // This resolver was needed
  Subreddit: {
    posts: subreddit =>
      dummyPosts.filter(obj => obj.subredditId == subreddit.id)
  }
};