Skip to content

Commit

Permalink
Add graphql unit tests (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrod-lowe authored Aug 17, 2024
1 parent 13f05ae commit 88345b6
Show file tree
Hide file tree
Showing 14 changed files with 7,529 additions and 1,184 deletions.
3 changes: 3 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
exclude_paths:
# Auto-generated files
- "graphql/mutation/*/appsync.js"
- "graphql/query/*/appsync.js"
# The auto-formatter fights with Codacy PMD wanting to be compatiable with all browsers, but this is a local config file
- "graphql/jest.config.js"
6 changes: 3 additions & 3 deletions .github/workflows/environment-main-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
node-version: '20'

- name: Install esbuild
run: npm install -g esbuild

- name: Compile graphql
run: make graphql
- name: Compile, check and test graphql
run: IN_PIPELINE=true make graphql

- name: Configure AWS Access
uses: aws-actions/configure-aws-credentials@55f725fcb586ca16a5ed5b0d75d464defbfa831b
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/environment-main-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Install esbuild
run: npm install -g esbuild

- name: Compile graphql
run: make graphql
- name: Compile, check and test graphql
run: IN_PIPELINE=true make graphql

- name: Configure AWS Access
uses: aws-actions/configure-aws-credentials@55f725fcb586ca16a5ed5b0d75d464defbfa831b
Expand Down
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ terraform/environment/%/.validate: terraform/environment/%/*.tf terraform-format
touch $@

.PHONY: dev
dev: terraform-format terraform/environment/aws-dev/.apply terraform/environment/wildsea-dev/.apply
dev: $(GRAPHQL_DEV) terraform-format terraform/environment/aws-dev/.apply terraform/environment/wildsea-dev/.apply
@true

terraform/environment/aws-dev/.apply: terraform/environment/aws-dev/*.tf terraform/module/iac-roles/*.tf
./terraform/environment/aws-dev/deploy.sh $(ACCOUNT_ID) dev
touch $@

terraform/environment/wildsea-dev/plan.tfplan: terraform/environment/wildsea-dev/*.tf terraform/module/wildsea/*.tf terraform/environment/wildsea-dev/.terraform $(GRAPHQL)
terraform/environment/wildsea-dev/plan.tfplan: terraform/environment/wildsea-dev/*.tf terraform/module/wildsea/*.tf terraform/environment/wildsea-dev/.terraform $(GRAPHQL_JS)
cd terraform/environment/wildsea-dev ; ../../../scripts/run-as.sh $(RO_ROLE) \
terraform plan -out=./plan.tfplan

terraform/environment/wildsea-dev/.apply: terraform/environment/wildsea-dev/plan.tfplan $(GRAPHQL)
terraform/environment/wildsea-dev/.apply: terraform/environment/wildsea-dev/plan.tfplan $(GRAPHQL_JS)
cd terraform/environment/wildsea-dev ; ../../../scripts/run-as.sh $(RW_ROLE) \
terraform apply ./plan.tfplan ; \
status=$$? ; \
Expand All @@ -62,7 +62,3 @@ clean:
rm -f graphql/mutation/*/appsync.js
rm -f graphql/query/*/appsync.js
rm -rf graphql/node_modules

.PHONY: graphql-eslint
graphql-eslint:
docker run --rm -it --user $$(id -u):$$(id -g) -v $(PWD)/graphql:/code pipelinecomponents/eslint eslint
20 changes: 16 additions & 4 deletions graphql/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

import prettierConfig from "eslint-config-prettier";
import prettierPlugin from "eslint-plugin-prettier";

export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
prettierConfig,
{
rules: {
"prettier/prettier": ["error"],
},
},
{
plugins: {
prettier: prettierPlugin,
},
},
];
28 changes: 24 additions & 4 deletions graphql/graphql.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,30 @@ graphql/%/appsync.js: graphql/node_modules graphql/%/appsync.ts
--outdir=$*

graphql/node_modules: graphql/package.json
cd graphql && npm install && npm ci
cd graphql && npm install && npm ci \

GRAPHQL := $(patsubst %.ts,%.js,$(wildcard graphql/*/*/appsync.ts))
GRAPHQL_TS := $(wildcard graphql/*/*/appsync.ts)
GRAPHQL_JS := $(patsubst %.ts,%.js,$(GRAPHQL_TS))
GRAPHQL_DEV := graphql-eslint graphql-test

.PHONY: graphql
graphql: $(GRAPHQL)
echo $(GRAPHQL)
graphql: $(GRAPHQL_JS) graphql-test
echo $(GRAPHQL_JS)

.PHONY: graphql-test
graphql-test: graphql/node_modules
if [ -z "$(IN_PIPELINE)" ] ; then \
docker run --rm -it --user $$(id -u):$$(id -g) -v $(PWD)/graphql:/app -w /app --entrypoint ./node_modules/jest/bin/jest.js node:20 \
else \
cd graphql && jest ; \
fi

# Won't auto-fix in pipeline
.PHONY: graphql-eslint
graphql-eslint: $(GRAPHQL_TS)
if [ -z "$(IN_PIPELINE)" ] ; then \
docker run --rm -it --user $$(id -u):$$(id -g) -v $(PWD)/graphql:/code pipelinecomponents/eslint eslint --fix
else \
cd graphql && eslint ;:w
\
fi
6 changes: 6 additions & 0 deletions graphql/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: { "^.+\\.ts?$": "ts-jest" },
testEnvironment: "node",
testRegex: "/tests/.*\\.(test|spec)?\\.(ts|tsx)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
66 changes: 37 additions & 29 deletions graphql/mutation/createGame/appsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { util, Context, DynamoDBPutItemRequest, AppSyncIdentityCognito, PutItemInputAttributeMap } from '@aws-appsync/utils';
import {
util,
Context,
DynamoDBPutItemRequest,
AppSyncIdentityCognito,
PutItemInputAttributeMap,
} from "@aws-appsync/utils";

/**
* A CreateGameInput creates a Game.
Expand All @@ -12,37 +18,39 @@ interface CreateGameInput {
description?: string;
}

export function request(context: Context<{ input: CreateGameInput }>): DynamoDBPutItemRequest {
if (!context.identity) {
util.error('Unauthorized: Identity information is missing.' as string);
}
export function request(
context: Context<{ input: CreateGameInput }>,
): DynamoDBPutItemRequest {
if (!context.identity) {
util.error("Unauthorized: Identity information is missing." as string);
}

const identity = context.identity as AppSyncIdentityCognito;
if (!identity.sub) {
util.error('Unauthorized: User ID is missing.' as string);
}
const identity = context.identity as AppSyncIdentityCognito;
if (!identity.sub) {
util.error("Unauthorized: User ID is missing." as string);
}

const input = context.arguments.input;
const id = util.autoId();
const timestamp = util.time.nowISO8601();
return {
operation: 'PutItem',
key: util.dynamodb.toMapValues({ PK: "GAME#"+id, SK: 'GAME' }),
attributeValues: util.dynamodb.toMapValues({
name: input.name,
description: input.description,
id: id,
fireflyUserId: identity.sub,
createdAt: timestamp,
updatedAt: timestamp
}) as PutItemInputAttributeMap
};
const input = context.arguments.input;
const id = util.autoId();
const timestamp = util.time.nowISO8601();
return {
operation: "PutItem",
key: util.dynamodb.toMapValues({ PK: "GAME#" + id, SK: "GAME" }),
attributeValues: util.dynamodb.toMapValues({
name: input.name,
description: input.description,
id: id,
fireflyUserId: identity.sub,
createdAt: timestamp,
updatedAt: timestamp,
}) as PutItemInputAttributeMap,
};
}

export function response(context: Context): unknown {
if (context.error) {
util.appendError(context.error.message, context.error.type, context.result);
return;
}
return context.result;
if (context.error) {
util.appendError(context.error.message, context.error.type, context.result);
return;
}
return context.result;
}
Loading

0 comments on commit 88345b6

Please sign in to comment.