本文整理汇总了TypeScript中graphql.TypeInfo类的典型用法代码示例。如果您正苦于以下问题:TypeScript TypeInfo类的具体用法?TypeScript TypeInfo怎么用?TypeScript TypeInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TypeInfo类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: replaceFieldsWithFragments
function replaceFieldsWithFragments(
targetSchema: GraphQLSchema,
document: DocumentNode,
mapping: FieldToFragmentMapping,
): DocumentNode {
const typeInfo = new TypeInfo(targetSchema);
return visit(
document,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET](
node: SelectionSetNode,
): SelectionSetNode | null | undefined {
const parentType: GraphQLType = typeInfo.getParentType();
if (parentType) {
const parentTypeName = parentType.name;
let selections = node.selections;
if (mapping[parentTypeName]) {
node.selections.forEach(selection => {
if (selection.kind === Kind.FIELD) {
const name = selection.name.value;
const fragments = mapping[parentTypeName][name];
if (fragments && fragments.length > 0) {
const fragment = concatInlineFragments(
parentTypeName,
fragments,
);
selections = selections.concat(fragment);
}
}
});
}
if (selections !== node.selections) {
return {
...node,
selections,
};
}
}
},
}),
);
}
示例2: addTypenameToAbstract
function addTypenameToAbstract(
targetSchema: GraphQLSchema,
document: DocumentNode,
): DocumentNode {
const typeInfo = new TypeInfo(targetSchema);
return visit(
document,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET](
node: SelectionSetNode,
): SelectionSetNode | null | undefined {
const parentType: GraphQLType = typeInfo.getParentType();
let selections = node.selections;
if (
parentType &&
(parentType instanceof GraphQLInterfaceType ||
parentType instanceof GraphQLUnionType) &&
!selections.find(
_ =>
(_ as FieldNode).kind === Kind.FIELD &&
(_ as FieldNode).name.value === '__typename',
)
) {
selections = selections.concat({
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: '__typename',
},
});
}
if (selections !== node.selections) {
return {
...node,
selections,
};
}
},
}),
);
}
示例3: stripQuery
function stripQuery(schema, queryAST, operationName, extensionFields) {
const typeInfo = new TypeInfo(schema);
const changedAST = visit(queryAST, visitWithTypeInfo(typeInfo, {
[Kind.FIELD]: () => {
const typeName = typeInfo.getParentType().name;
const fieldName = typeInfo.getFieldDef().name;
if (fieldName.startsWith('__'))
return null;
if ((extensionFields[typeName] || []).includes(fieldName))
return null;
},
[Kind.SELECTION_SET]: {
leave(node) {
const type = typeInfo.getParentType()
if (isAbstractType(type) || node.selections.length === 0)
return injectTypename(node);
}
},
}), null);
return print(extractOperation(changedAST, operationName));
}
示例4: expandAbstractTypes
function expandAbstractTypes(
targetSchema: GraphQLSchema,
mapping: TypeMapping,
reverseMapping: TypeMapping,
document: DocumentNode,
): DocumentNode {
const operations: Array<
OperationDefinitionNode
> = document.definitions.filter(
def => def.kind === Kind.OPERATION_DEFINITION,
) as Array<OperationDefinitionNode>;
const fragments: Array<FragmentDefinitionNode> = document.definitions.filter(
def => def.kind === Kind.FRAGMENT_DEFINITION,
) as Array<FragmentDefinitionNode>;
const existingFragmentNames = fragments.map(fragment => fragment.name.value);
let fragmentCounter = 0;
const generateFragmentName = (typeName: string) => {
let fragmentName;
do {
fragmentName = `_${typeName}_Fragment${fragmentCounter}`;
fragmentCounter++;
} while (existingFragmentNames.indexOf(fragmentName) !== -1);
return fragmentName;
};
const newFragments: Array<FragmentDefinitionNode> = [];
const fragmentReplacements: {
[fragmentName: string]: Array<{ fragmentName: string; typeName: string }>;
} = {};
fragments.forEach((fragment: FragmentDefinitionNode) => {
newFragments.push(fragment);
const possibleTypes = mapping[fragment.typeCondition.name.value];
if (possibleTypes) {
fragmentReplacements[fragment.name.value] = [];
possibleTypes.forEach(possibleTypeName => {
const name = generateFragmentName(possibleTypeName);
existingFragmentNames.push(name);
const newFragment: FragmentDefinitionNode = {
kind: Kind.FRAGMENT_DEFINITION,
name: {
kind: Kind.NAME,
value: name,
},
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: possibleTypeName,
},
},
selectionSet: fragment.selectionSet,
};
newFragments.push(newFragment);
fragmentReplacements[fragment.name.value].push({
fragmentName: name,
typeName: possibleTypeName,
});
});
}
});
const newDocument = {
...document,
definitions: [...operations, ...newFragments],
};
const typeInfo = new TypeInfo(targetSchema);
return visit(
newDocument,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET](node: SelectionSetNode) {
const newSelections = [...node.selections];
const parentType: GraphQLNamedType = getNamedType(
typeInfo.getParentType(),
);
node.selections.forEach((selection: SelectionNode) => {
if (selection.kind === Kind.INLINE_FRAGMENT) {
const possibleTypes = mapping[selection.typeCondition.name.value];
if (possibleTypes) {
possibleTypes.forEach(possibleType => {
if (
implementsAbstractType(
targetSchema,
parentType,
targetSchema.getType(possibleType),
)
) {
newSelections.push({
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: possibleType,
},
},
selectionSet: selection.selectionSet,
});
//.........这里部分代码省略.........