Skip to content

Commit

Permalink
flow plugin: fixes issues with scalars resolvers and scalars imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Dec 19, 2018
1 parent ab0f22e commit ad07eaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/plugins/flow-resolvers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ScalarsMap } from 'graphql-codegen-flow';
import { DocumentFile, GraphQLSchema, PluginFunction } from 'graphql-codegen-core';
import { parse, printSchema, visit } from 'graphql';
import { isScalarType, parse, printSchema, visit } from 'graphql';
import { FlowResolversVisitor } from './visitor';

export interface FlowResolversPluginConfig {
Expand All @@ -17,14 +17,16 @@ export const plugin: PluginFunction<FlowResolversPluginConfig> = (
config: FlowResolversPluginConfig
) => {
const imports = ['GraphQLResolveInfo'];
const hasScalars = false;
const hasScalars = Object.values(schema.getTypeMap())
.filter(t => t.astNode)
.some(isScalarType);

if (hasScalars) {
imports.push('GraphQLScalarType', 'GraphQLScalarTypeConfig');
imports.push('GraphQLScalarTypeConfig');
}

const result = `
import { ${imports.join(', ')} } from 'graphql';
import { ${imports.join(', ')} } from 'graphql/type';
export type Resolver<Result, Parent = {}, Context = {}, Args = {}> = (
parent?: Parent,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/flow-resolvers/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class FlowResolversVisitor implements BasicFlowVisitor {

return new DeclarationBlock()
.export()
.asKind('type')
.asKind('interface')
.withName(this.convertName(node.name + 'ScalarConfig'), ` extends GraphQLScalarTypeConfig<${baseName}, any>`)
.withBlock(indent(`name: '${node.name}'`)).string;
};
Expand Down
27 changes: 20 additions & 7 deletions packages/plugins/flow-resolvers/tests/flow-resolvers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'graphql-codegen-core/dist/testing';
import { FlowResolversVisitor } from '../src/visitor';
import { parse, visit, printSchema } from 'graphql';
import { validateFlow } from './validate-flow';
import { makeExecutableSchema } from 'graphql-tools';
import { plugin } from '../src';
import { typeDefs } from '../../../graphql-codegen-cli/tests/test-files/schema-dir/schema';

describe('Flow Resolvers Plugin', () => {
const schema = makeExecutableSchema({
Expand Down Expand Up @@ -53,9 +51,9 @@ describe('Flow Resolvers Plugin', () => {
bar?: Resolver<string, ParentType, Context>,
}
export type MyScalarScalarConfig extends GraphQLScalarTypeConfig<MyScalar, any> = {
export interface MyScalarScalarConfig extends GraphQLScalarTypeConfig<MyScalar, any> {
name: 'MyScalar'
};
}
export interface MyTypeResolvers<Context = any, ParentType = MyType> {
foo?: Resolver<string, ParentType, Context>,
Expand Down Expand Up @@ -84,6 +82,21 @@ describe('Flow Resolvers Plugin', () => {
}
`);
});

it('Should generate the correct imports when schema has scalars', () => {
const result = plugin(makeExecutableSchema({ typeDefs: `scalar MyScalar` }), [], {}, { outputFile: '' });

expect(result).toBeSimilarStringTo(`import { GraphQLResolveInfo, GraphQLScalarTypeConfig } from 'graphql/type';`);
});

it('Should generate the correct imports when schema has no scalars', () => {
const result = plugin(makeExecutableSchema({ typeDefs: `type MyType { f: String }` }), [], {}, { outputFile: '' });

expect(result).not.toBeSimilarStringTo(
`import { GraphQLResolveInfo, GraphQLScalarTypeConfig } from 'graphql/type';`
);
});

it('Should generate basic type resolvers with mapping', () => {
const result = plugin(
schema,
Expand All @@ -104,9 +117,9 @@ describe('Flow Resolvers Plugin', () => {
bar?: Resolver<string, ParentType, Context>,
}
export type MyScalarScalarConfig extends GraphQLScalarTypeConfig<MyScalar, any> = {
export interface MyScalarScalarConfig extends GraphQLScalarTypeConfig<MyScalar, any> {
name: 'MyScalar'
};
}
export interface MyTypeResolvers<Context = any, ParentType = MyType> {
foo?: Resolver<string, ParentType, Context>,
Expand Down

0 comments on commit ad07eaf

Please sign in to comment.