Skip to content

Commit 0773a53

Browse files
committed
⛑ Fix naming convention
1 parent a5efc3b commit 0773a53

File tree

8 files changed

+112
-52
lines changed

8 files changed

+112
-52
lines changed

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,76 @@
33
[![Version](https://img.shields.io/npm/v/graphql-codegen-typescript-operations-tester.svg)](https://www.npmjs.com/package/graphql-codegen-typescript-operations-tester)
44
![Test workflow](https://github.com/hellocomet/graphql-codegen-typescript-operations-tester/workflows/Tests/badge.svg?branch=main)
55
![Release workflow](https://github.com/hellocomet/graphql-codegen-typescript-operations-tester/workflows/Release%20package/badge.svg)
6+
7+
## Install
8+
9+
`npm i -D @graphql-codegen/typescript @graphql-codegen/typescript-operations graphql-codegen-typescript-operations-tester`
10+
11+
In `codegen.yml`
12+
13+
```yaml
14+
generated/tests.ts:
15+
plugins:
16+
- typescript
17+
- typescript-operations
18+
- graphql-codegen-typescript-operations-tester
19+
```
20+
21+
Given a schema like so
22+
23+
```graphql
24+
type Author {
25+
firstname: String
26+
lastname: String
27+
fullname: String
28+
}
29+
30+
type Book {
31+
title: String
32+
author: Author
33+
}
34+
35+
type Query {
36+
books: [Book]
37+
}
38+
```
39+
40+
Create a graphQL requester somewhere in your tests
41+
42+
```typescript
43+
import { graphql, GraphQLSchema, ExecutionResult } from 'graphql'
44+
import { testGetBooksQuery } from './generated/tests.ts'
45+
import { schema } from 'path/to/my/schema'
46+
47+
type Options = {
48+
silenceError?: boolean
49+
}
50+
51+
export function graphqlRequester<TResultDataType>(options: Options = {}) {
52+
return async (
53+
query: string,
54+
variables?: Record<string, any>
55+
): Promise<ExecutionResult<TResultDataType>> => {
56+
const result = await graphql({
57+
variableValues: variables,
58+
schema,
59+
source: query,
60+
})
61+
62+
if (!options.silenceError && result.errors?.length > 0) {
63+
result.errors.forEach((error) => console.error(error))
64+
}
65+
66+
// Allow typing the return value of the query which is by default:
67+
// `Record<string, any>`.
68+
return result as ExecutionResult<TResultDataType>
69+
}
70+
}
71+
72+
describe('Test something cool', () => {
73+
it('testGetBooksQuery should return something', async () => {
74+
const res = await testGetBooksQuery(graphqlRequester())
75+
expect(res.data?.books).toBeAwesome()
76+
})
77+
})
78+
```

package-lock.json

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@
5353
"typescript": "^4.3.5"
5454
},
5555
"dependencies": {
56-
"@graphql-codegen/plugin-helpers": "^1.18.8"
56+
"@graphql-codegen/plugin-helpers": "^1.18.8",
57+
"change-case-all": "^1.0.14"
5758
},
5859
"peerDependencies": {
60+
"@graphql-codegen/typescript": ">=1.21.1",
61+
"@graphql-codegen/typescript-operations": ">=1.17.8",
5962
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
6063
}
6164
}

src/index.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { PluginFunction } from '@graphql-codegen/plugin-helpers'
2+
import { pascalCase } from 'change-case-all'
23
import {
34
concatAST,
45
DocumentNode,
6+
ExecutionResult,
57
FragmentDefinitionNode,
8+
graphql,
9+
GraphQLArgs,
10+
GraphQLSchema,
611
OperationDefinitionNode,
712
print,
813
visit,
@@ -32,7 +37,7 @@ function getOperationFragments(
3237
}
3338

3439
export const plugin: PluginFunction = (schema, documents) => {
35-
const imports = [`import { graphql, ExecutionResult } from 'graphql'`]
40+
const imports = [`import { request, Args } from 'graphql-codegen-typescript-operations-tester'`]
3641

3742
const allAst = concatAST(
3843
documents.reduce<DocumentNode[]>((acc, source) => {
@@ -55,28 +60,26 @@ export const plugin: PluginFunction = (schema, documents) => {
5560
if (!node.name) return
5661

5762
const type = node.operation === 'mutation' ? 'Mutation' : 'Query'
58-
const name = `${node.name.value[0].toUpperCase()}${node.name.value.substr(
59-
1,
60-
node.name.value.length - 1
61-
)}`
63+
64+
// Mimic the default naming convention.
65+
// See https://www.graphql-code-generator.com/docs/getting-started/naming-convention#namingconvention
66+
const name = pascalCase(`${node.name.value}${type}`)
6267

6368
const fragments = getOperationFragments(node, allFragments)
6469
const fragmentsStr =
6570
fragments.size > 0 ? `${Array.from(fragments.values()).map(print)}\n` : ''
6671

6772
lines.push(``)
68-
lines.push(`export const ${name}${type}Source: string = \``)
73+
lines.push(`export const ${name}Source: string = \``)
6974
lines.push(`${fragmentsStr}${print(node)}\`;`)
7075
lines.push(``)
71-
lines.push(`export function test${name}${type}(`)
72-
lines.push(` graphqlRequester: (`)
73-
lines.push(` query: string,`)
74-
lines.push(` variables: ${name}${type}Variables`)
75-
lines.push(` ) => Promise<ExecutionResult<${name}${type}>>,`)
76-
lines.push(` variables: ${name}${type}Variables`)
76+
lines.push(`export function test${name}(`)
77+
lines.push(` graphqlArgs: Args,`)
78+
lines.push(` variables?: ${name}Variables`)
7779
lines.push(`) {`)
78-
lines.push(` const query = ${name}${type}Source`)
79-
lines.push(` return graphqlRequester(query, variables)`)
80+
lines.push(
81+
` return request<${name}>({ ...graphqlArgs, source: ${name}Source, variableValues: variables })`
82+
)
8083
lines.push(`};`)
8184
},
8285
})
@@ -88,3 +91,11 @@ export const plugin: PluginFunction = (schema, documents) => {
8891
content: content,
8992
}
9093
}
94+
95+
export type Args = { schema: GraphQLSchema } & Partial<GraphQLArgs>
96+
97+
export async function request<TData extends Record<string, unknown> = Record<string, unknown>>(
98+
graphqlArgs: GraphQLArgs
99+
): Promise<ExecutionResult<TData>> {
100+
return graphql(graphqlArgs) as Promise<ExecutionResult<TData>>
101+
}

test/int/documents.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
query getBooks {
2-
books {
1+
query getBooks($var1: String!) {
2+
books(var1: $var1) {
33
title
44
author {
55
firstname

test/int/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ type Book {
1010
}
1111

1212
type Query {
13-
books: [Book]
13+
books(var1: String!): [Book]
1414
}

test/int/test.spec.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import { readFileSync } from 'fs'
2-
import { buildSchema, graphql, GraphQLSchema } from 'graphql'
2+
import { buildSchema } from 'graphql'
33
import * as generated from './generated'
44

55
const schema = buildSchema(readFileSync(`${__dirname}/schema.graphql`, { encoding: 'utf-8' }))
66

7-
function graphqlRequester(schema: GraphQLSchema) {
8-
return async (query: string, variables?: null | undefined | Record<string, unknown>) => {
9-
const res = await graphql({
10-
variableValues: variables,
11-
schema,
12-
source: query,
13-
})
14-
return res
15-
}
16-
}
17-
187
describe('Test generated file', () => {
198
it('testGetBooksQuery should exist', async () => {
209
expect(generated).toHaveProperty('testGetBooksQuery')
2110
})
2211

2312
it('testGetBooksQuery should return something', async () => {
24-
const res = await generated.testGetBooksQuery(graphqlRequester(schema), {})
13+
const res = await generated.testGetBooksQuery({ schema }, { var1: 'hello' })
2514
expect(generated).toHaveProperty('testGetBooksQuery')
2615
expect(res.data?.books).toBeNull()
2716
expect(res.errors).toBeUndefined()

tsconfig.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// "allowJs": true, /* Allow javascript files to be compiled. */
1313
// "checkJs": true, /* Report errors in .js files. */
1414
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
15-
// "declaration": true, /* Generates corresponding '.d.ts' file. */
16-
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15+
"declaration": true /* Generates corresponding '.d.ts' file. */,
16+
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
1717
"sourceMap": true /* Generates corresponding '.map' file. */,
1818
// "outFile": "./", /* Concatenate and emit output to single file. */
1919
"outDir": "dist" /* Redirect output structure to the directory. */,
@@ -46,7 +46,9 @@
4646
/* Module Resolution Options */
4747
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4848
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
49-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
49+
"paths": {
50+
"graphql-codegen-typescript-operations-tester": ["./src/index.ts"]
51+
} /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
5052
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
5153
// "typeRoots": [], /* List of folders to include type definitions from. */
5254
// "types": [], /* Type declaration files to be included in compilation. */

0 commit comments

Comments
 (0)