Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@link directive is missing when using hotchocolate authorization #55

Open
rohinz opened this issue Nov 10, 2023 · 2 comments
Open

@link directive is missing when using hotchocolate authorization #55

rohinz opened this issue Nov 10, 2023 · 2 comments

Comments

@rohinz
Copy link

rohinz commented Nov 10, 2023

Adding HotChocolate.AspNetCore.Authorization to the schema removes the @link directive from the schema.

How to reproduce

  1. create a graphql schema with federation support
using ApolloGraphQL.HotChocolate.Federation;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddGraphQLServer()
    .AddApolloFederationV2()
    .AddQueryType<Query>();

var app = builder.Build();

app.MapGraphQL("/");
app.RunWithGraphQLCommands(args);

public class Query
{
    public User GetUser() => new("1", "David");
    [ReferenceResolver]
    public User ResolveUser(string id) => new(id, "David");
}

[Key("id")]
public record User(string Id, string Name);
  1. print the schema with dotnet run -- schema export

the schema contains the @link directive

schema @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.5", import: [ "@extends", "@external", "@key", "@inaccessible", "@override", "@provides", "@requires", "@shareable", "@tag", "FieldSet", "@composeDirective", "@interfaceObject" ]) {
  query: Query
}

type Query {
  user: User!
  resolveUser(id: String!): User!
  _service: _Service!
  _entities(representations: [_Any!]!): [_Entity]!
}
...
  1. add HotChocolate.AspNetCore.Authorization nuget and add authorization to the schema builder:
using ApolloGraphQL.HotChocolate.Federation;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddGraphQLServer()
    .AddApolloFederationV2()
    .AddAuthorization()
    .AddQueryType<Query>();

var app = builder.Build();

app.MapGraphQL("/");
app.RunWithGraphQLCommands(args);

public class Query
{
    public User GetUser() => new("1", "David");
    [ReferenceResolver]
    public User ResolveUser(string id) => new(id, "David");
}

[Key("id")]
public record User(string Id, string Name);
  1. print the schema again with dotnet run -- schema export

Expectation

  • the schema will still define a @link directive

Current Behavior

  • the @link directive is gone :-(
schema {
  query: Query
}

type Query {
  user: User!
  resolveUser(id: String!): User!
  _service: _Service!
  _entities(representations: [_Any!]!): [_Entity]!
}
@dariuszkuc
Copy link
Member

Hello 👋
I believe this is due to the limitations of the existing HC TypeInterceptor API (that 🤞 will be fixed in their replacement "skimmed" API which is still WIP). Underlying issue is that current HC API is somewhat finicky and depending on the order of processed items you can have side effects that break other things. AFAIK currently it is not possible to apply multiple schema transformations (see: ChilliCream/graphql-platform#6651) so you only get the results of applying the last transformation.

Since the @link definitions are getting removed it looks like HotChocolate.AspNetCore.Authorization also attempts to change the schema object so you will only get the last transformation. You could try changing the order of the extension but I wouldn't be surprised if your HC authorization logic stops working in that case, i.e.

builder.Services
    .AddGraphQLServer()
    .AddAuthorization()
    .AddApolloFederationV2()
    .AddQueryType<Query>();

@cli00004
Copy link

cli00004 commented Jan 9, 2024

@dariuszkuc Changing order still resulted in same behaviour (@link missing) which resulted in federation compatibility issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants