Skip to content

Commit

Permalink
Merge pull request #36 from graphql-dotnet/enums-errors-examples
Browse files Browse the repository at this point in the history
Version 1.2.1
  • Loading branch information
tlil authored Mar 27, 2017
2 parents 404660c + 3d2c0ba commit dda2b76
Show file tree
Hide file tree
Showing 29 changed files with 701 additions and 16 deletions.
11 changes: 11 additions & 0 deletions samples/SimpleWebApp/Data/AuthorDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GraphQL.Conventions.Tests.Server.Data
{
public class AuthorDto
{
public int Id { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }
}
}
16 changes: 16 additions & 0 deletions samples/SimpleWebApp/Data/BookDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Data
{
public class BookDto
{
public int Id { get; set; }

public string Title { get; set; }

public List<int> AuthorIds { get; set; }

public DateTime? ReleaseDate { get; set; }
}
}
86 changes: 86 additions & 0 deletions samples/SimpleWebApp/Data/Repositories/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Data.Repositories
{
public class AuthorRepository : IAuthorRepository
{
private readonly Dictionary<int, AuthorDto> _authors = new Dictionary<int, AuthorDto>
{
{
1,
new AuthorDto
{
Id = 1,
FirstName = "Benny",
LastName = "Frank",
}
},
{
2,
new AuthorDto
{
Id = 2,
FirstName = "Amalie",
LastName = "Jones",
}
},
{
3,
new AuthorDto
{
Id = 3,
FirstName = "Stewart",
LastName = "Clarksen",
}
},
{
4,
new AuthorDto
{
Id = 4,
FirstName = "Sony",
LastName = "Mahony",
}
},
};

private int _nextId = 1;

public int AddAuthor(AuthorDto author)
{
var id = _nextId++;
_authors[id] = author;
_authors[id].Id = id;
return id;
}

public AuthorDto GetAuthorById(int id)
{
AuthorDto author;
if (_authors.TryGetValue(id, out author))
{
return author;
}
return null;
}

public IEnumerable<AuthorDto> GetAuthorsByIds(List<int> ids)
{
foreach (var id in ids)
{
yield return GetAuthorById(id);
}
}

public IEnumerable<AuthorDto> SearchForAuthorsByLastName(string searchString)
{
foreach (var author in _authors.Values)
{
if (author.LastName?.ToLower()?.Contains(searchString?.ToLower()) ?? false)
{
yield return author;
}
}
}
}
}
91 changes: 91 additions & 0 deletions samples/SimpleWebApp/Data/Repositories/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Data.Repositories
{
public class BookRepository : IBookRepository
{
private readonly Dictionary<int, BookDto> _books = new Dictionary<int, BookDto>
{
{
1,
new BookDto
{
Id = 1,
Title = "A Lone Wolf in the Forest",
ReleaseDate = new DateTime(1985, 1, 1),
AuthorIds = new List<int> { 1, 2 }
}
},
{
2,
new BookDto
{
Id = 2,
Title = "Surfer Boy",
ReleaseDate = new DateTime(1998, 1, 1),
AuthorIds = new List<int> { 2 }
}
},
{
3,
new BookDto
{
Id = 3,
Title = "GraphQL, a Love Story",
ReleaseDate = new DateTime(2015, 1, 1),
AuthorIds = new List<int> { 4 }
}
},
{
4,
new BookDto
{
Id = 4,
Title = "Dare I Say What?",
ReleaseDate = new DateTime(2009, 1, 1),
AuthorIds = new List<int> { 3, 4 }
}
},
};

private int _nextId = 5;

public int AddBook(BookDto book)
{
var id = _nextId++;
_books[id] = book;
_books[id].Id = id;
return id;
}

public BookDto GetBookById(int id)
{
BookDto book;
if (_books.TryGetValue(id, out book))
{
return book;
}
return null;
}

public IEnumerable<BookDto> GetBooksByIds(List<int> ids)
{
foreach (var id in ids)
{
yield return GetBookById(id);
}
}

public IEnumerable<BookDto> SearchForBooksByTitle(string searchString)
{
foreach (var book in _books.Values)
{
if (book.Title?.ToLower()?.Contains(searchString?.ToLower()) ?? false)
{
yield return book;
}
}
}
}
}
15 changes: 15 additions & 0 deletions samples/SimpleWebApp/Data/Repositories/IAuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Data.Repositories
{
public interface IAuthorRepository
{
int AddAuthor(AuthorDto author);

AuthorDto GetAuthorById(int id);

IEnumerable<AuthorDto> GetAuthorsByIds(List<int> ids);

IEnumerable<AuthorDto> SearchForAuthorsByLastName(string searchString);
}
}
15 changes: 15 additions & 0 deletions samples/SimpleWebApp/Data/Repositories/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Data.Repositories
{
public interface IBookRepository
{
int AddBook(BookDto book);

BookDto GetBookById(int id);

IEnumerable<BookDto> GetBooksByIds(List<int> ids);

IEnumerable<BookDto> SearchForBooksByTitle(string searchString);
}
}
26 changes: 26 additions & 0 deletions samples/SimpleWebApp/DependencyInjector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Reflection;

namespace GraphQL.Conventions.Tests.Server
{
public class DependencyInjector : IDependencyInjector
{
private readonly Dictionary<TypeInfo, object> _registrations =
new Dictionary<TypeInfo, object>();

public void Register<TType>(TType instance)
{
_registrations.Add(typeof(TType).GetTypeInfo(), instance);
}

public object Resolve(TypeInfo typeInfo)
{
object instance;
if (_registrations.TryGetValue(typeInfo, out instance))
{
return instance;
}
return null;
}
}
}
21 changes: 21 additions & 0 deletions samples/SimpleWebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace GraphQL.Conventions.Tests.Server
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
13 changes: 13 additions & 0 deletions samples/SimpleWebApp/Schema/Input/AddAuthorParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GraphQL.Conventions.Relay;

namespace GraphQL.Conventions.Tests.Server.Schema.Input
{
[Description("Operation for adding a new author.")]
public class AddAuthorParams : IRelayMutationInputObject
{
public string ClientMutationId { get; set; }

[Description("The author to add.")]
public NonNull<AuthorInput> Author { get; set; }
}
}
13 changes: 13 additions & 0 deletions samples/SimpleWebApp/Schema/Input/AddBookParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GraphQL.Conventions.Relay;

namespace GraphQL.Conventions.Tests.Server.Schema.Input
{
[Description("Operation for adding a new book to the library.")]
public class AddBookParams : IRelayMutationInputObject
{
public string ClientMutationId { get; set; }

[Description("The book to add.")]
public NonNull<BookInput> Book { get; set; }
}
}
13 changes: 13 additions & 0 deletions samples/SimpleWebApp/Schema/Input/AuthorInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GraphQL.Conventions.Tests.Server.Schema.Input
{
[InputType]
[Description("An author")]
public class AuthorInput
{
[Description("The author's firstname.")]
public string FirstName { get; set; }

[Description("The author's lastname.")]
public NonNull<string> LastName { get; set; }
}
}
19 changes: 19 additions & 0 deletions samples/SimpleWebApp/Schema/Input/BookInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;

namespace GraphQL.Conventions.Tests.Server.Schema.Input
{
[InputType]
[Description("A book")]
public class BookInput
{
[Description("The book's title.")]
public NonNull<string> Title { get; set; }

[Description("The book's release date.")]
public DateTime? ReleaseDate { get; set; }

[Description("The IDs of the book's authors.")]
public List<Id> Authors { get; set; }
}
}
Loading

0 comments on commit dda2b76

Please sign in to comment.