From 83c2da58f9fa088f9f44eca4a424bfe16120b72a Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU <20847995+ardatan@users.noreply.github.com> Date: Thu, 15 Aug 2019 09:57:29 -0400 Subject: [PATCH] Able to provide custom fetch implementation (#2369) * Able to provide custom fetch implementation * Fix tests --- packages/graphql-codegen-cli/src/load.ts | 6 ++++++ .../tests/__mocks__/some-fetch.js | 14 ++++++++++++++ .../graphql-codegen-cli/tests/codegen.spec.ts | 17 ++++++++++++++++- packages/utils/plugins-helpers/src/types.ts | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/graphql-codegen-cli/tests/__mocks__/some-fetch.js diff --git a/packages/graphql-codegen-cli/src/load.ts b/packages/graphql-codegen-cli/src/load.ts index 1cec28be782..4951fe84fba 100644 --- a/packages/graphql-codegen-cli/src/load.ts +++ b/packages/graphql-codegen-cli/src/load.ts @@ -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); diff --git a/packages/graphql-codegen-cli/tests/__mocks__/some-fetch.js b/packages/graphql-codegen-cli/tests/__mocks__/some-fetch.js new file mode 100644 index 00000000000..5c70acef525 --- /dev/null +++ b/packages/graphql-codegen-cli/tests/__mocks__/some-fetch.js @@ -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); + }, + }; + }, +}; diff --git a/packages/graphql-codegen-cli/tests/codegen.spec.ts b/packages/graphql-codegen-cli/tests/codegen.spec.ts index 8f6e525b51a..b0c54134ca5 100644 --- a/packages/graphql-codegen-cli/tests/codegen.spec.ts +++ b/packages/graphql-codegen-cli/tests/codegen.spec.ts @@ -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 () => { @@ -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(); + }); }); diff --git a/packages/utils/plugins-helpers/src/types.ts b/packages/utils/plugins-helpers/src/types.ts index 79f010390b0..b73f370388d 100644 --- a/packages/utils/plugins-helpers/src/types.ts +++ b/packages/utils/plugins-helpers/src/types.ts @@ -92,6 +92,7 @@ export namespace Types { export interface Config { schema?: InstanceOrArray; require?: RequireExtension; + customFetch?: string; documents?: InstanceOrArray; config?: { [key: string]: any }; generates: { [output: string]: OutputConfig | ConfiguredOutput };