# Build a GraphQL API with ASP.NET Core

**URL:** <https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037>\
**Category:** Developer Blog Comments\
**Created:** [April 16, 2019, 8:18pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037 "2019-04-16T20:18:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 16, 2019, 8:18pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/1 "2019-04-16T20:18:45Z")

</div>

[Build a GraphQL API with ASP.NET Core](https://developer.okta.com/blog/2019/04/16/graphql-api-with-aspnetcore)

‘Build an API with GraphQL and [ASP.NET](http://ASP.NET) Core.’

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 18, 2019, 12:12am UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/2 "2019-04-18T00:12:42Z")

</div>

**Ahad L. Amdani**

Hello Ibrahim,

Great article, first and foremost. I’ve learned quite a bit from the quick run-down, so I just wanted to drop you a note and say I appreciate the level of detail that went into this article.

Secondly, it appears there is a formatting issue in this article. Directly before the “Secure Your [ASP.NET](http://ASP.NET) Core GraphQL Application” section, the code ticks seem to be missing to end the code block, thus rendering the next section (until the next set of ending code ticks) as code instead of the proper markdown you’ve supplied.

Thanks!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 18, 2019, 12:59pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/3 "2019-04-18T12:59:53Z")

</div>

**Dave van Herten**

This is an interesting implementation. All of the GraphQL implementations I’ve seen so far have accessed the Entity Framework DB context by adding Service Location within Resolve methods which allows for an instance per request.

This takes a different take on it where a controller is used to build up the schema and execute it allowing the controller to bring in the request scoped DBContext dependency. Despite the service location, this has the advantage of a singleton schema declaration which prevents building it on every request.

The OPs mechanism is essentially trading avoiding service location in favor of building the schema up on every single request. Depending on the size of your schema I like the way this looks, but I’d have to play with it more.

Neat stuff! Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 19, 2019, 2:31pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/4 "2019-04-19T14:31:13Z")

</div>

**Mathys**

Thanks for this tutorial.

Things I did to make the client work and the article forgot to mention:

define postUri   
var postUri = “[https://localhost:5001/graphql](https://localhost:5001/graphql)”;

added access\_token to the scopes of the default authorization server in the admin page of okta.   
This solved the badrequest I got:

BadRequest {“error”:“invalid\_scope”,“error\_description”:“One or more scopes are not configured for the authorization server resource.”}

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 22, 2019, 10:24pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/5 "2019-04-22T22:24:08Z")

</div>

**Wouter**

How to solve HttpRequestException:The SSL connection could not be established.  
The remote certificate is invalid according to the validation procedure.

Btw my should i get a custom [dev-918247.okta.com](http://dev-918247.okta.com "dev-918247.okta.com") ? what’s the point of having number that seems random to be the same for everyone?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 23, 2019, 5:33am UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/6 "2019-04-23T05:33:20Z")

</div>

**Mathys**

See the part about trusting a certificate:

[https://docs.microsoft.com/…](https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos "https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos")

Or simply disable the useHttps middelware and change https to http in your client code

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [May 23, 2019, 5:53pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/7 "2019-05-23T17:53:01Z")

</div>

**leebrandt**

Your domain is automatically placed into blog posts if you’re logged into Okta. The blog post itself just has a placeholder in it.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 14, 2019, 10:40pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/8 "2019-09-14T22:40:12Z")

</div>

**Dang**

Thanks for article. Everything works except the data returns with extra “$id” which is the id of graphQL and I have no idea how to exclude this field.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [November 5, 2019, 1:01pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/9 "2019-11-05T13:01:50Z")

</div>

**Peter Kellner**

No repo?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [November 5, 2019, 3:06pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/10 "2019-11-05T15:06:52Z")

</div>

**leebrandt**

I’m afraid not.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [November 5, 2019, 3:10pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/11 "2019-11-05T15:10:08Z")

</div>

**Peter Kellner**

Thanks, I’m just learning graphql with core 3 and slowly pasting in code. There does seem to be some syntax mistakes that would make the code never actually work but not 100% sure yet.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [March 2, 2020, 7:27pm UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/12 "2020-03-02T19:27:48Z")

</div>

**christos-P**

Nice article ! Thanks !

Below I add some things that are needed for running it actually:

1. At the [https://developer.okta.com/…](https://developer.okta.com/blog/2019/04/16/graphql-api-with-aspnetcore#seed-the-database "https://developer.okta.com/blog/2019/04/16/graphql-api-with-aspnetcore#seed-the-database") we have to add an Id for both Author and for the Books.

2. At the same point as in 1, [https://developer.okta.com/…](https://developer.okta.com/blog/2019/04/16/graphql-api-with-aspnetcore#seed-the-database "https://developer.okta.com/blog/2019/04/16/graphql-api-with-aspnetcore#seed-the-database"), we have to call ToString on authorDbEntry.Entity.Id, since the later returns an int and AuthorId is of type string.

3. We have to add the packages in the attached screenshot [https://uploads.disquscdn.c…](https://uploads.disquscdn.com/images/8497b801cee8f2043c08a208e1bda12e361a40d87ff2844dd0ef54e07ef5d54d.png "https://uploads.disquscdn.com/images/8497b801cee8f2043c08a208e1bda12e361a40d87ff2844dd0ef54e07ef5d54d.png") along with the packages mentioned in the post.

4. We have to go to the Okta dev portal and the go to API → Authorization Servers → Default (The one we have created in the early stage of this post) → Scopes → Add Scope and add scope with the name access\_token. Pls check the attached screenshot [https://uploads.disquscdn.c…](https://uploads.disquscdn.com/images/5c3b971b04ed89f673b3d43f1954000173821041a5f04fcbeaf89a509e4cf41b.png "https://uploads.disquscdn.com/images/5c3b971b04ed89f673b3d43f1954000173821041a5f04fcbeaf89a509e4cf41b.png")

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [July 22, 2020, 7:45am UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/13 "2020-07-22T07:45:06Z")

</div>

**adrian**

A bit late to the party - but this may be the repo: [https://github.com/Ibro/Okt…](https://github.com/Ibro/OktaGraphQL/tree/master/Api "https://github.com/Ibro/OktaGraphQL/tree/master/Api")

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 29, 2020, 12:39am UTC](https://devforum.okta.com/t/build-a-graphql-api-with-asp-net-core/17037/14 "2020-08-29T00:39:56Z")

</div>

**Daniel Brown**

Thanks for the tutorial! Question, How do you add multiple Queries to the GraphQLController Schema object? For instance if I have a bookQuery.cs and an authorQuery.cs and a… categoryQuery.cs class and want to be able to query all of those, how would I go about adding those in the GraphQL Controller as well?

Thanks!
