graphql-dotnet / examples

Examples for GraphQL.NET
https://graphql-dotnet.github.io
MIT License
268 stars 188 forks source link

Add ASP.Net Core MVC Controller sample #91

Closed Shane32 closed 2 years ago

Shane32 commented 2 years ago

Closes:

Shane32 commented 2 years ago

@sungam3r2022 We have been asked for this sample so many times that I created an example here; please review at your convenience. It is largely a copy of the AspNetCoreCustom sample. I updated a few things to have everything pulled from DI, etc, as would be in line with our latest recommendations. There is no complexity to support a form post body as well as a JSON post body, or many of the other features supported by the server project. Perhaps that would be well to add, but as of now it just supports a JSON post body.

sungam3r2022 commented 2 years ago

Reviewed.

killjoy2013 commented 2 years ago

Hey @Shane32 , very impressed by your prompt action, thank you :-) .net core controller sample runs OK. Just tried to add this starwars sample as my third subgraph into apollo federation gateway. Already have two subgraphs created with Nestjs. STARWARS_SUBGRAPH=http://localhost:6100/api/graphql (running your sample on 6100)

Apollo Gateway runs in http://localhost:4000

Below errors happened;


[Nest] 74252  - 03/04/2022, 10:07:58 AM     LOG [InstanceLoader] GraphQLModule dependencies initialized +1ms
(node:74252) UnhandledPromiseRejectionWarning: Error: Couldn't load service definitions for "starwars" at http://localhost:6100/api/graphql: 400: Bad Reques

t
    at C:\dev\ess-web-2022\gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\loadServicesFromRemoteEndpoint.ts:77:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Promise.all (index 2)
    at loadServicesFromRemoteEndpoint (C:\dev\ess-web-2022\gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\loadServicesFrom

Can't we use it in apollo federation? Do we need another sample?

Thanks & regards

Shane32 commented 2 years ago

While it is compatible, I’ve never set up Apollo Federation or used Apollo Gateway unfortunately. It’s something that I need to learn also! I believe you need to use the schema-first technique to create the schema. That’s about all I know. Sorry I can’t be more help.

killjoy2013 commented 2 years ago

Would you like me to create a working apollo federation sample github repo for your reference?

Shane32 commented 2 years ago

I do not have the time to work on this. However, if you would like to make a pull request to this repo comprising of a complete sample that demonstrates Apollo federation with two GraphQL.NET projects, that would be great. There should be a powershell script that starts both of the GraphQL.NET projects and also the Apollo Gateway (or whatever is needed) for a complete sample, along with a url to playground or something that demonstrates making a request to Apollo Gateway that is properly "federated" between the two projects as necessary. I would imagine we should be able to make a request like this

{
  widget {  # handled by project 1
    id
    name
    category {  # handled by project 2
      id
      name
      widgets {  # handled by project 1
        id
        name
      }
    }
  }
}

I'm sorta guessing on all this, since I've never used federation, but you get the idea.

If the sample isn't complete, it is unlikely we will want to merge it in here, as we cannot answer questions or support a project that does not execute (especially when we do not understand it). However, if it is complete, it will help me to understand federation, and as we make updates to GraphQL.NET, we will be able to test federation with this sample to be sure we did not break anything.

Your assistance would be very much welcomed if you would like to participate here. It is possible that @joemcbride may be able to answer questions, as I believe he does use federation.

Thanks for the offer!

cc: @sungam3r2022 @joemcbride

sungam3r2022 commented 2 years ago

Note from me - I started to work with federation some time ago and can say that GraphQL.NET needs a bunch of work to support federation for code first. And not only for code first. I started to implement this stuff. I stopped working on PR because of other things I had to do here and at work. I'm going to return to that work soon, at least I hope to release it in v5. Really.

killjoy2013 commented 2 years ago

Hi @Shane32 @joemcbride @sungam3r2022 ,

I created a Github repo and added detailed explanations and steps in README. Please check it out. Structure is like;

D:\DEV\GIT\GRAPHQL.NET-IN-APOLLO-FEDERATION
├───country-subgraph
│   └───src
│       └───country
│           ├───dto
│           └───entities
├───federation-gateway
│   └───src
├───food-subgraph
│   └───src
│       └───restaurant
│           ├───dto
│           └───entities
└───starwars-subgraph
    ├───StarWars
    │   └───Types
    └───WebApi
        ├───Controllers
        └───Properties

country-subgraph, food-subgraph & federation-gateway are created by NestJS and use ApolloServer. Gateway don't care if the subgraphs are code-firts or SDL-first. While the country-subgraph is code-first, food-subgraph is SDL-first. They're functional. You can try it following the steps in README.

starwars-subgraph is created with GraphQL.NET 4.7.1. You can add it to gateway just uncommenting a few lines . Below errors occur when added,

(node:3892) UnhandledPromiseRejectionWarning: Error: Couldn't load service definitions for "starwars"
    at C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\loadServicesFromRemoteEndpoint.ts:77:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Promise.all (index 2)
    at loadServicesFromRemoteEndpoint (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\loadServicesFromRemoteEndpoint.ts:81:30)
    at IntrospectAndCompose.updateSupergraphSdl (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\index.ts:95:20)
    at IntrospectAndCompose.initialize (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\supergraphManagers\IntrospectAndCompose\index.ts:65:30)
    at ApolloGateway.initializeSupergraphManager (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\index.ts:456:22)
    at ApolloGateway.load (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\@apollo\gateway\src\index.ts:351:7)
    at SchemaManager.start (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\apollo-server-core\src\utils\schemaManager.ts:111:22)
    at ApolloServer._start (C:\murat\graphql.net-apollo-federation\federation-gateway\node_modules\apollo-server-core\src\ApolloServer.ts:359:24)

I guess, Apollo Gateway sends an introspection query and it failes. I may be doing something wrong, not sure. Maybe it can be fixed easily.

Importance of being able to add our graphql.net as subgraph to an existing Apollo Federation is really huge. When we achieve it, we'll be able to use our legacy codebases which are in Graphql.Net 2. We'll just upgrade to 4.7.1 and add it to our Apollo Server. So, we won't need to change the codebase! Would be wonderful :-)

As seen here, we're not trying to create the gateway with Graphql.Net. Gateway already exists. Apollo Federation is running. We just need to add our GraphQL.Net 4 app as a subgraph.

I'd send a PR for this sample gladly, when we put it to a final shape. I'd like to help for this in any way I can...

Thanks & regards