Join Glenn Block for an in-depth discussion in this video Configure the schema, part of API Development in .NET with GraphQL.
- [Instructor] Now we'll go create our OrderCreateInputType using those classes that I just showed you. So, what we're going to do is we're going to Right Click on Schema, we're going to add a new item, and we will call it OrderCreateInputType. Now we'll add a using statement for GraphQL.Types. We'll make our class public.
And now we're going to have it derive from a new type, which we mentioned in the slides, called InputObjectGraphType. And this is going to tell GraphQL.NET that this is an input type. So, we'll create a constructor, we'll set the name to OrderInput, and we'll start to create our fields. Now, you're going to see some of those other types that we just learned about, like NonNullGraphType. So, we'll specify our name, description, customerId, and the created date.
Okay, well, now we'll move over and create our mutation. And remember, we have two different mutations that we're going to support. One that will allow creating the order, and the other that will allow starting the order. So, we're going to Right Click on Schema, Add, create a new item again, and we will call that Orders Mutation. Once again we'll bring in some namespaces, which you can see I'm doing on line one. So, we'll bring in GraphQL, we'll bring in GraphQL.Types, and we're also going to need access to our order service, so we're basically going to import our Services namespace as well.
Going to make our OrdersMutation class public. And like what we did with the query, we're going to derive from ObjectGraphType of object. We'll give it a constructor, and we'll give it a name of Mutation, and then we'll start defining our fields. We'll define our first field which is going to be of OrderType. There's going to be the createOrder mutation.
So, it is createOrder. Now, one of the things we need to do now is deal with arguments. We haven't dealt with arguments up until this point. When the client creates the order, they're going to have to pass me an instance of an OrderType, and this will be a variable that they will provide in GraphQL. So, to access this, GraphQL.NET gives us this arguments collection, and what we're going to do is instantiate a new QueryArgument, which is a NonNullGraphType of OrderCreate input type, which is the type that we just defined for when we create our order.
And now I'm going to give it a name. Oh, I'm missing a parenthesis, so let me fix that. Just missing a closing parenthesis here, I'll just go up and fix that on line 18. And now I have my resolve method. Now, in my resolve method, what I'm going to do is I'm going to get access to that orderInput that was provided.
And I'm going to do that using this GetArgument of T function, and I'm going to pass in the orderCreate input as the model. And the parameter that I'm going to try to get is called order. Next I'm going to create a new id, because whenever a create an order, I have to pass in an id, so I'm going to use this NewGuid function which will create for me a Guid. Okay, looks like I have a slight error there on line 21 just because I'm missing a using statement.
Okay, that's fixed. And now I can instantiate my order. And I can use the orderInput and pass the parameters in. So, I'm going to get the Name, I'm going to get the Description, Created, CustomerId, and finally, I'll pass in the id, the Guid that I just created for the new order.
Okay, so it looks like I cannot access the orders service, and the reason for that is because I didn't actually inject it on the constructor. So, let's go do that right now, that should be very easy to do. I'm just going to go up here on line 13 and I'm going to say give me the orders service. Now I have the orders service. And now I can call orders.CreateAsync, and I can pass in my order.
Okay, let's go implement our next mutation. Before we do that, it's looking like there's a lot of code here. It's getting kind of hard to read, so I'm just going to autohide the Solution Explorer before we continue. Okay, so now, for this next mutation we're actually going to use FieldAsync instead of using the Field function as we did before, and I'll show you why in a minute. So, we'll call this startOrder.
And we're going to need our arguments again. And in this case, because of the fact that we're just accepting an argument, a simple argument of a String, we can just new up our arguments collection and pass in a new QueryArgument NonNullGraphType of StringGraphType. And we'll say that the name of the argument is orderId. And then we will add our resolve function.
And because of the fact that we're using FieldAsync, we're going to actually use an async function. And you'll see in a minute why this is useful. So, we're going to use GetArgument in order to retrieve the orderId. And now, whereas in the past we would just return, we're actually going to return using await, and take advantage of this helper function called TryAsyncResolve.
Now, what TryAsyncResolve is going to do is it's going to allow us to asynchronously invoke the method that we're going to call now, which is orders.StartAsync. And it's going to capture the result. If there's any kind of exception, it's going to capture that exception and pass that on to allow the TryAsyncResolve function to handle it. Okay, so we now have completed our OrdersMutation.
Let's just try building to see if we get any errors. No errors. The next thing we want to go onto now is to update our Schema. This is going to be really easy, so let's bring back our Solution Explorer, we'll go into our OrdersSchema, and what we're going to do is we're going to add an additional dependency in our constructor which is going to be the mutation.
And now what we'll do, similar to the way we set our Query, we will set the Mutation. Okay, the very last thing we need to do is to go to our Startup class and register these types that we've just created. So, we'll go back to our Startup method. So, we'll call AddSingleton and we'll register our OrderCreateInputType.
And then we will register our OrdersMutation. That's it.
- Creating a new GraphQL project
- Exposing a new GraphQL API
- Enabling and creating mutations
- Updating and configuring the schema
- Enabling subscriptions
- New GraphQL.NET schema classes
- Creating the OrderEventType