@graphql-codegen/visitor-plugin-common
Advanced tools
Comparing version 1.0.4-alpha-879e52ae.0 to 1.0.4-alpha-8c810d4e.8
@@ -104,6 +104,10 @@ "use strict"; | ||
} | ||
const documentName = this.convertName(node, { | ||
suffix: 'Document', | ||
useTypesPrefix: false, | ||
}); | ||
const documentVariableName = this.convertName(node, { | ||
suffix: 'Document', | ||
}); | ||
const documentString = `export const ${documentVariableName}${this.config.noGraphQLTag ? ': DocumentNode' : ''} = ${this._gql(node)};`; | ||
const documentString = `export const ${documentName}${this.config.noGraphQLTag ? ': DocumentNode' : ''} = ${this._gql(node)};`; | ||
const operationType = plugin_helpers_1.toPascalCase(node.operation); | ||
@@ -110,0 +114,0 @@ const operationResultType = this.convertName(node, { |
@@ -16,4 +16,3 @@ import { SelectionSetNode, FieldNode, FragmentSpreadNode, InlineFragmentNode, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
}; | ||
export declare type FragmentSpreadField = string; | ||
export declare type InlineFragmentField = { | ||
export declare type FragmentsMap = { | ||
[onType: string]: string[]; | ||
@@ -36,4 +35,3 @@ }; | ||
protected _linksFields: LinkField[]; | ||
protected _fragmentSpreads: FragmentSpreadField[]; | ||
protected _inlineFragments: InlineFragmentField; | ||
protected _fragments: FragmentsMap; | ||
protected _queriedForTypename: boolean; | ||
@@ -53,4 +51,3 @@ constructor(_scalars: ScalarsMap, _schema: GraphQLSchema, _convertName: ConvertNameFn<BaseVisitorConvertOptions>, _addTypename: boolean, _loadedFragments: LoadedFragment[], _parentSchemaType?: GraphQLNamedType, _selectionSet?: SelectionSetNode); | ||
protected buildLinkFields(fields: LinkField[]): string | null; | ||
protected buildInlineFragments(inlineFragments: InlineFragmentField): string | null; | ||
protected buildFragmentSpread(fragmentsSpread: FragmentSpreadField[]): string | null; | ||
protected buildFragments(fragments: FragmentsMap): string | null; | ||
} |
@@ -27,4 +27,3 @@ "use strict"; | ||
this._linksFields = []; | ||
this._fragmentSpreads = []; | ||
this._inlineFragments = {}; | ||
this._fragments = {}; | ||
this._queriedForTypename = false; | ||
@@ -77,3 +76,7 @@ } | ||
_collectFragmentSpread(node) { | ||
this._fragmentSpreads.push(node.name.value); | ||
const loadedFragment = this._loadedFragments.find(f => f.name === node.name.value); | ||
if (!this._fragments[loadedFragment.onType]) { | ||
this._fragments[loadedFragment.onType] = []; | ||
} | ||
this._fragments[loadedFragment.onType].push(this._convertName(node.name.value, { useTypesPrefix: true, suffix: 'Fragment' })); | ||
} | ||
@@ -83,7 +86,10 @@ _collectInlineFragment(node) { | ||
const schemaType = this._schema.getType(onType); | ||
if (!schemaType) { | ||
throw new Error(`Inline fragment refernces a GraphQL type "${onType}" that does not exists in your schema!`); | ||
} | ||
const selectionSet = this.createNext(schemaType, node.selectionSet); | ||
if (!this._inlineFragments[onType]) { | ||
this._inlineFragments[onType] = []; | ||
if (!this._fragments[onType]) { | ||
this._fragments[onType] = []; | ||
} | ||
this._inlineFragments[onType].push(selectionSet.string); | ||
this._fragments[onType].push(selectionSet.string); | ||
} | ||
@@ -115,5 +121,4 @@ get string() { | ||
const linksFields = this.buildLinkFields(this._linksFields); | ||
const inlineFragments = this.buildInlineFragments(this._inlineFragments); | ||
const fragmentSpreads = this.buildFragmentSpread(this._fragmentSpreads); | ||
const fieldsSet = [typeName, baseFields, aliasBaseFields, linksFields, fragmentSpreads, inlineFragments].filter(f => f && f !== ''); | ||
const fragments = this.buildFragments(this._fragments); | ||
const fieldsSet = [typeName, baseFields, aliasBaseFields, linksFields, fragments].filter(f => f && f !== ''); | ||
return this.mergeAllFields(fieldsSet); | ||
@@ -155,25 +160,39 @@ } | ||
} | ||
buildInlineFragments(inlineFragments) { | ||
const allPossibleTypes = Object.keys(inlineFragments).map(typeName => inlineFragments[typeName].join(' & ')); | ||
return utils_1.quoteIfNeeded(allPossibleTypes, ' | '); | ||
} | ||
buildFragmentSpread(fragmentsSpread) { | ||
if (fragmentsSpread.length === 0) { | ||
return null; | ||
buildFragments(fragments) { | ||
const interfaces = {}; | ||
const types = {}; | ||
const onInterfaces = Object.keys(fragments).filter(typeName => graphql_1.isInterfaceType(this._schema.getType(typeName))); | ||
const onNonInterfaces = Object.keys(fragments).filter(typeName => !graphql_1.isInterfaceType(this._schema.getType(typeName))); | ||
for (const typeName of onInterfaces) { | ||
const interfaceFragments = fragments[typeName]; | ||
interfaces[typeName] = { | ||
fragments: interfaceFragments, | ||
implementingFragments: [], | ||
}; | ||
} | ||
const typeToFragment = fragmentsSpread.reduce((prev, fragmentName) => { | ||
const fragmentDef = this._loadedFragments.find(r => r.name === fragmentName); | ||
if (!prev[fragmentDef.onType]) { | ||
prev[fragmentDef.onType] = []; | ||
for (const typeName of onNonInterfaces) { | ||
const schemaType = this._schema.getType(typeName); | ||
if (!schemaType) { | ||
throw new Error(`Inline fragment refernces a GraphQL type "${typeName}" that does not exists in your schema!`); | ||
} | ||
prev[fragmentDef.onType].push(fragmentName); | ||
return prev; | ||
}, {}); | ||
const allPossibleTypes = Object.keys(typeToFragment).map(typeName => typeToFragment[typeName] | ||
.map(fragmentName => this._convertName(fragmentName, { | ||
suffix: 'Fragment', | ||
useTypesPrefix: true, | ||
})) | ||
.join(' & ')); | ||
return utils_1.quoteIfNeeded(allPossibleTypes, ' | '); | ||
const typeFragments = fragments[typeName]; | ||
const interfacesFragments = schemaType.getInterfaces().filter(gqlInterface => !!interfaces[gqlInterface.name]); | ||
if (interfacesFragments.length > 0) { | ||
for (const relevantInterface of interfacesFragments) { | ||
interfaces[relevantInterface.name].implementingFragments.push(...typeFragments); | ||
} | ||
} | ||
else { | ||
types[typeName] = { | ||
fragments: typeFragments, | ||
implementingFragments: [], | ||
}; | ||
} | ||
} | ||
const mergedResult = Object.assign({}, interfaces, types); | ||
return utils_1.quoteIfNeeded(Object.keys(mergedResult).map(typeName => { | ||
const baseFragments = utils_1.quoteIfNeeded(mergedResult[typeName].fragments, ' & '); | ||
const implementingFragments = utils_1.quoteIfNeeded(mergedResult[typeName].implementingFragments, ' | '); | ||
return utils_1.quoteIfNeeded([baseFragments, implementingFragments].filter(a => a), ' & '); | ||
}), ' | '); | ||
} | ||
@@ -180,0 +199,0 @@ } |
@@ -102,6 +102,10 @@ import { BaseVisitor } from './index'; | ||
} | ||
const documentName = this.convertName(node, { | ||
suffix: 'Document', | ||
useTypesPrefix: false, | ||
}); | ||
const documentVariableName = this.convertName(node, { | ||
suffix: 'Document', | ||
}); | ||
const documentString = `export const ${documentVariableName}${this.config.noGraphQLTag ? ': DocumentNode' : ''} = ${this._gql(node)};`; | ||
const documentString = `export const ${documentName}${this.config.noGraphQLTag ? ': DocumentNode' : ''} = ${this._gql(node)};`; | ||
const operationType = toPascalCase(node.operation); | ||
@@ -108,0 +112,0 @@ const operationResultType = this.convertName(node, { |
@@ -16,4 +16,3 @@ import { SelectionSetNode, FieldNode, FragmentSpreadNode, InlineFragmentNode, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
}; | ||
export declare type FragmentSpreadField = string; | ||
export declare type InlineFragmentField = { | ||
export declare type FragmentsMap = { | ||
[onType: string]: string[]; | ||
@@ -36,4 +35,3 @@ }; | ||
protected _linksFields: LinkField[]; | ||
protected _fragmentSpreads: FragmentSpreadField[]; | ||
protected _inlineFragments: InlineFragmentField; | ||
protected _fragments: FragmentsMap; | ||
protected _queriedForTypename: boolean; | ||
@@ -53,4 +51,3 @@ constructor(_scalars: ScalarsMap, _schema: GraphQLSchema, _convertName: ConvertNameFn<BaseVisitorConvertOptions>, _addTypename: boolean, _loadedFragments: LoadedFragment[], _parentSchemaType?: GraphQLNamedType, _selectionSet?: SelectionSetNode); | ||
protected buildLinkFields(fields: LinkField[]): string | null; | ||
protected buildInlineFragments(inlineFragments: InlineFragmentField): string | null; | ||
protected buildFragmentSpread(fragmentsSpread: FragmentSpreadField[]): string | null; | ||
protected buildFragments(fragments: FragmentsMap): string | null; | ||
} |
@@ -25,4 +25,3 @@ import { Kind, isObjectType, isUnionType, isInterfaceType, isEnumType, isEqualType, SchemaMetaFieldDef, TypeMetaFieldDef, isScalarType, } from 'graphql'; | ||
this._linksFields = []; | ||
this._fragmentSpreads = []; | ||
this._inlineFragments = {}; | ||
this._fragments = {}; | ||
this._queriedForTypename = false; | ||
@@ -75,3 +74,7 @@ } | ||
_collectFragmentSpread(node) { | ||
this._fragmentSpreads.push(node.name.value); | ||
const loadedFragment = this._loadedFragments.find(f => f.name === node.name.value); | ||
if (!this._fragments[loadedFragment.onType]) { | ||
this._fragments[loadedFragment.onType] = []; | ||
} | ||
this._fragments[loadedFragment.onType].push(this._convertName(node.name.value, { useTypesPrefix: true, suffix: 'Fragment' })); | ||
} | ||
@@ -81,7 +84,10 @@ _collectInlineFragment(node) { | ||
const schemaType = this._schema.getType(onType); | ||
if (!schemaType) { | ||
throw new Error(`Inline fragment refernces a GraphQL type "${onType}" that does not exists in your schema!`); | ||
} | ||
const selectionSet = this.createNext(schemaType, node.selectionSet); | ||
if (!this._inlineFragments[onType]) { | ||
this._inlineFragments[onType] = []; | ||
if (!this._fragments[onType]) { | ||
this._fragments[onType] = []; | ||
} | ||
this._inlineFragments[onType].push(selectionSet.string); | ||
this._fragments[onType].push(selectionSet.string); | ||
} | ||
@@ -113,5 +119,4 @@ get string() { | ||
const linksFields = this.buildLinkFields(this._linksFields); | ||
const inlineFragments = this.buildInlineFragments(this._inlineFragments); | ||
const fragmentSpreads = this.buildFragmentSpread(this._fragmentSpreads); | ||
const fieldsSet = [typeName, baseFields, aliasBaseFields, linksFields, fragmentSpreads, inlineFragments].filter(f => f && f !== ''); | ||
const fragments = this.buildFragments(this._fragments); | ||
const fieldsSet = [typeName, baseFields, aliasBaseFields, linksFields, fragments].filter(f => f && f !== ''); | ||
return this.mergeAllFields(fieldsSet); | ||
@@ -153,27 +158,41 @@ } | ||
} | ||
buildInlineFragments(inlineFragments) { | ||
const allPossibleTypes = Object.keys(inlineFragments).map(typeName => inlineFragments[typeName].join(' & ')); | ||
return quoteIfNeeded(allPossibleTypes, ' | '); | ||
} | ||
buildFragmentSpread(fragmentsSpread) { | ||
if (fragmentsSpread.length === 0) { | ||
return null; | ||
buildFragments(fragments) { | ||
const interfaces = {}; | ||
const types = {}; | ||
const onInterfaces = Object.keys(fragments).filter(typeName => isInterfaceType(this._schema.getType(typeName))); | ||
const onNonInterfaces = Object.keys(fragments).filter(typeName => !isInterfaceType(this._schema.getType(typeName))); | ||
for (const typeName of onInterfaces) { | ||
const interfaceFragments = fragments[typeName]; | ||
interfaces[typeName] = { | ||
fragments: interfaceFragments, | ||
implementingFragments: [], | ||
}; | ||
} | ||
const typeToFragment = fragmentsSpread.reduce((prev, fragmentName) => { | ||
const fragmentDef = this._loadedFragments.find(r => r.name === fragmentName); | ||
if (!prev[fragmentDef.onType]) { | ||
prev[fragmentDef.onType] = []; | ||
for (const typeName of onNonInterfaces) { | ||
const schemaType = this._schema.getType(typeName); | ||
if (!schemaType) { | ||
throw new Error(`Inline fragment refernces a GraphQL type "${typeName}" that does not exists in your schema!`); | ||
} | ||
prev[fragmentDef.onType].push(fragmentName); | ||
return prev; | ||
}, {}); | ||
const allPossibleTypes = Object.keys(typeToFragment).map(typeName => typeToFragment[typeName] | ||
.map(fragmentName => this._convertName(fragmentName, { | ||
suffix: 'Fragment', | ||
useTypesPrefix: true, | ||
})) | ||
.join(' & ')); | ||
return quoteIfNeeded(allPossibleTypes, ' | '); | ||
const typeFragments = fragments[typeName]; | ||
const interfacesFragments = schemaType.getInterfaces().filter(gqlInterface => !!interfaces[gqlInterface.name]); | ||
if (interfacesFragments.length > 0) { | ||
for (const relevantInterface of interfacesFragments) { | ||
interfaces[relevantInterface.name].implementingFragments.push(...typeFragments); | ||
} | ||
} | ||
else { | ||
types[typeName] = { | ||
fragments: typeFragments, | ||
implementingFragments: [], | ||
}; | ||
} | ||
} | ||
const mergedResult = Object.assign({}, interfaces, types); | ||
return quoteIfNeeded(Object.keys(mergedResult).map(typeName => { | ||
const baseFragments = quoteIfNeeded(mergedResult[typeName].fragments, ' & '); | ||
const implementingFragments = quoteIfNeeded(mergedResult[typeName].implementingFragments, ' | '); | ||
return quoteIfNeeded([baseFragments, implementingFragments].filter(a => a), ' & '); | ||
}), ' | '); | ||
} | ||
} | ||
//# sourceMappingURL=selection-set-to-object.js.map |
{ | ||
"name": "@graphql-codegen/visitor-plugin-common", | ||
"version": "1.0.4-alpha-879e52ae.0+879e52ae", | ||
"version": "1.0.4-alpha-8c810d4e.8+8c810d4e", | ||
"license": "MIT", | ||
@@ -9,3 +9,3 @@ "scripts": { | ||
"dependencies": { | ||
"@graphql-codegen/plugin-helpers": "1.0.4-alpha-879e52ae.0+879e52ae", | ||
"@graphql-codegen/plugin-helpers": "1.0.4-alpha-8c810d4e.8+8c810d4e", | ||
"auto-bind": "2.0.0", | ||
@@ -20,3 +20,3 @@ "dependency-graph": "0.8.0", | ||
"devDependencies": { | ||
"@graphql-codegen/testing": "1.0.4-alpha-879e52ae.0+879e52ae", | ||
"@graphql-codegen/testing": "1.0.4-alpha-8c810d4e.8+8c810d4e", | ||
"@types/graphql": "14.0.7", | ||
@@ -36,3 +36,3 @@ "graphql": "14.1.1", | ||
}, | ||
"gitHead": "879e52ae6ca50f26fda8ee7a45941345d51e90ed" | ||
"gitHead": "8c810d4ea1be96e5edf57ef2dfe0ebd7b0b6c9f1" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
245046
3585