How to use the graphql.visit function in graphql

To help you get started, we’ve selected a few graphql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github artsy / reaction / tslint / relayOperationGenericsRule.js View on Github external
function getOperationName(taggedTemplate) {
  const template = taggedTemplate.template.getFullText()
  // Strip backticks
  const source = template.substring(1, template.length - 1)

  const ast = parse(source)
  let queryName = null
  visit(ast, {
    OperationDefinition(node) {
      queryName = node.name.value
      return BREAK
    },
  })

  return queryName
}
github gridsome / gridsome / gridsome / lib / graphql / utils / parsePageQuery.js View on Github external
Field (fieldNode) {
      return visit(fieldNode, {
        Directive (node, key, parent, path, ancestors) {
          if (node.name.value === 'paginate') {
            if (result.paginate.typeName) {
              return BREAK
            }

            const parentNode = ancestors.slice().pop()
            const perPageArg = parentNode.arguments.find(node => node.name.value === 'perPage')

            // guess content type by converting root field value into a camel cased string
            result.paginate.typeName = upperFirst(trimStart(fieldNode.name.value, 'all'))

            if (perPageArg) {
              result.paginate.perPage = Number(perPageArg.value.value)
            }
github CDThomas / graphql-usage / src / getFieldInfo.ts View on Github external
function visitFields(
  node: OperationDefinitionNode | FragmentDefinitionNode,
  graphQLTag: GraphQLTag,
  typeInfo: TypeInfo,
  cb: (fieldInfo: FieldInfo) => void
) {
  if (!node.name) {
    throw new Error(
      "visitFields expects OperationDefinitions and FragmentDefinitions to be named"
    );
  }

  const { filePath, sourceLocationOffset, template } = graphQLTag;
  const operationOrFragmentName = node.name.value;

  visit(
    node,
    visitWithTypeInfo(typeInfo, {
      Field(graphqlNode) {
        // Discard client only fields, but don't throw an error
        if (isClientOnlyField(graphqlNode)) return;

        const parentType = typeInfo.getParentType();
        const nodeType = typeInfo.getType();
        const nodeName = graphqlNode.name.value;

        if (!parentType) {
          throw new Error(
            `visitFields expects fields to have a parent type. No parent type for ${nodeName}`
          );
        }
github TallerWebSolutions / apollo-cache-instorage / src / transform.js View on Github external
const hasPersistDirective = doc => {
  let hasDirective = false

  visit(doc, {
    Directive: ({ name: { value: name } }) => {
      if (name === 'persist') {
        hasDirective = true
        return BREAK
      }
    }
  })

  return hasDirective
}
github dotansimha / graphql-code-generator / packages / plugins / other / visitor-plugin-common / src / client-side-base-visitor.ts View on Github external
protected _extractFragments(document: FragmentDefinitionNode | OperationDefinitionNode): string[] {
    if (!document) {
      return [];
    }

    const names = [];

    visit(document, {
      enter: {
        FragmentSpread: (node: FragmentSpreadNode) => {
          names.push(node.name.value);
        },
      },
    });

    return names;
  }
github apollographql / apollo-tooling / packages / apollo-codegen-core / src / utilities / graphql.ts View on Github external
export function removeConnectionDirectives(ast: ASTNode) {
  return visit(ast, {
    Directive(node: DirectiveNode): DirectiveNode | null {
      if (node.name.value === "connection") return null;
      return node;
    }
  });
}
github apollographql / apollo-server / packages / apollo-engine-reporting / src / signature.ts View on Github external
export function removeAliases(ast: DocumentNode): DocumentNode {
  return visit(ast, {
    Field(node: FieldNode): FieldNode {
      return {
        ...node,
        alias: undefined,
      };
    },
  });
}
github artsy / metaphysics / src / schema / v2 / search / SearchResolver.ts View on Github external
shouldFetchEntityType(entityType: string): boolean {
    if (!this.cachedEntityTypesToFetch) {
      const entityTypesToFetch: string[] = []

      visit(this.info.fieldNodes[0], {
        Field(node) {
          if (node.name.value === "node") {
            visit(node, {
              InlineFragment(node) {
                if (
                  node.typeCondition &&
                  (node.typeCondition.name.value !== Searchable.name &&
                    node.typeCondition.name.value !== SearchableItem.name)
                ) {
                  entityTypesToFetch.push(node.typeCondition.name.value)
                }
              },
              FragmentSpread(_node) {
                throw new Error(
                  "Named fragment spreads are currently unsupported for search."
                )
github imolorhe / altair / src / app / services / gql / helpers.ts View on Github external
export const refactorFieldsWithFragmentSpread = (
  ast: DocumentNode,
  refactorMap: FragmentRefactorMap,
  schema: GraphQLSchema
) => {

  const typeInfo = new TypeInfo(schema);
  const edited = visit(ast, visitWithTypeInfo(typeInfo, {
    Field: {
      enter(node) {
        typeInfo.enter(node);
        const type = typeInfo.getType();
        if (type) {
          const typeName = getTypeName(type);
          const refactorFields = refactorMap[typeName];
          if (
            refactorFields &&
            node.selectionSet &&
            refactorFields.every(
              field => !!node.selectionSet!.selections.find((selection: FieldNode) => selection.name.value === field)
            )
          ) {
            return {
              ...node,
github gridsome / gridsome / gridsome / lib / graphql / utils / parsePageQuery.js View on Github external
function parsePageQuery (pageQuery) {
  const result = {
    content: pageQuery.content || '',
    options: pageQuery.options || {},
    typeName: pageQuery.typeName,
    variables: [],
    paginate: {
      typeName: undefined,
      perPage: PER_PAGE
    }
  }

  const ast = result.content ? parse(result.content) : null

  result.query = ast && visit(ast, {
    Variable ({ name: { value: name }}) {
      if (name === 'page') return
      if (name === 'path') return

      const path = !NODE_FIELDS.includes(name)
        ? ['fields'].concat(name.split('__'))
        : [name]

      result.variables.push({ name, path })
    },
    Field (fieldNode) {
      return visit(fieldNode, {
        Directive (node, key, parent, path, ancestors) {
          if (node.name.value === 'paginate') {
            if (result.paginate.typeName) {
              return BREAK