Skip to content

Commit

Permalink
add graphql plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fantazista committed Nov 13, 2024
1 parent 3944f6b commit 16e4b6b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
3 changes: 1 addition & 2 deletions dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"@aws-sdk/util-dynamodb": "^3.645.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/nock-aws": "^0.8.0",
"graphql-request": "^7.1.2"
"@cgauge/nock-aws": "^0.8.0"
},
"scripts": {
"build": "rm -rf dist && tsc",
Expand Down
3 changes: 2 additions & 1 deletion dtc-graphql-plugin/src/graphql-call-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import extraAssert from '@cgauge/assert'
import {isRecord} from '@cgauge/dtc'
import {GraphQLClient} from 'graphql-request'

type GraphQlCall = {url: string; query: string; variables: {}} & RequestInit
type GraphQlCall = {url: string; query: string; variables: {}; headers?: Record<string, string>}

const isGraphQlCall = (v: unknown): v is GraphQlCall => isRecord(v) && 'url' in v

Expand All @@ -18,6 +18,7 @@ export const act = async (args: unknown) => {
...args.headers,
authorization: process.env.AUTHORIZATION_TOKEN || '',
},
...args,
})

response = await graphQLClient.request(args.query, args.variables)
Expand Down
24 changes: 20 additions & 4 deletions dtc-graphql-plugin/test/graphql-call-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ afterEach(() => {
checkForPendingMocks()
})

test.only('It calls a graphql endpoint', async () => {
process.env.AUTHORIZATION_TOKEN = 'token';
test('It calls a graphql endpoint with the token', async () => {
process.env.AUTHORIZATION_TOKEN = 'token'
const response = {data: {key: 'value'}}
const query = `myQuery { items: {name, status} }`
const variables = {id: 1}

const expectedHeaders = {
accept: 'application/graphql-response+json, application/json',
authorization: process.env.AUTHORIZATION_TOKEN,
'content-type': 'application/json',
}

appsync({query, variables}, expectedHeaders, response)
Expand All @@ -30,3 +28,21 @@ test.only('It calls a graphql endpoint', async () => {

await assert({graphql: response})
})

test('It calls a graphql endpoint without the token', async () => {
delete process.env.AUTHORIZATION_TOKEN
const response = {data: {key: 'value'}}
const query = `myQuery { items: {name, status} }`
const expectedHeaders = {
authorization: '',
}

appsync({query}, expectedHeaders, response)

await act({
url: `https://appsync.eu-west-1.amazonaws.com`,
query,
})

await assert({graphql: response})
})
9 changes: 5 additions & 4 deletions dtc-graphql-plugin/test/nock-appsync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {afterEach, test} from 'node:test'
import {act, assert} from '../src/graphql-call-plugin'
import nock from 'nock'
import {RequestDocument} from 'graphql-request'
import nodeAssert from 'node:assert/strict'
Expand All @@ -22,11 +20,14 @@ export const partialBodyCheck = (expected: AppSyncRequest) => (body: Record<stri
return true
}

export const appsync = (request: AppSyncRequest, expectedHeaders, response): void => {
export const appsync = (
request: AppSyncRequest,
expectedHeaders: Record<string, string>,
response: string | Record<string, unknown>,
): void => {
nock('https://appsync.eu-west-1.amazonaws.com')
.post('/', partialBodyCheck(request))
.matchHeader('authorization', expectedHeaders.authorization)
.matchHeader('accept', expectedHeaders.accept)
.reply(200, response)
}

Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16e4b6b

Please sign in to comment.