|
1 | 1 | # Error Handling
|
2 | 2 |
|
3 |
| -TODO |
| 3 | +The `ExecutionResult` provides an `Errors` property which includes any errors encountered during exectution. Errors are returned [according to the spec](http://facebook.github.io/graphql/#sec-Errors), which means stack traces are excluded. The `ExecutionResult` is transformed to what the spec requires using JSON.NET. You can change what information is provided by overriding the JSON Converter. |
| 4 | + |
| 5 | +You can provide additional error handling or logging for fields by adding Field Middleware. |
4 | 6 |
|
5 | 7 | # User Context
|
6 | 8 |
|
7 |
| -TODO |
| 9 | +You can provide a `UserContext` to provide access to your specific data. The `UserContext` is accessible in field resolvers and validation rules. |
| 10 | + |
| 11 | +```csharp |
| 12 | +public class GraphQLUserContext |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +var result = await _executer.ExecuteAsync(_ => |
| 17 | +{ |
| 18 | + _.UserContext = new GraphQLUserContext(); |
| 19 | +}).ConfigureAwait(false); |
| 20 | + |
| 21 | +Field<ListGraphType<DinnerType>>( |
| 22 | + "popularDinners", |
| 23 | + resolve: context => |
| 24 | + { |
| 25 | + var userContext = context.UserContext.As<GraphQLUserContext>(); |
| 26 | + }); |
| 27 | +``` |
8 | 28 |
|
9 | 29 | # Dependency Injection
|
10 | 30 |
|
11 |
| -TODO |
| 31 | +GraphQL .NET supports dependency injection through a simple resolve function on the Schema class. Internally when trying to resolve a type the library will call this resolve function. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +The default implementation uses `Activator.CreateInstance`. |
| 36 | + |
| 37 | +```csharp |
| 38 | +type => (GraphType) Activator.CreateInstance(type) |
| 39 | +``` |
| 40 | + |
| 41 | +How you integrate this into your system will depend on the dependency injection framework you are using. Registering your schema with a resolve function that accesses your container may look something like this: |
| 42 | + |
| 43 | +```csharp |
| 44 | +// Nancy TinyIoCContainer |
| 45 | +container.Register((c, overloads) => |
| 46 | +{ |
| 47 | + return new NerdDinnerSchema(type => c.Resolve(type) as IGraphType); |
| 48 | +}); |
| 49 | + |
| 50 | +// SimpleContainer |
| 51 | +var container = new SimpleContainer(); |
| 52 | +container.Singleton(new StarWarsSchema(type => container.Get(type) as IGraphType)); |
| 53 | +``` |
| 54 | + |
| 55 | +[The GraphiQL sample application uses Dependency Injection.](https://github.com/graphql-dotnet/graphql-dotnet/blob/master/src/GraphQL.GraphiQL/Bootstrapper.cs) |
| 56 | + |
| 57 | +```csharp |
| 58 | +public class NerdDinnerSchema : GraphQL.Types.Schema |
| 59 | +{ |
| 60 | + public NerdDinnerSchema(Func<Type, IGraphType> resolve) |
| 61 | + : base(resolve) |
| 62 | + { |
| 63 | + Query = (IObjectGraphType)resolve(typeof(Query)); |
| 64 | + Mutation = (IObjectGraphType)resolve(typeof(Mutation)); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
12 | 68 |
|
13 | 69 | # Object/Field Metadata
|
14 | 70 |
|
@@ -241,4 +297,6 @@ var report = StatsReport.From(schema, result.Operation, result.Perf, start);
|
241 | 297 |
|
242 | 298 | # Relay
|
243 | 299 |
|
244 |
| -TODO |
| 300 | +The core project provides a few classes to help with Relay. You can find more types and helpers [here](https://github.com/graphql-dotnet/relay). |
| 301 | + |
| 302 | +(Example needed) |
0 commit comments