-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Flow support - spread operator #2550
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
@@ -1,7 +1,66 @@ | ||
import { SelectionSetToObject, PrimitiveField, PrimitiveAliasedFields, LinkField, ConvertNameFn, NormalizedScalarsMap, LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; | ||
import { GraphQLObjectType, GraphQLNonNull, GraphQLList, isNonNullType, isListType, GraphQLSchema, GraphQLNamedType, SelectionSetNode } from 'graphql'; | ||
import { ConvertNameFn, getBaseType, LinkField, LoadedFragment, NormalizedScalarsMap, PrimitiveAliasedFields, PrimitiveField, SelectionSetToObject } from '@graphql-codegen/visitor-plugin-common'; | ||
import { | ||
FieldNode, | ||
FragmentSpreadNode, | ||
GraphQLField, | ||
GraphQLList, | ||
GraphQLNamedType, | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLOutputType, | ||
GraphQLSchema, | ||
isListType, | ||
isNonNullType, | ||
SchemaMetaFieldDef, | ||
SelectionNode, | ||
SelectionSetNode, | ||
TypeMetaFieldDef, | ||
} from 'graphql'; | ||
import { FlowDocumentsParsedConfig } from './visitor'; | ||
|
||
const getFieldNodeNameValue = (node: FieldNode): string => { | ||
return (node.alias || node.name).value; | ||
}; | ||
|
||
const metadataFieldMap: Record<string, GraphQLField<any, any>> = { | ||
__schema: SchemaMetaFieldDef, | ||
__type: TypeMetaFieldDef, | ||
}; | ||
|
||
const mergeSelectionSets = (selectionSet1: SelectionSetNode, selectionSet2: SelectionSetNode) => { | ||
const newSelections = [...selectionSet1.selections]; | ||
|
||
for (const selection2 of selectionSet2.selections) { | ||
if (selection2.kind === 'FragmentSpread') { | ||
newSelections.push(selection2); | ||
continue; | ||
} | ||
|
||
if (selection2.kind !== 'Field') { | ||
throw new TypeError('Invalid state.'); | ||
} | ||
|
||
const match = newSelections.find(selection1 => selection1.kind === 'Field' && getFieldNodeNameValue(selection1) === getFieldNodeNameValue(selection2)); | ||
|
||
if (match) { | ||
// recursively merge all selection sets | ||
if (match.kind === 'Field' && match.selectionSet && selection2.selectionSet) { | ||
mergeSelectionSets(match.selectionSet, selection2.selectionSet); | ||
} | ||
continue; | ||
} | ||
|
||
newSelections.push(selection2); | ||
} | ||
|
||
// replace existing selections | ||
selectionSet1.selections = newSelections; | ||
}; | ||
|
||
function isMetadataFieldName(name: string) { | ||
return ['__schema', '__type'].includes(name); | ||
} | ||
|
||
export class FlowSelectionSetToObject extends SelectionSetToObject { | ||
constructor( | ||
_scalars: NormalizedScalarsMap, | ||
|
@@ -31,6 +90,125 @@ export class FlowSelectionSetToObject extends SelectionSetToObject { | |
); | ||
} | ||
|
||
protected buildSelectionSetString(parentSchemaType: GraphQLObjectType, selectionNodes: SelectionNode[]) { | ||
const primitiveFields = new Map<string, FieldNode>(); | ||
const primitiveAliasFields = new Map<string, FieldNode>(); | ||
const linkFieldSelectionSets = new Map< | ||
string, | ||
{ | ||
selectedFieldType: GraphQLOutputType; | ||
field: FieldNode; | ||
} | ||
>(); | ||
const fragmentSpreadSelectionSets = new Map<string, FragmentSpreadNode>(); | ||
let requireTypename = false; | ||
|
||
for (const selectionNode of selectionNodes) { | ||
if (selectionNode.kind === 'Field') { | ||
if (!selectionNode.selectionSet) { | ||
if (selectionNode.alias) { | ||
primitiveAliasFields.set(selectionNode.alias.value, selectionNode); | ||
} else if (selectionNode.name.value === '__typename') { | ||
requireTypename = true; | ||
} else { | ||
primitiveFields.set(selectionNode.name.value, selectionNode); | ||
} | ||
} else { | ||
let selectedField: GraphQLField<any, any, any> = null; | ||
|
||
const fields = parentSchemaType.getFields(); | ||
selectedField = fields[selectionNode.name.value]; | ||
|
||
if (isMetadataFieldName(selectionNode.name.value)) { | ||
selectedField = metadataFieldMap[selectionNode.name.value]; | ||
} | ||
|
||
if (!selectedField) { | ||
throw new TypeError(`Could not find field type. ${parentSchemaType}.${selectionNode.name.value}`); | ||
} | ||
|
||
const fieldName = getFieldNodeNameValue(selectionNode); | ||
let linkFieldNode = linkFieldSelectionSets.get(fieldName); | ||
if (!linkFieldNode) { | ||
linkFieldNode = { | ||
selectedFieldType: selectedField.type, | ||
field: selectionNode, | ||
}; | ||
linkFieldSelectionSets.set(fieldName, linkFieldNode); | ||
} else { | ||
mergeSelectionSets(linkFieldNode.field.selectionSet, selectionNode.selectionSet); | ||
} | ||
} | ||
} else if (selectionNode.kind === 'FragmentSpread') { | ||
fragmentSpreadSelectionSets.set(selectionNode.name.value, selectionNode); | ||
} | ||
} | ||
|
||
const linkFields: LinkField[] = []; | ||
for (const { field, selectedFieldType } of linkFieldSelectionSets.values()) { | ||
const realSelectedFieldType = getBaseType(selectedFieldType as any); | ||
const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet); | ||
|
||
linkFields.push({ | ||
alias: field.alias ? field.alias.value : undefined, | ||
name: field.name.value, | ||
type: realSelectedFieldType.name, | ||
selectionSet: this.wrapTypeWithModifiers(selectionSet.string.split(`\n`).join(`\n `), selectedFieldType as any), | ||
}); | ||
} | ||
|
||
const parentName = | ||
(this._namespacedImportName ? `${this._namespacedImportName}.` : '') + | ||
this._convertName(parentSchemaType.name, { | ||
useTypesPrefix: true, | ||
}); | ||
|
||
const typeInfoField = this.buildTypeNameField(parentSchemaType, this._nonOptionalTypename, this._addTypename, requireTypename); | ||
|
||
if (this._preResolveTypes) { | ||
const primitiveFieldsTypes = this.buildPrimitiveFieldsWithoutPick(parentSchemaType, Array.from(primitiveFields.values()).map(field => field.name.value)); | ||
const primitiveAliasTypes = this.buildAliasedPrimitiveFieldsWithoutPick( | ||
parentSchemaType, | ||
Array.from(primitiveAliasFields.values()).map(field => ({ | ||
alias: field.alias.value, | ||
fieldName: field.name.value, | ||
})) | ||
); | ||
const linkFieldsTypes = this.buildLinkFieldsWithoutPick(linkFields); | ||
|
||
return `{ ${[typeInfoField, ...primitiveFieldsTypes, ...primitiveAliasTypes, ...linkFieldsTypes] | ||
.filter(a => a) | ||
.map(b => `${b.name}: ${b.type}`) | ||
.join(', ')} }`; | ||
} | ||
|
||
let typeInfoString: null | string = null; | ||
if (typeInfoField) { | ||
typeInfoString = `{ ${typeInfoField.name}: ${typeInfoField.type} }`; | ||
} | ||
|
||
const primitiveFieldsString = this.buildPrimitiveFields(parentName, Array.from(primitiveFields.values()).map(field => field.name.value)); | ||
const primitiveAliasFieldsString = this.buildAliasedPrimitiveFields( | ||
parentName, | ||
Array.from(primitiveAliasFields.values()).map(field => ({ | ||
alias: field.alias.value, | ||
fieldName: field.name.value, | ||
})) | ||
); | ||
const linkFieldsString = this.buildLinkFields(linkFields); | ||
const fragmentSpreadString = this.buildFragmentSpreadString([...fragmentSpreadSelectionSets.values()]); | ||
|
||
const result = [typeInfoString, primitiveFieldsString, primitiveAliasFieldsString, linkFieldsString, fragmentSpreadString].filter(Boolean); | ||
|
||
if (result.length === 0) { | ||
return null; | ||
} else if (result.length === 1) { | ||
return result[0]; | ||
} else { | ||
return `{\n ...` + result.join(`,\n ...`) + `\n}`; | ||
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. I think to change it in a base behind |
||
} | ||
} | ||
|
||
public createNext(parentSchemaType: GraphQLNamedType, selectionSet: SelectionSetNode): SelectionSetToObject { | ||
return new FlowSelectionSetToObject(this._scalars, this._schema, this._convertName, this._addTypename, this._preResolveTypes, this._nonOptionalTypename, this._loadedFragments, this._visitorConfig, parentSchemaType, selectionSet); | ||
} | ||
|
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.
I think to change it in a base behind
isFlow
flag. Will be less changes