从graphQL解析器中的API提取数据并将响应设置为指定字段

时间:2018-07-16 19:08:31

标签: javascript ajax graphql mongoose-schema

我正在尝试向movie db发出请求,要求为每部电影拉入海报图像。我试图弄清楚是否应该在运行addMovie Mutation时执行此操作,或者应该在电影查询中设置它。如果添加到突变中,诺言能否在继续并保存到db之前完成解析?另外,将其设置为发帖人字段时,会是什么样?

MovieType类型如下所示:

const MovieType = new GraphQLObjectType({
  name: 'Movie',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    genre: { type: GraphQLString },
    poster: { type: GraphQLString },
    director: {
      type: DirectorType,
      resolve(parent, args) {

      // HELP ### Make request to movie db using 'name' field to search movie and assign image url to 'poster' field.           

        // look at Director collection for records matching directorId
        return Director.findById(parent.directorId);
      }
    }
  })
});

根查询:

name: 'RootQueryType',
  fields: {
    movie: {
      type: MovieType,
      // args define which additional parameters are expected in the query // if I query a movie I need to pass the 'id'
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {

       // ### Make request here?

        return Movie.findById(args.id);
      }
    },

静音addMovie:

addMovie: {
      type: MovieType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        genre: { type: new GraphQLNonNull(GraphQLString) },
        directorId: { type: new GraphQLNonNull(GraphQLID) }
      },
      resolve(parent, args) {
        let movie = new Movie({
          name: args.name,
          genre: args.genre,
          directorId: args.directorId
        });
        return movie.save();
      }
    }

graphQL的新手。对此表示感谢。

0 个答案:

没有答案