Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Mar 10, 2023
1 parent 5c1471a commit 7285e00
Show file tree
Hide file tree
Showing 39 changed files with 573 additions and 821 deletions.
4 changes: 2 additions & 2 deletions examples/ez/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"gqty": "workspace:^2.3.0",
"graphql": "^16.6.0",
"graphql-ez": "^0.16.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"randomstring": "^1.2.3",
"test-utils": "workspace:^0.1.0"
},
"devDependencies": {
"@types/lodash": "^4.14.191",
"@types/lodash-es": "^4.17.6",
"@types/node": "^18.14.6",
"@types/randomstring": "^1.1.8",
"bob-tsm": "^1.1.2",
Expand Down
46 changes: 24 additions & 22 deletions examples/ez/src/generated/gqty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,48 @@ import {
GeneratedSchema,
generatedSchema,
scalarsEnumsHash,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';

import { CreateTestClient } from '@graphql-ez/fastify-testing';

const testClientPromise = CreateTestClient(ezApp);
const queryFetcher: QueryFetcher = async function (query, variables) {
const queryFetcher: QueryFetcher = async function ({
query,
variables,
operationName,
}) {
const testClient = await testClientPromise;

return testClient.query(query, {
variables,
operationName,
});
};

export const client = createClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher,
normalization: {
identifier(obj) {
switch (obj.__typename) {
case 'A': {
return obj.a;
scalars: scalarsEnumsHash,
cacheOptions: {
normalization: {
identity(obj) {
switch (obj.__typename) {
case 'A': {
return `${obj.a ?? ''}`;
}
default: {
return;
}
}
default: {
return;
}
}
},
schemaKeys: {},
},
keyFields: {},
},
fetchOptions: {
fetcher: queryFetcher,
},
});

const { query, mutation, mutate, subscription, resolved, refetch } = client;

export { query, mutation, mutate, subscription, resolved, refetch };

export * from './schema.generated';
export { query, mutation, mutate, subscription, resolved, refetch };
20 changes: 0 additions & 20 deletions examples/ez/src/generated/schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,26 +212,6 @@ export interface Subscription {
__typename?: 'Subscription';
}

export interface SchemaObjectTypes {
A: A;
B: B;
C: C;
Dog: Dog;
Human: Human;
Mutation: Mutation;
Query: Query;
Subscription: Subscription;
}
export type SchemaObjectTypesNames =
| 'A'
| 'B'
| 'C'
| 'Dog'
| 'Human'
| 'Mutation'
| 'Query'
| 'Subscription';

export interface $NamedEntity {
Dog?: Dog;
Human?: Human;
Expand Down
2 changes: 1 addition & 1 deletion examples/ez/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ezAltairIDE } from '@graphql-ez/plugin-altair/static';
import { ezCodegen } from '@graphql-ez/plugin-codegen';
import { ezSchema } from '@graphql-ez/plugin-schema';
import Fastify, { LogLevel } from 'fastify';
import { random, range } from 'lodash';
import { random, range } from 'lodash-es';
import { generate } from 'randomstring';
import { gql } from 'test-utils';
import { GreetingsEnum, Human, TestUnion } from './ez.generated';
Expand Down
19 changes: 8 additions & 11 deletions examples/ez/test/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { waitForExpect } from 'test-utils';

import { CreateTestClient, GlobalTeardown } from '@graphql-ez/fastify-testing';
import { selectFields } from 'gqty';

import { waitForExpect } from 'test-utils';
import { ezApp } from '../src';
import {
ArrayObjectArgsDocument,
MultipleArgsDocument,
SimpleStringDocument,
} from '../src/ez.generated';
import {
client as generatedClient,
GreetingsEnum,
mutation,
query,
resolved,
} from '../src/generated/gqty';
import {
ArrayObjectArgsDocument,
MultipleArgsDocument,
SimpleStringDocument,
} from '../src/ez.generated';

import { CreateTestClient, GlobalTeardown } from '@graphql-ez/fastify-testing';
import { ezApp } from '../src';

const testClientPromise = CreateTestClient(ezApp);

Expand Down
19 changes: 7 additions & 12 deletions examples/github/src/gqty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
* GQTY: You can safely modify this file and Query Fetcher based on your needs
*/

import 'isomorphic-unfetch';
import 'dotenv/config';
import 'isomorphic-unfetch';

import { createClient, QueryFetcher } from 'gqty';

import {
generatedSchema,
GeneratedSchema,
scalarsEnumsHash,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';

if (!process.env.GITHUB_TOKEN) {
Expand Down Expand Up @@ -41,18 +39,15 @@ const queryFetcher: QueryFetcher = async function (query, variables) {
return json;
};

export const client = createClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher,
scalars: scalarsEnumsHash,
fetchOptions: {
fetcher: queryFetcher,
},
});

const { query, mutation, mutate, subscription, resolved, refetch } = client;

export { query, mutation, mutate, subscription, resolved, refetch };

export * from './schema.generated';
export { query, mutation, mutate, subscription, resolved, refetch };
23 changes: 8 additions & 15 deletions integration/graphql-17/src/gqty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@
import type { QueryFetcher } from 'gqty';
import { createClient } from 'gqty';
import { TestClient } from './api';
import type {
GeneratedSchema,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';
import type { GeneratedSchema } from './schema.generated';
import { generatedSchema, scalarsEnumsHash } from './schema.generated';

const queryFetcher: QueryFetcher = async function (query, variables) {
const queryFetcher: QueryFetcher = async function ({ query, variables }) {
return (await TestClient).query(query, {
variables,
});
};

export const client = createClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher,
scalars: scalarsEnumsHash,
fetchOptions: {
fetcher: queryFetcher,
},
});

const { query, mutation, mutate, subscription, resolved, refetch, track } =
client;

export { query, mutation, mutate, subscription, resolved, refetch, track };

export * from './schema.generated';
export { query, mutation, mutate, subscription, resolved, refetch, track };
19 changes: 5 additions & 14 deletions packages/cli/src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
export { program } from 'commander';

export { cosmiconfig, Loader } from 'cosmiconfig';

export { codegen } from '@graphql-codegen/core';

export * as typescriptPlugin from '@graphql-codegen/typescript';

export { printSchemaWithDirectives } from '@graphql-tools/utils';

export { schemaFromExecutor, wrapSchema } from '@graphql-tools/wrap';
export { program } from 'commander';
export { cosmiconfig, Loader } from 'cosmiconfig';
export { default as fg } from 'fast-glob';
export { default as sortBy } from 'lodash-es/sortBy.js';

export { introspectSchema, wrapSchema } from '@graphql-tools/wrap';

export { default as prettier, Options as PrettierOptions } from 'prettier';

export { default as mkdirp } from 'mkdirp';

export { default as fg } from 'fast-glob';
export { default as prettier, Options as PrettierOptions } from 'prettier';
45 changes: 16 additions & 29 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,23 +733,6 @@ export async function generate(
return acum;
}, '');

const objectTypesEntries = deps.sortBy(
Array.from(objectTypeTSTypes.entries()),
(v) => v[0]
);

typescriptTypes += `
export interface SchemaObjectTypes {
${objectTypesEntries.reduce((acum, [typeName]) => {
acum += `${typeName}:${typeName};`;
return acum;
}, '')}
}
export type SchemaObjectTypesNames = ${objectTypesEntries
.map(([key]) => `"${key}"`)
.join(' | ')};
`;

if (unionsAndInterfacesObjectTypesMap.size) {
typescriptTypes += `
${deps
Expand Down Expand Up @@ -798,8 +781,8 @@ export async function generate(
${
isJavascriptOutput
? typeDoc('import("gqty").QueryFetcher') + 'const queryFetcher'
: 'const queryFetcher : QueryFetcher'
} = async function (query, variables, fetchOptions) {
: 'const queryFetcher: QueryFetcher'
} = async function ({ query, variables, operationName }, fetchOptions) {
// Modify "${endpoint}" if needed
const response = await fetch("${endpoint}", {
method: "POST",
Expand All @@ -809,6 +792,7 @@ export async function generate(
body: JSON.stringify({
query,
variables,
operationName,
}),
mode: "cors",
...fetchOptions
Expand Down Expand Up @@ -977,15 +961,15 @@ export const generatedSchema = {${generatedSchemaCodeString}};
${react ? `import { createReactClient } from "@gqty/react"` : ''}
${
subscriptions
? `import { createSubscriptionsClient } from "@gqty/subscriptions"`
? `import { createClient as createSubscriptionsClient } from "graphql-ws"`
: ''
}
${isJavascriptOutput ? '' : 'import type { QueryFetcher } from "gqty";'}
import { createClient } from "gqty";
${
isJavascriptOutput
? ''
: 'import type { GeneratedSchema, SchemaObjectTypes, SchemaObjectTypesNames } from "./schema.generated";'
: 'import type { GeneratedSchema } from "./schema.generated";'
}
import { generatedSchema, scalarsEnumsHash } from "./schema.generated";
Expand Down Expand Up @@ -1015,19 +999,22 @@ export const generatedSchema = {${generatedSchemaCodeString}};
'import("gqty").GQtyClient<import("./schema.generated").GeneratedSchema>'
)}export const client = createClient({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher
${subscriptions ? ', subscriptionsClient' : ''}
scalars: scalarsEnumsHash,
fetchOptions: {
fetcher: queryFetcher,
${subscriptions ? 'subscriber: subscriptionsClient' : ''}
},
});`
: `export const client = createClient<GeneratedSchema, SchemaObjectTypesNames, SchemaObjectTypes>({
: `export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher
${subscriptions ? ', subscriptionsClient' : ''}
scalars: scalarsEnumsHash,
fetchOptions:{
fetcher: queryFetcher,
${subscriptions ? 'subscriber: subscriptionsClient' : ''}
},
});`
}
const { query, mutation, mutate, subscription, resolved, refetch, track } = client;
export { query, mutation, mutate, subscription, resolved, refetch, track };
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const getRemoteSchema = async (
};

const schema = deps.wrapSchema({
schema: await deps.introspectSchema(executor, {
schema: await deps.schemaFromExecutor(executor, {
endpoint,
}),
executor,
Expand Down
Loading

0 comments on commit 7285e00

Please sign in to comment.