-
-
Notifications
You must be signed in to change notification settings - Fork 796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor GitLoader #1796
Merged
Merged
Refactor GitLoader #1796
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,11 +34,9 @@ export type GitLoaderOptions = SingleFileOptions & { | |
/** | ||
* Additional options to pass to `graphql-tag-pluck` | ||
*/ | ||
pluckConfig: GraphQLTagPluckOptions; | ||
pluckConfig?: GraphQLTagPluckOptions; | ||
}; | ||
|
||
const createInvalidExtensionError = (path: string) => new Error(`Invalid file extension: ${path}`); | ||
|
||
/** | ||
* This loader loads a file from git. | ||
* | ||
|
@@ -72,7 +70,10 @@ export class GitLoader implements UniversalLoader { | |
|
||
const rawSDL = await gqlPluckFromCodeString(pointer, content, options.pluckConfig); | ||
|
||
return ensureSource({ rawSDL, pointer, path }); | ||
return { | ||
location: pointer, | ||
rawSDL, | ||
}; | ||
} | ||
|
||
loadSync(pointer: string, options: GitLoaderOptions) { | ||
|
@@ -86,17 +87,9 @@ export class GitLoader implements UniversalLoader { | |
|
||
const rawSDL = gqlPluckFromCodeStringSync(pointer, content, options.pluckConfig); | ||
|
||
return ensureSource({ rawSDL, pointer, path }); | ||
} | ||
} | ||
|
||
function ensureSource({ rawSDL, pointer, path }: { rawSDL: string; pointer: string; path: string }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping |
||
if (rawSDL) { | ||
return { | ||
location: pointer, | ||
rawSDL, | ||
}; | ||
} | ||
|
||
throw createInvalidExtensionError(path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
import { Source } from '@graphql-tools/utils'; | ||
|
||
import { GitLoader } from '../src'; | ||
import { runTests } from '../../../testing/utils'; | ||
|
||
describe('GitLoader', () => { | ||
const loader = new GitLoader(); | ||
const lastCommit = execSync('git rev-parse HEAD', { encoding: 'utf-8' }).replace(/\n/g, ''); | ||
const getPointer = (fileName: string) => { | ||
return `git:${lastCommit}:packages/loaders/git/tests/test-files/${fileName}`; | ||
}; | ||
|
||
describe('loaderId', () => { | ||
it('should return a loader id', () => { | ||
expect(loader.loaderId()).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('canLoad', () => { | ||
runTests({ | ||
async: loader.canLoad.bind(loader), | ||
sync: loader.canLoadSync.bind(loader), | ||
})(canLoad => { | ||
it('should return true for a valid pointer', async () => { | ||
expect(canLoad(getPointer('some-file.graphql'))).resolves.toBe(true); | ||
}); | ||
|
||
it('should return false if pointer does not begin with "git:"', async () => { | ||
expect(canLoad(getPointer('some-file.graphql').substring(4))).resolves.toBe(false); | ||
}); | ||
|
||
it('should return false if pointer is not a string', async () => { | ||
expect(canLoad(42 as any)).resolves.toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('load', () => { | ||
runTests({ | ||
async: loader.load.bind(loader), | ||
sync: loader.loadSync.bind(loader), | ||
})(load => { | ||
it('should load document from a .graphql file', async () => { | ||
const result: Source = await load(getPointer('type-defs.graphql'), {}); | ||
expect(result.document).toBeDefined(); | ||
}); | ||
|
||
it('should load introspection data from a .json file', async () => { | ||
const result: Source = await load(getPointer('introspection.json'), {}); | ||
expect(result.schema).toBeDefined(); | ||
}); | ||
|
||
it('should load type definitions from a .json file', async () => { | ||
const result: Source = await load(getPointer('type-defs.json'), {}); | ||
expect(result.document).toBeDefined(); | ||
}); | ||
|
||
it('should load type definitions from a pluckable file', async () => { | ||
const result: Source = await load(getPointer('pluckable.ts'), {}); | ||
expect(result.rawSDL).toBeDefined(); | ||
}); | ||
|
||
it('should throw when pointer is malformed', async () => { | ||
await expect(load(getPointer('foo:graphql'), {})).rejects.toThrowError( | ||
'Schema pointer should match "git:branchName:path/to/file"' | ||
); | ||
}); | ||
|
||
it('should throw when the file does not exist', async () => { | ||
await expect(load(getPointer('wrong-filename.graphql'), {})).rejects.toThrowError( | ||
'Unable to load file from git' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type Query { | ||
hello: String | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making this optional since
graphql-tag-pluck
doesn't actually require an options object to be passed in.