Skip to content

Commit

Permalink
Modify typescript-graphql-request example and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
beerose committed Mar 10, 2023
1 parent ef28588 commit 7a94464
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 146 deletions.
6 changes: 6 additions & 0 deletions examples/typescript-graphql-request/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: process.versions.node.split('.')[0] } }],
'@babel/preset-typescript',
],
};
3 changes: 3 additions & 0 deletions examples/typescript-graphql-request/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const config: CodegenConfig = {
generates: {
'./src/gql/': {
preset: 'client',
config: {
documentMode: 'string',
},
},
},
hooks: { afterAllFileWrite: ['prettier --write'] },
Expand Down
3 changes: 3 additions & 0 deletions examples/typescript-graphql-request/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
transform: { '^.+\\.ts': 'babel-jest' },
};
6 changes: 4 additions & 2 deletions examples/typescript-graphql-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"private": true,
"devDependencies": {
"@graphql-codegen/cli": "3.2.2",
"@graphql-codegen/gql-tag-operations-preset": "2.1.0"
"@graphql-codegen/gql-tag-operations-preset": "2.1.0",
"babel-jest": "28.1.3",
"jest": "28.1.3"
},
"dependencies": {
"graphql": "16.6.0",
Expand All @@ -15,7 +17,7 @@
"codegen": "graphql-codegen --config codegen.ts",
"build": "tsc",
"dev": "ts-node src/main.ts",
"test:end2end": "node dist/main.js"
"test:end2end": "yarn jest"
},
"type": "commonjs",
"bob": false
Expand Down
26 changes: 2 additions & 24 deletions examples/typescript-graphql-request/src/gql/gql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable */
import * as types from './graphql';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';

/**
* Map of all GraphQL operations in the project.
Expand All @@ -19,40 +18,19 @@ const documents = {
types.AllPeopleWithVariablesQueryDocument,
};

/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*
*
* @example
* ```ts
* const query = graphql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
* ```
*
* The query argument is unknown!
* Please regenerate the types.
*/
export function graphql(source: string): unknown;

/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(
source: '\n query AllPeopleQuery {\n allPeople(first: 5) {\n edges {\n node {\n name\n homeworld {\n name\n }\n }\n }\n }\n }\n'
): (typeof documents)['\n query AllPeopleQuery {\n allPeople(first: 5) {\n edges {\n node {\n name\n homeworld {\n name\n }\n }\n }\n }\n }\n'];
): typeof import('./graphql').AllPeopleQueryDocument;
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(
source: '\n query AllPeopleWithVariablesQuery($first: Int!) {\n allPeople(first: $first) {\n edges {\n node {\n name\n homeworld {\n name\n }\n }\n }\n }\n }\n'
): (typeof documents)['\n query AllPeopleWithVariablesQuery($first: Int!) {\n allPeople(first: $first) {\n edges {\n node {\n name\n homeworld {\n name\n }\n }\n }\n }\n }\n'];
): typeof import('./graphql').AllPeopleWithVariablesQueryDocument;

export function graphql(source: string) {
return (documents as any)[source] ?? {};
}

export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = TDocumentNode extends DocumentNode<
infer TType,
any
>
? TType
: never;
149 changes: 29 additions & 120 deletions examples/typescript-graphql-request/src/gql/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
import { TypedDocumentString } from '@graphql-typed-document-node/core';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
Expand Down Expand Up @@ -1309,122 +1309,31 @@ export type AllPeopleWithVariablesQueryQuery = {
} | null;
};

export const AllPeopleQueryDocument = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: { kind: 'Name', value: 'AllPeopleQuery' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'allPeople' },
arguments: [
{ kind: 'Argument', name: { kind: 'Name', value: 'first' }, value: { kind: 'IntValue', value: '5' } },
],
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'edges' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'node' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'name' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'homeworld' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'name' } }],
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
} as unknown as DocumentNode<AllPeopleQueryQuery, AllPeopleQueryQueryVariables>;
export const AllPeopleWithVariablesQueryDocument = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: { kind: 'Name', value: 'AllPeopleWithVariablesQuery' },
variableDefinitions: [
{
kind: 'VariableDefinition',
variable: { kind: 'Variable', name: { kind: 'Name', value: 'first' } },
type: { kind: 'NonNullType', type: { kind: 'NamedType', name: { kind: 'Name', value: 'Int' } } },
},
],
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'allPeople' },
arguments: [
{
kind: 'Argument',
name: { kind: 'Name', value: 'first' },
value: { kind: 'Variable', name: { kind: 'Name', value: 'first' } },
},
],
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'edges' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'node' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'name' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'homeworld' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'name' } }],
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
} as unknown as DocumentNode<AllPeopleWithVariablesQueryQuery, AllPeopleWithVariablesQueryQueryVariables>;
export const AllPeopleQueryDocument = `
query AllPeopleQuery {
allPeople(first: 5) {
edges {
node {
name
homeworld {
name
}
}
}
}
}
` as unknown as TypedDocumentString<AllPeopleQueryQuery, AllPeopleQueryQueryVariables>;
export const AllPeopleWithVariablesQueryDocument = `
query AllPeopleWithVariablesQuery($first: Int!) {
allPeople(first: $first) {
edges {
node {
name
homeworld {
name
}
}
}
}
}
` as unknown as TypedDocumentString<AllPeopleWithVariablesQueryQuery, AllPeopleWithVariablesQueryQueryVariables>;
8 changes: 8 additions & 0 deletions examples/typescript-graphql-request/src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { execSync } from 'child_process';

describe('TypeScript GraphQL Request tests', () => {
it('works', () => {
const result = execSync('node dist/main.js', { stdio: 'pipe' }).toString();
expect(result).toContain('Luke Skywalker');
});
});

0 comments on commit 7a94464

Please sign in to comment.