Skip to content

Commit

Permalink
Return source for each plucked string (#3407)
Browse files Browse the repository at this point in the history
* Return source for each plucked string

* fix(code-file-loader): return multiple sources for each plucked SDL block
  • Loading branch information
ardatan authored Aug 19, 2021
1 parent b3c4afd commit b64ff64
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-sheep-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/code-file-loader': patch
---

fix(code-file-loader): return multiple sources for each plucked SDL block
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('documentsFromGlob', () => {
loaders: [new CodeFileLoader()]
});

expect(result).toHaveLength(1);
expect(result).toHaveLength(2);
});

test(`Should load GraphQL operations that match custom settings`, async () => {
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('documentsFromGlob', () => {
});
// 1 from 2.graphql
// 2 from tags.js
expect(result.length).toEqual(2);
expect(result.length).toEqual(3);
})
})
});
49 changes: 19 additions & 30 deletions packages/loaders/code-file/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { GlobbyOptions } from 'globby';

import {
isSchema,
GraphQLSchema,
DocumentNode,
parse,
concatAST,
Source as GraphQLSource,
ParseOptions,
} from 'graphql';
import { isSchema, GraphQLSchema, DocumentNode, parse } from 'graphql';
import {
Source,
asArray,
Expand Down Expand Up @@ -170,12 +162,11 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
const sources = await gqlPluckFromCodeString(normalizedFilePath, content, options.pluckConfig);

if (sources.length) {
const mergedDocument = flattenSources(sources, options);
const mergedSource = {
document: mergedDocument,
return sources.map(source => ({
rawSDL: source.body,
document: parse(source),
location,
};
return [mergedSource];
}));
}
} catch (e) {
if (env['DEBUG']) {
Expand All @@ -192,10 +183,12 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

const loaded = await tryToLoadFromExport(normalizedFilePath);
const source = resolveSource(location, loaded, options);
const sources = asArray(loaded)
.map(value => resolveSource(location, value, options))
.filter(Boolean);

if (source) {
return [source];
if (sources.length) {
return sources as Source[];
}
} catch (e) {
errors.push(e);
Expand Down Expand Up @@ -224,12 +217,11 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
const sources = gqlPluckFromCodeStringSync(normalizedFilePath, content, options.pluckConfig);

if (sources.length) {
const mergedDocument = flattenSources(sources, options);
const mergedSource = {
document: mergedDocument,
return sources.map(source => ({
rawSDL: source.body,
document: parse(source),
location,
};
return [mergedSource];
}));
}
} catch (e) {
if (env['DEBUG']) {
Expand All @@ -249,10 +241,12 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}

const loaded = tryToLoadFromExportSync(normalizedFilePath);
const source = resolveSource(location, loaded, options);
const sources = asArray(loaded)
.map(value => resolveSource(location, value, options))
.filter(Boolean);

if (source) {
return [source];
if (sources.length) {
return sources as Source[];
}
} catch (e) {
errors.push(e);
Expand All @@ -267,11 +261,6 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
}
}

function flattenSources(sources: GraphQLSource[], options: ParseOptions) {
const documents = sources.map(source => parse(source, options));
return concatAST(documents);
}

function resolveSource(
pointer: string,
value: GraphQLSchema | DocumentNode | string | null,
Expand Down
127 changes: 70 additions & 57 deletions packages/loaders/code-file/tests/load-from-code-file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
import * as path from 'path';
import { CodeFileLoader } from '../src';
import { parse, print } from 'graphql';
import { parse } from 'graphql';

describe('loadFromCodeFile', () => {
const loader = new CodeFileLoader();
Expand Down Expand Up @@ -64,18 +64,17 @@ describe('loadFromCodeFile', () => {
expect(doc?.kind).toEqual('Document');
});


it('does not try to load single file it cannot load', async () => {
const loader = new CodeFileLoader({
pluckConfig: {
skipIndent: true
}
})
skipIndent: true,
},
});
const loaded = await loader.load('./test-files/other.graphql', {
cwd: __dirname,
});
expect(loaded).toEqual([])
})
expect(loaded).toEqual([]);
});
});

describe('loadFromCodeFileSync', () => {
Expand Down Expand Up @@ -134,74 +133,88 @@ describe('loadFromCodeFileSync', () => {
skipIndent: true,
},
});
expect(loadedSources?.length).toEqual(1);
const loadedSource = loadedSources![0];
expect(loadedSource.document).toBeDefined();
const rawSDL = print(loadedSource.document!);
expect(rawSDL).toMatchInlineSnapshot(`
"query Foo {
Tweets {
id
}
}
fragment Lel on Tweet {
id
body
}
query Bar {
Tweets {
...Lel
}
}
"
`);
expect(loadedSources?.length).toEqual(3);
expect(loadedSources![0].rawSDL).toBeDefined();
expect(loadedSources![0].rawSDL).toMatchInlineSnapshot(`
"
query Foo {
Tweets {
id
}
}
"
`);
expect(loadedSources![1].rawSDL).toBeDefined();
expect(loadedSources![1].rawSDL).toMatchInlineSnapshot(`
"
fragment Lel on Tweet {
id
body
}
"
`);
expect(loadedSources![2].rawSDL).toBeDefined();
expect(loadedSources![2].rawSDL).toMatchInlineSnapshot(`
"
query Bar {
Tweets {
...Lel
}
}
"
`);
});

it('can inherit config options from constructor', () => {
const loader = new CodeFileLoader({
pluckConfig: {
skipIndent: true
}
})
skipIndent: true,
},
});
const loadedSources = loader.loadSync('./test-files/multiple-from-file.ts', {
cwd: __dirname,
});
expect(loadedSources?.length).toEqual(1);
const loadedSource = loadedSources![0];
expect(loadedSource.document).toBeDefined();
const rawSDL = print(loadedSource.document!);
expect(rawSDL).toMatchInlineSnapshot(`
"query Foo {
Tweets {
expect(loadedSources?.length).toEqual(3);
expect(loadedSources![0].rawSDL).toBeDefined();
expect(loadedSources![0].rawSDL).toMatchInlineSnapshot(`
"
query Foo {
Tweets {
id
}
}
"
`);
expect(loadedSources![1].rawSDL).toBeDefined();
expect(loadedSources![1].rawSDL).toMatchInlineSnapshot(`
"
fragment Lel on Tweet {
id
body
}
}
fragment Lel on Tweet {
id
body
}
query Bar {
Tweets {
...Lel
"
`);
expect(loadedSources![2].rawSDL).toBeDefined();
expect(loadedSources![2].rawSDL).toMatchInlineSnapshot(`
"
query Bar {
Tweets {
...Lel
}
}
}
"
`);
})
});

it('does not try to load single file it cannot load', async () => {
const loader = new CodeFileLoader({
pluckConfig: {
skipIndent: true
}
})
skipIndent: true,
},
});
const loaded = loader.loadSync('./test-files/other.graphql', {
cwd: __dirname,
});
expect(loaded).toEqual([])
})
expect(loaded).toEqual([]);
});
});

1 comment on commit b64ff64

@vercel
Copy link

@vercel vercel bot commented on b64ff64 Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.