Skip to content

Commit

Permalink
Fixed argument variable resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Mar 7, 2023
1 parent f42fe7c commit bbb93f8
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public ArgumentVariableDefinition(
string argumentName)
{
Name = name;
Subgraph = subgraph;
SubgraphName = subgraph;
Type = type;
ArgumentName = argumentName;
}

public string Name { get; }

public string Subgraph { get; }
public string SubgraphName { get; }

public ITypeNode Type { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public FieldVariableDefinition(
FieldNode select)
{
Name = name;
Subgraph = subgraph;
SubgraphName = subgraph;
Select = select;
}

public string Name { get; }

public string Subgraph { get; }
public string SubgraphName { get; }

public FieldNode Select { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ internal interface IVariableDefinition
/// <summary>
/// Gets the name of the subgraph the variable is defined for.
/// </summary>
string Subgraph { get; }
string SubgraphName { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ private void ResolveRequirements(

foreach (var variable in field.Variables)
{
if (resolver.Requires.Contains(variable.Name))
if (resolver.Requires.Contains(variable.Name) &&
resolver.SubgraphName.Equals(variable.SubgraphName))
{
if (variable is ArgumentVariableDefinition argumentVariable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ executionStep.ParentSelection is { } parent &&
// from sibling execution steps.
foreach (var variable in step.SelectionSetType.Variables)
{
var schemaName = variable.Subgraph;
var subgraphName = variable.SubgraphName;

if (requires.Contains(variable.Name) &&
schemas.TryGetValue(schemaName, out var providingExecutionStep))
schemas.TryGetValue(subgraphName, out var providingExecutionStep))
{
requires.Remove(variable.Name);

Expand Down Expand Up @@ -98,7 +98,7 @@ executionStep.ParentSelection is { } parent &&
foreach (var variable in step.SelectionSetType.Variables)
{
if (executionStep.Requires.Contains(variable.Name) &&
executionStep.SubgraphName.EqualsOrdinal(variable.Subgraph) &&
executionStep.SubgraphName.EqualsOrdinal(variable.SubgraphName) &&
context.Exports.TryGetStateKey(selectionSet, variable.Name, out var stateKey, out _))
{
context.Exports.RegisterAdditionExport(variable, executionStep, stateKey);
Expand Down
40 changes: 40 additions & 0 deletions src/HotChocolate/Fusion/test/Core.Tests/DemoIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ query GetUser {
}
""");

// act
var result = await executor.ExecuteAsync(
QueryRequestBuilder
.New()
.SetQuery(request)
.Create());

// assert
var snapshot = new Snapshot();
CollectSnapshotData(snapshot, request, result, fusionGraph);
await snapshot.MatchAsync();
}

[Fact]
public async Task Authors_And_Reviews_Query_GetUserById()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();

// act
var fusionGraph = await new FusionGraphComposer().ComposeAsync(
new[]
{
demoProject.Reviews.ToConfiguration(ReviewsExtensionSdl),
demoProject.Accounts.ToConfiguration(AccountsExtensionSdl)
});

var executor = await new ServiceCollection()
.AddSingleton(demoProject.HttpClientFactory)
.AddFusionGatewayServer(SchemaFormatter.FormatAsString(fusionGraph))
.BuildRequestExecutorAsync();

var request = Parse(
"""
query GetUser {
userById(id: 1) {
id
}
}
""");

// act
var result = await executor.ExecuteAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
User Request
---------------
query GetUser {
userById(id: 1) {
id
}
}
---------------

QueryPlan
---------------
{
"document": "query GetUser { userById(id: 1) { id } }",
"operation": "GetUser",
"rootNode": {
"type": "Serial",
"nodes": [
{
"type": "Resolver",
"schemaName": "Reviews",
"document": "query GetUser_1 { userById: authorById(id: 1) { id } }",
"selectionSetId": 0
},
{
"type": "Composition",
"selectionSetIds": [
0
]
}
]
}
}
---------------

Result
---------------
{
"data": {
"userById": {
"id": 1
}
}
}
---------------

Fusion Graph
---------------
schema @fusion(version: 1) @httpClient(subgraph: "Reviews", baseAddress: "http:\/\/localhost:5000\/graphql") @httpClient(subgraph: "Accounts", baseAddress: "http:\/\/localhost:5000\/graphql") {
query: Query
}

type Query {
productById(upc: Int!): Product! @variable(subgraph: "Reviews", name: "upc", argument: "upc") @resolver(subgraph: "Reviews", select: "{ productById(upc: $upc) }", arguments: [ { name: "upc", type: "Int!" } ])
reviews: [Review!]! @resolver(subgraph: "Reviews", select: "{ reviews }")
userById(id: Int!): User! @variable(subgraph: "Reviews", name: "id", argument: "id") @resolver(subgraph: "Reviews", select: "{ authorById(id: $id) }", arguments: [ { name: "id", type: "Int!" } ]) @variable(subgraph: "Accounts", name: "id", argument: "id") @resolver(subgraph: "Accounts", select: "{ userById(id: $id) }", arguments: [ { name: "id", type: "Int!" } ])
users: [User!]! @resolver(subgraph: "Accounts", select: "{ users }")
usersById(ids: [Int!]!): [User!]! @variable(subgraph: "Accounts", name: "ids", argument: "ids") @resolver(subgraph: "Accounts", select: "{ usersById(ids: $ids) }", arguments: [ { name: "ids", type: "[Int!]!" } ])
}

type Product @variable(subgraph: "Reviews", name: "Product_upc", select: "upc") @resolver(subgraph: "Reviews", select: "{ productById(upc: $Product_upc) }", arguments: [ { name: "Product_upc", type: "Int!" } ]) {
reviews: [Review!]! @source(subgraph: "Reviews")
upc: Int! @source(subgraph: "Reviews")
}

type Review {
author: User! @source(subgraph: "Reviews")
body: String! @source(subgraph: "Reviews")
id: Int! @source(subgraph: "Reviews")
product: Product! @source(subgraph: "Reviews")
}

type User @source(subgraph: "Reviews", name: "Author") @variable(subgraph: "Reviews", name: "User_id", select: "id") @variable(subgraph: "Accounts", name: "User_id", select: "id") @resolver(subgraph: "Reviews", select: "{ authorById(id: $User_id) }", arguments: [ { name: "User_id", type: "Int!" } ]) @resolver(subgraph: "Accounts", select: "{ userById(id: $User_id) }", arguments: [ { name: "User_id", type: "Int!" } ]) @resolver(subgraph: "Accounts", select: "{ usersById(ids: $User_id) }", arguments: [ { name: "User_id", type: "[Int!]!" } ], kind: "BATCH_BY_KEY") {
birthdate: DateTime! @source(subgraph: "Accounts")
id: Int! @source(subgraph: "Reviews") @source(subgraph: "Accounts")
name: String! @source(subgraph: "Reviews") @source(subgraph: "Accounts")
reviews: [Review!]! @source(subgraph: "Reviews")
username: String! @source(subgraph: "Accounts")
}

"The `DateTime` scalar represents an ISO-8601 compliant date time type."
scalar DateTime
---------------
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public IEnumerable<User> GetUsersById(
}
}
}

}

0 comments on commit bbb93f8

Please sign in to comment.