Skip to content

Commit

Permalink
Added AddDocumentFromFile (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Aug 8, 2019
1 parent 79e2d5e commit cbe8298
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
@@ -1,8 +1,4 @@
using System.Threading.Tasks;
using System;
using HotChocolate.Execution;
using HotChocolate.Resolvers;
using Snapshooter.Xunit;
using Xunit;
using HotChocolate.Types;

Expand Down
@@ -0,0 +1,74 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Snapshooter.Xunit;
using HotChocolate.Types;
using IOPath = System.IO.Path;

namespace HotChocolate
{
public class SchemaBuilderExtensionsDocumentTests
{
[Fact]
public void AddDocumentFromFile_Builder_Is_Null()
{
// arrange
// act
Action action = () =>
SchemaBuilderExtensions.AddDocumentFromFile(null, "abc");

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddDocumentFromFile_File_Is_Null()
{
// arrange
var builder = SchemaBuilder.New();

// act
Action action = () =>
SchemaBuilderExtensions.AddDocumentFromFile(builder, null);

// assert
Assert.Throws<ArgumentException>(action);
}

[Fact]
public void AddDocumentFromFile_File_Is_Empty()
{
// arrange
var builder = SchemaBuilder.New();

// act
Action action = () =>
SchemaBuilderExtensions.AddDocumentFromFile(
builder, string.Empty);

// assert
Assert.Throws<ArgumentException>(action);
}

[Fact]
public async Task AddDocumentFromFile()
{
// arrange
var builder = SchemaBuilder.New();
string file = IOPath.GetTempFileName();
await File.WriteAllTextAsync(file, "type Query { a: String }");

// act
SchemaBuilderExtensions.AddDocumentFromFile(builder, file);

// assert
ISchema schema = builder
.Use(next => context => next.Invoke(context))
.Create();

schema.ToString().MatchSnapshot();
}
}
}

@@ -1,9 +1,9 @@
using System.Threading.Tasks;
using System;
using HotChocolate.Execution;
using HotChocolate.Resolvers;
using System.Threading.Tasks;
using Snapshooter.Xunit;
using Xunit;
using HotChocolate.Execution;
using HotChocolate.Resolvers;

namespace HotChocolate
{
Expand Down
@@ -1,7 +1,7 @@
using System;
using HotChocolate.Types;
using Snapshooter.Xunit;
using Xunit;
using HotChocolate.Types;

namespace HotChocolate
{
Expand Down
@@ -0,0 +1,10 @@
schema {
query: Query
}

type Query {
a: String
}

"The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text."
scalar String
2 changes: 0 additions & 2 deletions src/Core/Types/Extensions/SchemaBuilderExtensions.Bindings.cs
@@ -1,7 +1,5 @@
using System;
using HotChocolate.Configuration.Bindings;
using HotChocolate.Language;
using HotChocolate.Properties;
using HotChocolate.Types;

namespace HotChocolate
Expand Down
25 changes: 23 additions & 2 deletions src/Core/Types/Extensions/SchemaBuilderExtensions.Document.cs
@@ -1,4 +1,5 @@
using System;
using System.IO;
using HotChocolate.Language;
using HotChocolate.Properties;

Expand All @@ -22,8 +23,28 @@ public static partial class SchemaBuilderExtensions
nameof(schema));
}

DocumentNode document = Utf8GraphQLParser.Parse(schema);
return builder.AddDocument(sp => document);
return builder.AddDocument(sp => Utf8GraphQLParser.Parse(schema));
}

public static ISchemaBuilder AddDocumentFromFile(
this ISchemaBuilder builder,
string filePath)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentException(
"",
nameof(filePath));
}

return builder.AddDocument(sp =>
Utf8GraphQLParser.Parse(
File.ReadAllBytes(filePath)));
}

public static ISchemaBuilder AddDocument(
Expand Down

0 comments on commit cbe8298

Please sign in to comment.