graphql#ASTVisitor TypeScript Examples
The following examples show how to use
graphql#ASTVisitor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: graphql-js-validation.ts From graphql-eslint with MIT License | 6 votes |
getFragmentDefsAndFragmentSpreads = (node: DocumentNode): GetFragmentDefsAndFragmentSpreads => {
const fragmentDefs = new Set<string>();
const fragmentSpreads = new Set<string>();
const visitor: ASTVisitor = {
FragmentDefinition(node) {
fragmentDefs.add(node.name.value);
},
FragmentSpread(node) {
fragmentSpreads.add(node.name.value);
},
};
visit(node, visitor);
return { fragmentDefs, fragmentSpreads };
}
Example #2
Source File: relay-edge-types.ts From graphql-eslint with MIT License | 6 votes |
function getEdgeTypes(schema: GraphQLSchema): EdgeTypes {
// We don't want cache edgeTypes on test environment
// Otherwise edgeTypes will be same for all tests
if (process.env.NODE_ENV !== 'test' && edgeTypesCache) {
return edgeTypesCache;
}
const edgeTypes: EdgeTypes = new Set();
const visitor: ASTVisitor = {
ObjectTypeDefinition(node): void {
const typeName = node.name.value;
const hasConnectionSuffix = typeName.endsWith('Connection');
if (!hasConnectionSuffix) {
return;
}
const edges = node.fields.find(field => field.name.value === 'edges');
if (edges) {
const edgesTypeName = getTypeName(edges);
const edgesType = schema.getType(edgesTypeName);
if (isObjectType(edgesType)) {
edgeTypes.add(edgesTypeName);
}
}
},
};
const astNode = getDocumentNodeFromSchema(schema); // Transforms the schema into ASTNode
visit(astNode, visitor);
edgeTypesCache = edgeTypes;
return edgeTypesCache;
}
Example #3
Source File: converter.ts From graphql-eslint with MIT License | 5 votes |
export function convertToESTree<T extends DocumentNode>(node: T, schema?: GraphQLSchema) {
const typeInfo = schema ? new TypeInfo(schema) : null;
const visitor: ASTVisitor = {
leave(node, key, parent) {
const leadingComments: Comment[] =
'description' in node && node.description
? [
{
type: node.description.block ? 'Block' : 'Line',
value: node.description.value,
},
]
: [];
const calculatedTypeInfo: TypeInformation | Record<string, never> = typeInfo
? {
argument: typeInfo.getArgument(),
defaultValue: typeInfo.getDefaultValue(),
directive: typeInfo.getDirective(),
enumValue: typeInfo.getEnumValue(),
fieldDef: typeInfo.getFieldDef(),
inputType: typeInfo.getInputType(),
parentInputType: typeInfo.getParentInputType(),
parentType: typeInfo.getParentType(),
gqlType: typeInfo.getType(),
}
: {};
const rawNode = () => {
if (parent && key !== undefined) {
return parent[key];
}
return node.kind === Kind.DOCUMENT
? <DocumentNode>{
...node,
definitions: node.definitions.map(definition =>
(definition as unknown as GraphQLESTreeNode<DefinitionNode>).rawNode()
),
}
: node;
};
const commonFields: Omit<GraphQLESTreeNode<typeof node>, 'parent'> = {
...node,
type: node.kind,
loc: convertLocation(node.loc),
range: [node.loc.start, node.loc.end],
leadingComments,
// Use function to prevent RangeError: Maximum call stack size exceeded
typeInfo: () => calculatedTypeInfo as any, // Don't know if can fix error
rawNode,
};
return 'type' in node
? {
...commonFields,
gqlType: node.type,
}
: commonFields;
},
};
return visit(node, typeInfo ? visitWithTypeInfo(typeInfo, visitor) : visitor) as GraphQLESTreeNode<T>;
}
Example #4
Source File: no-unreachable-types.ts From graphql-eslint with MIT License | 5 votes |
function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
// We don't want cache reachableTypes on test environment
// Otherwise reachableTypes will be same for all tests
if (process.env.NODE_ENV !== 'test' && reachableTypesCache) {
return reachableTypesCache;
}
const reachableTypes: ReachableTypes = new Set();
const collect = (node: ASTNode): false | void => {
const typeName = getTypeName(node);
if (reachableTypes.has(typeName)) {
return;
}
reachableTypes.add(typeName);
const type = schema.getType(typeName) || schema.getDirective(typeName);
if (isInterfaceType(type)) {
const { objects, interfaces } = schema.getImplementations(type);
for (const { astNode } of [...objects, ...interfaces]) {
visit(astNode, visitor);
}
} else if (type.astNode) {
// astNode can be undefined for ID, String, Boolean
visit(type.astNode, visitor);
}
};
const visitor: ASTVisitor = {
InterfaceTypeDefinition: collect,
ObjectTypeDefinition: collect,
InputValueDefinition: collect,
UnionTypeDefinition: collect,
FieldDefinition: collect,
Directive: collect,
NamedType: collect,
};
for (const type of [
schema, // visiting SchemaDefinition node
schema.getQueryType(),
schema.getMutationType(),
schema.getSubscriptionType(),
]) {
// if schema don't have Query type, schema.astNode will be undefined
if (type?.astNode) {
visit(type.astNode, visitor);
}
}
reachableTypesCache = reachableTypes;
return reachableTypesCache;
}
Example #5
Source File: selectors.ts From tql with MIT License | 4 votes |
transform = (
ast: DocumentNode,
schema: GraphQLSchema
): ASTVisitor => {
// const Field = imp("Field@timkendall@tql");
// const Argument = imp("Argument@timkendall@tql");
// const Variable = imp("Variable@timkendall@tql");
// const InlineFragment = imp("InlineFragment@timkendall@tql");
return {
[Kind.DIRECTIVE_DEFINITION]: () => null,
[Kind.SCALAR_TYPE_DEFINITION]: () => null,
[Kind.ENUM_TYPE_DEFINITION]: (node) => {
return null;
},
[Kind.ENUM_VALUE_DEFINITION]: (node) => {
return null;
},
[Kind.INPUT_OBJECT_TYPE_DEFINITION]: (def) => {
return null;
},
[Kind.OBJECT_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLObjectType,
`Type "${typename}" was not instance of expected class GraphQLObjectType.`
);
const fields = Object.values(type.getFields());
return code`
${/* selector interface */ ""}
interface I${type.name}Selector {
readonly __typename: () => Field<"__typename">
${fields.map(printSignature).join("\n")}
}
${/* selector object */ ""}
const ${typename}Selector: I${typename}Selector = {
__typename: () => field("__typename"),
${fields.map(printMethod).join("\n")}
}
${/* select fn */ ""}
export const ${toLower(
typename
)} = <T extends ReadonlyArray<Selection>>(select: (t: I${typename}Selector) => T) => new SelectionBuilder<ISchema, "${type}", T>(SCHEMA as any, "${typename}", select(${typename}Selector))
`;
},
[Kind.INTERFACE_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLInterfaceType,
`Type "${typename}" was not instance of expected class GraphQLInterfaceType.`
);
// @note Get all implementors of this union
const implementations = schema
.getPossibleTypes(type)
.map((type) => type.name);
const fields = Object.values(type.getFields());
return code`
${/* selector interface */ ""}
interface I${type.name}Selector {
readonly __typename: () => Field<"__typename">
${fields.map(printSignature).join("\n")}
readonly on: <T extends ReadonlyArray<Selection>, F extends ${implementations
.map((name) => `"${name}"`)
.join(" | ")}>(
type: F,
select: (t: ${printConditionalSelectorArg(
implementations.map((name) => name)
)}) => T
) => InlineFragment<NamedType<F>, SelectionSet<T>>
}
${/* selector object */ ""}
const ${typename}Selector: I${typename}Selector = {
__typename: () => field("__typename"),
${fields.map(printMethod).join("\n")}
on: (
type,
select,
) => {
switch(type) {
${implementations
.map(
(name) => `
case "${name}": {
return inlineFragment(
namedType("${name}"),
selectionSet(select(${name}Selector as Parameters<typeof select>[0])),
)
}
`
)
.join("\n")}
default:
throw new TypeConditionError({
selectedType: type,
abstractType: "${type.name}",
})
}
},
}
${/* select fn */ ""}
export const ${toLower(
typename
)} = <T extends ReadonlyArray<Selection>>(select: (t: I${typename}Selector) => T) => new SelectionBuilder<ISchema, "${type}", T>(SCHEMA as any, "${typename}", select(${typename}Selector))
`;
},
[Kind.UNION_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLUnionType,
`Type "${typename}" was not instance of expected class GraphQLUnionType.`
);
// @note Get all implementors of this union
const implementations = schema
.getPossibleTypes(type)
.map((type) => type.name);
return code`
${/* selector interface */ ""}
interface I${type.name}Selector {
readonly __typename: () => Field<"__typename">
readonly on: <T extends ReadonlyArray<Selection>, F extends ${implementations
.map((name) => `"${name}"`)
.join(" | ")}>(
type: F,
select: (t: ${printConditionalSelectorArg(
implementations.map((name) => name)
)}) => T
) => InlineFragment<NamedType<F>, SelectionSet<T>>
}
${/* selector object */ ""}
const ${typename}Selector: I${typename}Selector = {
__typename: () => field("__typename"),
on: (
type,
select,
) => {
switch(type) {
${implementations
.map(
(name) => `
case "${name}": {
return inlineFragment(
namedType("${name}"),
selectionSet(select(${name}Selector as Parameters<typeof select>[0])),
)
}
`
)
.join("\n")}
default:
throw new TypeConditionError({
selectedType: type,
abstractType: "${type.name}",
})
}
},
}
${/* select fn */ ""}
export const ${toLower(
typename
)} = <T extends ReadonlyArray<Selection>>(select: (t: I${typename}Selector) => T) => new SelectionBuilder<ISchema, "${type}", T>(SCHEMA as any, "${typename}", select(${typename}Selector))
`;
},
[Kind.SCHEMA_DEFINITION]: (node) => {
return null;
},
};
}
Example #6
Source File: types.ts From tql with MIT License | 4 votes |
transform = (
ast: DocumentNode,
schema: GraphQLSchema
): ASTVisitor => {
// @note needed to serialize inline enum values correctly at runtime
const enumValues = new Set<string>();
return {
[Kind.DIRECTIVE_DEFINITION]: () => null,
[Kind.SCALAR_TYPE_DEFINITION]: () => null,
[Kind.ENUM_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const values = node.values?.map((v) => v.name.value) ?? [];
const printMember = (member: string): string => {
return `${member} = "${member}"`;
};
return code`
export enum ${typename} {
${values.map(printMember).join(",\n")}
}
`;
},
[Kind.ENUM_VALUE_DEFINITION]: (node) => {
enumValues.add(node.name.value);
return null;
},
[Kind.INPUT_OBJECT_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLInputObjectType,
`Type "${typename}" was not instance of expected class GraphQLInputObjectType.`
);
const fields = Object.values(type.getFields());
const printField = (field: GraphQLInputField) => {
const isList = listType(field.type);
const isNonNull = isNonNullType(field.type);
const baseType = inputType(field.type);
let tsType: string;
if (baseType instanceof GraphQLScalarType) {
tsType = toPrimitive(baseType);
} else if (baseType instanceof GraphQLEnumType) {
tsType = baseType.name;
} else if (baseType instanceof GraphQLInputObjectType) {
tsType = "I" + baseType.name;
} else {
throw new Error("Unable to render inputField!");
}
return [
field.name,
isNonNull ? ":" : "?:",
" ",
tsType,
isList ? "[]" : "",
].join("");
};
return code`
export interface I${typename} {
${fields.map(printField).join("\n")}
}
`;
},
[Kind.OBJECT_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLObjectType,
`Type "${typename}" was not instance of expected class GraphQLObjectType.`
);
const fields = Object.values(type.getFields());
const interfaces = type.getInterfaces();
// @note TypeScript only requires new fields to be defined on interface extendors
const interfaceFields = interfaces.flatMap((i) =>
Object.values(i.getFields()).map((field) => field.name)
);
const uncommonFields = fields.filter(
(field) => !interfaceFields.includes(field.name)
);
// @todo extend any implemented interfaces
// @todo only render fields unique to this type
const extensions =
interfaces.length > 0
? `extends ${interfaces.map((i) => "I" + i.name).join(", ")}`
: "";
return code`
export interface I${typename} ${extensions} {
readonly __typename: ${`"${typename}"`}
${uncommonFields.map(printField).join("\n")}
}
`;
},
[Kind.INTERFACE_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLInterfaceType,
`Type "${typename}" was not instance of expected class GraphQLInterfaceType.`
);
// @note Get all implementors of this union
const implementations = schema
.getPossibleTypes(type)
.map((type) => type.name);
const fields = Object.values(type.getFields());
return code`
export interface I${typename} {
readonly __typename: ${implementations
.map((type) => `"${type}"`)
.join(" | ")}
${fields.map(printField).join("\n")}
}
`;
},
[Kind.UNION_TYPE_DEFINITION]: (node) => {
const typename = node.name.value;
const type = schema.getType(typename);
invariant(
type instanceof GraphQLUnionType,
`Type "${typename}" was not instance of expected class GraphQLUnionType.`
);
// @note Get all implementors of this union
const implementations = schema
.getPossibleTypes(type)
.map((type) => type.name);
return code`
export type ${"I" + type.name} = ${implementations
.map((type) => `I${type}`)
.join(" | ")}
`;
},
[Kind.SCHEMA_DEFINITION]: (_) => {
return null;
},
};
}