Skip to content

Commit

Permalink
Fix for enums and preResolveTypes (#2284)
Browse files Browse the repository at this point in the history
* fix for enums and preResolveTypes

* minor fix
  • Loading branch information
dotansimha committed Aug 4, 2019
1 parent 2369c1d commit 0259e01
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,17 @@ export class SelectionSetToObject {
return fields.map(field => {
const fieldObj = schemaType.getFields()[field];
const baseType = getBaseType(fieldObj.type);
const typeToUse = this._scalars[baseType.name] || baseType.name;
let typeToUse = baseType.name;

if (isEnumType(baseType)) {
typeToUse = baseType
.getValues()
.map(v => `'${v.value}'`)
.join(' | ');
} else if (this._scalars[baseType.name]) {
typeToUse = this._scalars[baseType.name];
}

const wrappedType = this.wrapTypeWithModifiers(typeToUse, fieldObj.type as GraphQLObjectType);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ describe('TypeScript Operations Plugin', () => {
}
`);

const validate = async (content: Types.PluginOutput, config: any = {}, pluginSchema = schema) => validateTs(mergeOutputs([await tsPlugin(pluginSchema, [], config, { outputFile: '' }), content]));
const validate = async (content: Types.PluginOutput, config: any = {}, pluginSchema = schema) => {
const m = mergeOutputs([await tsPlugin(pluginSchema, [], config, { outputFile: '' }), content]);

await validateTs(m);

return m;
};

describe('Config', () => {
it('Should not generate "export" when noExport is set to true', async () => {
Expand Down Expand Up @@ -951,6 +957,49 @@ describe('TypeScript Operations Plugin', () => {
validate(result, config, gitHuntSchema);
});

it('Should produce valid output with preResolveTypes=true and enums', async () => {
const ast = parse(/* GraphQL */ `
query test {
info {
...information
}
}
fragment information on Information {
entries {
id
value
}
}
`);
const testSchema = buildSchema(/* GraphQL */ `
type Information {
entries: [Information_Entry!]!
}
enum Information_EntryType {
NAME
ADDRESS
}
type Information_Entry {
id: Information_EntryType!
value: String
}
type Query {
info: Information
}
`);
const config = { preResolveTypes: true };
const result = await plugin(testSchema, [{ filePath: 'test-file.ts', content: ast }], config, {
outputFile: '',
});

const o = await validate(result, config, testSchema);
expect(o).toContain(`__typename?: 'Information_Entry', id: 'NAME' | 'ADDRESS',`);
});

it('Should build a basic selection set based on basic query', async () => {
const ast = parse(/* GraphQL */ `
query dummy {
Expand Down

0 comments on commit 0259e01

Please sign in to comment.