1.创建项目
2.对类库操作,在这里建立三个文件夹,其中一个文件夹改名为schema
3.建立IMovieService,IActorService接口与实现类
GraphQL是什么将放到第二章来讲,第一章为GraphQL的准备工程
1.创建项目
之后选择空项目,.net core 2.1
添加类库,取名
2.对类库操作,在这里建立三个文件夹,其中一个文件夹改名为schema
Movie:用于确定movie类与actor类得类型
Schema:
Service:用来写服务的
安装GraphQL包,加入类库中
在Movies文件夹中建立Movie类,Actor类与枚举MovieRating,MovieRating为Movie中其中一个的枚举
Movie
using GraphStudy.Movies.Movies;
using System;
namespace GraphStudy.Movie.Movies
{
//电影类
public class Movie
{
public int Id { get; set; }//电影编号
public string Name { get; set; }
public DateTime ReleaseDate { get; set; }//上映时间
public string Company { get; set; }
public int ActorId { get; set; }//演员ID
public MovieRating MovieRating { get; set; }//电影等级枚举
}
}
枚举MovieRating
using System;
namespace GraphStudy.Movies.Movies
{
[Flags]
public enum MovieRating
{
//这是电影的5个等级
Unrated = 0,
G = 1,
PG = 2,
PG13 = 3,
R = 4,
NC17 = 5
}
}
Actor演员类,写简单点
namespace GraphStudy.Movies.Movies
{
//演员类
public class Actor
{
public int Id { get; set; }
public string Name { get; set; }
}
}
**
3.建立IMovieService,IActorService接口与实现类
**
在Services文件夹添加IMovieService,IActorService接口与MovieService,ActorServices实现类
IMovieService接口
using GraphStudy.Movies.Movies;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GraphStudy.Movies.Services
{
public interface IMovieService
{
Task<IEnumerable<Movie>> GetAsyncs();//获取所有列
Task<Movie> GetByIdAsync(int id);//获取某个对应id属性的列
Task<Movie> CreateAsync(Movie movie);//创建
}
}
IActorService接口
using GraphStudy.Movies.Movies;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GraphStudy.Movies.Services
{
public interface IActorService
{
Task<Actor> GetByIdAsync(int id);//获取某个演员信息
Task<IEnumerable<Actor>> GetAsync();//获取所有演员信息
}
}
IMovieService接口的实现类MovieService类
using GraphStudy.Movies.Movies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GraphStudy.Movies.Services
{
public class MovieService : IMovieService
{
//因为我们需要创建子列表,所以一要用到IList
private readonly IList<Movie> _movie;
public MovieService()
{
_movie=new List<Movie>
{
#region 电影列表
new Movie
{
Id = 1,
Name = "肖申克的救赎The Shawshank Redemption",
Company = "美国",
MovieRating = MovieRating.G,
ActorId = 1,
ReleaseDate = new DateTime(1994-10-14)
},
new Movie
{
Id = 2,
Name = "这个杀手不太冷 Léon ",
Company = "法国",
MovieRating = MovieRating.NC17,
ActorId = 2,
ReleaseDate = new DateTime(1994-09-14)
},
new Movie
{
Id = 3,
Name = "三傻大闹好莱坞",
Company = "印度",
MovieRating = MovieRating.PG,
ActorId = 3,
ReleaseDate = new DateTime(2011-12-08)
},
new Movie
{
Id = 4,
Name = "功夫",
Company = "美国",
MovieRating = MovieRating.G,
ActorId = 4,
ReleaseDate = new DateTime(2004-12-23)
}
#endregion
};
}
public Task<Movie> CreateAsync(Movie movie)
{
_movie.Add(movie);
return Task.FromResult(movie);
}
public Task<IEnumerable<Movie>> GetAsyncs()
{
return Task.FromResult(_movie.AsEnumerable());
}
public Task<Movie> GetByIdAsync(int id)
{
//在这里需要做个判断这个id是否存在
var movie = _movie.SingleOrDefault(x => x.Id == id);
if (movie == null)
{
throw new ArgumentException(String.Format("Movie ID {0} 不正确", id););
}
return Task.FromResult(movie);
}
}
}
IActorService接口的实现,ActorService类
using GraphStudy.Movies.Movies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GraphStudy.Movies.Services
{
public class ActorService : IActorService
{
private readonly IList<Actor> _actor;
public ActorService()
{
_actor=new List<Actor>
{
#region 演员列表
new Actor
{
Id = 1,
Name = "肖申克"
},
new Actor
{
Id=2,
Name = "让·雷诺"
},
new Actor
{
Id = 3,
Name = "阿米尔·汗,卡琳娜·卡普尔 ",
},
new Actor
{
Id=4,
Name = "周星驰,元秋 元华"
}
#endregion
};
}
public Task<IEnumerable<Actor>> GetAsync()
{
return Task.FromResult(_actor.AsEnumerable());
}
public Task<Actor> GetByIdAsync(int id)
{
var actor = _actor.SingleOrDefault(x => x.Id == id);
if (actor == null)
{
throw new ArgumentException(String.Format("Actor ID {0} 不存在",id));
}
return Task.FromResult(actor);
}
}
}
此处注意:Movie与Actor中的ID为一对一,所以ActorService里面new Actor中的id只可唯一,不可以重复,会出错
4.在startup注册服务
添加引用
如果出现以下情况
将类库的版本降为.net core 2.1
右键点击类库->属性
如果无效,请把引用类库取消在引用一次类库
然后进行服务注册
这是第一部分