Skip to content

Commit

Permalink
Able to provide custom fetch implementation (#2369)
Browse files Browse the repository at this point in the history
* Able to provide custom fetch implementation

* Fix tests
  • Loading branch information
ardatan authored and dotansimha committed Aug 15, 2019
1 parent b8b66b3 commit 83c2da5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/graphql-codegen-cli/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export const loadSchema = async (schemaDef: Types.Schema, config: Types.Config):
options.tagPluck = config.pluckConfig;
}

if (config.customFetch) {
const customFetchStr = config.customFetch;
const [moduleName, fetchFnName] = customFetchStr.split('#');
options.fetch = await import(moduleName).then(module => (fetchFnName ? module[fetchFnName] : module));
}

const docs = (await loadTypedefs(pointToSchema, options)).map(({ content }) => content);

return mergeTypeDefs(docs);
Expand Down
14 changes: 14 additions & 0 deletions packages/graphql-codegen-cli/tests/__mocks__/some-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { readFileSync } = require('fs');
const { buildSchema, graphql, introspectionQuery } = require('graphql');
module.exports = {
someFetchFn: async (url, options) => {
const schemaFile = readFileSync(__dirname + '/../test-documents/schema.graphql', 'utf8');
const schema = buildSchema(schemaFile);
return {
json() {
global.CUSTOM_FETCH_FN_CALLED = true;
return graphql(schema, introspectionQuery);
},
};
},
};
17 changes: 16 additions & 1 deletion packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Types } from '@graphql-codegen/plugin-helpers';
import '@graphql-codegen/testing';
import { GraphQLObjectType, buildSchema, buildASTSchema, parse, print } from 'graphql';
import { GraphQLObjectType, buildSchema, buildASTSchema, parse, print, buildClientSchema, graphql, getIntrospectionQuery } from 'graphql';
import { mergeSchemas } from '@graphql-codegen/core';
import { executeCodegen } from '../src';
import { readFileSync } from 'fs';

const SHOULD_NOT_THROW_STRING = 'SHOULD_NOT_THROW';
const SIMPLE_TEST_SCHEMA = `type MyType { f: String } type Query { f: String }`;

jest.mock('some-fetch');

describe('Codegen Executor', () => {
describe('Generator General Options', () => {
it('Should output the correct filenames', async () => {
Expand Down Expand Up @@ -870,4 +873,16 @@ describe('Codegen Executor', () => {
}
});
});

it('should load schema with custom fetch', async () => {
await executeCodegen({
schema: ['http://www.dummyschema.com/graphql'],
customFetch: 'some-fetch#someFetchFn',
documents: ['./tests/test-documents/valid.graphql'],
generates: {
'out1.ts': ['typescript'],
},
});
expect(global['CUSTOM_FETCH_FN_CALLED']).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions packages/utils/plugins-helpers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export namespace Types {
export interface Config {
schema?: InstanceOrArray<Schema>;
require?: RequireExtension;
customFetch?: string;
documents?: InstanceOrArray<OperationDocument>;
config?: { [key: string]: any };
generates: { [output: string]: OutputConfig | ConfiguredOutput };
Expand Down

0 comments on commit 83c2da5

Please sign in to comment.