Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions protection/makeAllPublic/api.graphql
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
type Customer {
name: String
city: String
name: String
city: String
}
type Query {
# An ecmascript generator of customer data.
# Of course, in real life you will call an API or a database. You can do that by changing the `endpoint` argument on the `@rest` directive.
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
customer (id: ID): Customer
@rest (endpoint: "stepzen:empty"
ecmascript: """
function transformREST(s) {
var id = get('id')
if (id==1)
return (JSON.stringify({"name":"John Doe","city":"Miami"}))
else
return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"}))
}
"""
# An ecmascript generator of customer data.
# Of course, in real life you will call an API or a database. You can do that by changing the `endpoint` argument on the `@rest` directive.
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
customer(id: ID!): Customer
@rest(
endpoint: "stepzen:empty"
ecmascript: """
function transformREST(s) {
var id = get('id')
if (id==1)
return (JSON.stringify({"name":"John Doe","city":"Miami"}))
else
return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"}))
}
"""
)
}
}
6 changes: 6 additions & 0 deletions protection/makeAllPublic/operations.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query Customer($id: ID!) {
customer(id: $id) {
name
city
}
}
40 changes: 38 additions & 2 deletions protection/makeAllPublic/tests/Test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require("fs");
const path = require("node:path");
const {
deployAndRun,
authTypes,
Expand All @@ -6,8 +8,42 @@ const {

testDescription = getTestDescription("snippets", __dirname);

const requestsFile = path.join(path.dirname(__dirname), "operations.graphql");
const requests = fs.readFileSync(requestsFile, "utf8").toString();

describe(testDescription, function () {
// note tests using no authorization since api is public
const tests = [
]
{
label: "customer-1",
query: requests,
operationName: "Customer",
variables: {
id: 1,
},
expected: {
customer: {
name: "John Doe",
city: "Miami",
},
},
authType: authTypes.noAuth,
},
{
label: "customer-2",
query: requests,
operationName: "Customer",
variables: {
id: 2,
},
expected: {
customer: {
name: "Jane Smith",
city: "Santa Clara",
},
},
authType: authTypes.noAuth,
},
];
return deployAndRun(__dirname, tests);
});
});
15 changes: 11 additions & 4 deletions tests/gqltest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function deployEndpoint(endpoint, dirname) {
// The test will fail if the request does not
// have status 200 or has any GraphQL errors.
function runGqlOk(authType, endpoint, query, variables, operationName) {
let authValue;
switch (authType) {
case authTypes.adminKey:
authValue = adminKey;
Expand All @@ -59,12 +60,18 @@ function runGqlOk(authType, endpoint, query, variables, operationName) {
default:
authValue = "";
}

let headers = {
"Content-Type": "application/json",
};

if (authValue) {
headers.Authorization = authValue;
}

return fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: authValue,
},
headers: headers,
body: JSON.stringify({
query: query,
variables: variables,
Expand Down