Skip to content

Commit

Permalink
Accept absolute config file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
hasparus committed Oct 17, 2020
1 parent 1d67a35 commit 7a14dfe
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/lib/config.ts
@@ -1,5 +1,5 @@
import { Types } from '@graphql-codegen/plugin-helpers';
import { join as pathJoin } from 'path';
import { resolve as pathResolve } from 'path';
import { parse as parseYaml } from 'yaml';
import { env } from 'string-env-interpolation';
import { DEFAULT_CONFIG_FILENAME } from './consts';
Expand Down Expand Up @@ -59,7 +59,7 @@ export function buildConfig(raw: UserConfigTypes): ConfigTypes {
}

export const getConfigPath = (cwd: string, configFilePath?: string) =>
pathJoin(cwd, configFilePath || DEFAULT_CONFIG_FILENAME);
pathResolve(cwd, configFilePath || DEFAULT_CONFIG_FILENAME);

const getConfigFromContent = (content: string): [ConfigTypes, string] => {
content = env(content);
Expand Down
66 changes: 44 additions & 22 deletions test/__snapshots__/loader.test.ts.snap
@@ -1,27 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`graphql-let/loader accepts config path in options.configFile 2`] = `
"import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
export const GetFruitsDocument = gql\`
query GetFruits {
fruits {
id
name
flavor
}
}
\`;
export function useGetFruitsQuery(baseOptions) {
return Apollo.useQuery(GetFruitsDocument, baseOptions);
}
export function useGetFruitsLazyQuery(baseOptions) {
return Apollo.useLazyQuery(GetFruitsDocument, baseOptions);
}"
`;

exports[`graphql-let/loader generates .tsx and .d.ts 1`] = `
"import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
Expand Down Expand Up @@ -57,3 +35,47 @@ export function useViewerLazyQuery(baseOptions) {
return Apollo.useLazyQuery(ViewerDocument, baseOptions);
}"
`;

exports[`graphql-let/loader options accept absolute config path in options.configFile 2`] = `
"import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
export const GetFruitsDocument = gql\`
query GetFruits {
fruits {
id
name
flavor
}
}
\`;
export function useGetFruitsQuery(baseOptions) {
return Apollo.useQuery(GetFruitsDocument, baseOptions);
}
export function useGetFruitsLazyQuery(baseOptions) {
return Apollo.useLazyQuery(GetFruitsDocument, baseOptions);
}"
`;

exports[`graphql-let/loader options accept relative config path in options.configFile 2`] = `
"import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
export const GetFruitsDocument = gql\`
query GetFruits {
fruits {
id
name
flavor
}
}
\`;
export function useGetFruitsQuery(baseOptions) {
return Apollo.useQuery(GetFruitsDocument, baseOptions);
}
export function useGetFruitsLazyQuery(baseOptions) {
return Apollo.useLazyQuery(GetFruitsDocument, baseOptions);
}"
`;
96 changes: 56 additions & 40 deletions test/loader.test.ts
Expand Up @@ -66,49 +66,65 @@ describe('graphql-let/loader', () => {
strictEqual(globResults.length, 2);
});

test('accepts config path in options.configFile', async () => {
const stats = await compiler(
pathJoin(fixturePath2, 'packages/app'),
'src/index.ts',
'web',
{ configFile: '../../config/.graphql-let.yml' },
);

const modules = stats
.toJson()
.modules?.flatMap((m) => m.modules)
?.filter(Boolean);

ok(modules);
expect(modules.map((m) => m.name)).toMatchInlineSnapshot(`
Array [
"./src/index.ts",
"./src/fruits.graphql",
]
`);

const generated = modules.find((m) => m?.name === './src/fruits.graphql');

ok(generated);
describe('options', () => {
async function acceptsConfigPathInOptionsConfigFile(
configFilePath: string,
) {
const stats = await compiler(
pathJoin(fixturePath2, 'packages/app'),
'src/index.ts',
'web',
{ configFile: configFilePath },
);

const modules = stats
.toJson()
.modules?.flatMap((m) => m.modules)
?.filter(Boolean);

ok(modules);
expect(modules.map((m) => m.name)).toMatchInlineSnapshot(`
Array [
"./src/index.ts",
"./src/fruits.graphql",
]
`);

const generated = modules.find((m) => m?.name === './src/fruits.graphql');

ok(generated);

await waitOn({
resources: [
`${fixturePath2}/packages/app/__generated__/src/fruits.graphql.tsx`,
],
});

expect(generated.source).toContain('export function useGetFruitsQuery');
expect(
generated.source?.replace(/\/\*[\s\S]*?\*\//g, ''),
).toMatchSnapshot();

await Promise.all([
unlink(
`${fixturePath2}/packages/app/__generated__/src/fruits.graphql.tsx`,
),
unlink(`${fixturePath2}/packages/app/src/fruits.graphql.d.ts`),
]).catch(() => {
/* discard error */
});
}

await waitOn({
resources: [
`${fixturePath2}/packages/app/__generated__/src/fruits.graphql.tsx`,
],
test('accept relative config path in options.configFile', async () => {
await acceptsConfigPathInOptionsConfigFile(
'../../config/.graphql-let.yml',
);
});

expect(generated.source).toContain('export function useGetFruitsQuery');
expect(
generated.source?.replace(/\/\*[\s\S]*?\*\//g, ''),
).toMatchSnapshot();

await Promise.all([
unlink(
`${fixturePath2}/packages/app/__generated__/src/fruits.graphql.tsx`,
),
unlink(`${fixturePath2}/packages/app/src/fruits.graphql.d.ts`),
]).catch(() => {
/* discard error */
test('accept absolute config path in options.configFile', async () => {
await acceptsConfigPathInOptionsConfigFile(
require.resolve('./__fixtures/loader/monorepo/config/.graphql-let.yml'),
);
});
});
});

0 comments on commit 7a14dfe

Please sign in to comment.