-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SDL schema generation test for graphql subgraph
- Loading branch information
1 parent
7746096
commit a1a70ff
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
graphql-cli/src/test/resources/expectedSchemas/schema_product.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"]) | ||
|
||
type Query { | ||
product(id: String!): Product | ||
products: [Product!]! | ||
} | ||
|
||
type Product @key(fields: "id") { | ||
id: String! | ||
title: String! | ||
description: String! | ||
price: Int! | ||
category: Category! | ||
} | ||
|
||
type Category { | ||
id: String! | ||
title: String! | ||
} |
72 changes: 72 additions & 0 deletions
72
graphql-cli/src/test/resources/graphqlServices/valid/service_11.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.org). All Rights Reserved. | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/graphql; | ||
import ballerina/graphql.subgraph; | ||
|
||
@subgraph:Entity { | ||
key: "id", | ||
resolveReference: resolveProduct | ||
} | ||
public type Product record { | ||
string id; | ||
string title; | ||
string description; | ||
int price; | ||
Category category; | ||
}; | ||
|
||
public type Category record { | ||
string id; | ||
string title; | ||
}; | ||
|
||
function resolveProduct(subgraph:Representation representation) returns Product|error? { | ||
string id = check representation["id"].ensureType(); | ||
return trap products.filter(product => product.id == id).pop(); | ||
} | ||
|
||
@subgraph:Subgraph | ||
service /product on new graphql:Listener(4001) { | ||
resource function get product(string id) returns Product? { | ||
var product = products.filter(product => id == product.id); | ||
if (product.length() > 0) { | ||
return product[0]; | ||
} | ||
return (); | ||
} | ||
|
||
resource function get products() returns Product[] { | ||
return products; | ||
} | ||
} | ||
|
||
Category[] categories = [ | ||
{ | ||
id: "1", | ||
title: "Kitchen appliances" | ||
} | ||
]; | ||
|
||
Product[] products = [ | ||
{ | ||
id: "1", | ||
title: "Knife", | ||
description: "A knife is a tool with a cutting edge.", | ||
price: 100, | ||
category: categories[0] | ||
} | ||
]; |