npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fruits-chain/qiufen-pro-helpers

v0.1.6

Published

Collection of helper functions for qiufen-pro

Downloads

8

Readme

@fruits-chain/qiufen-pro-helpers

  • Collection of helper functions for qiufen-pro.
  • Suggestion: Use with graphql.js.
  • Fast and efficient processing of graphql ast.
  • You can also use some of the utility functions in "graphql.js".

Functions

fetchTypeDefs

fetchTypeDefs(url: string): Promise<string>

Given a url, return a Promise that resolves to a graphql sdl string when the retrieval is complete.

// demo
fetchTypeDefs('http://localhost:4001/graphql').then(str => {
  console.log(str)
})

str: /*
  type Book {
    title: String
    author: String
  }

  input TaskBoardQueryInput {
    date: Int!
  }

  type Query {
    books(contains: TaskBoardQueryInput): [Book]
  }
*/

fetchSchema

fetchSchema(url: string): Promise<GraphQLSchema>

Given a url, return a Promise that resolves to a GraphQLSchema when the retrieval is complete.

// demo
fetchSchema('http://localhost:4001/graphql').then(graphqlSchema => {
  console.log(graphqlSchema)
})

graphqlSchema: /*
 GraphQLSchema {__validationErrors: undefined, description: undefined,extensions: {…}, astNode: undefined, ...}
*/

parseOperationWithDescriptions

function parseOperationWithDescriptions(
  sourceSdl: string,
  options?: ParseOptions,
): DocumentNode

A string one set of reasonable formatting rules is parsed to ast with descriptions.

// demo
const gql = `
# -- test detail comment --
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    # test comment0
    abnormalOrderId
    abnormalOrderStatus
    customer {
      # test comment1
      customerId
      customerName
      customerType
    }
  }
}
`

const documentNode = parseOperationWithDescriptions(gql)
console.log(documentNode)

documentNode: /*
{kind: 'Document', definitions: Array(1), loc: Location ...}
*/

printWithComments

function printWithComments(
  ast: ASTNode,
  isAllFieldNodeAddComment?: boolean,
): string

Converts an AST into a string, using one set of reasonable formatting rules.

// demo1
const ruleStrWithHeaderComment = printWithComments(documentNode) // The documentNode in the last example
console.log(ruleStrWithHeaderComment)

ruleStrWithHeaderComment: /*
# -- test detail comment --
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    customer {
      customerId
      customerName
      customerType
    }
  }
}
*/

// demo2
const ruleStrWithAllFieldsComment = printWithComments(documentNode,true) // The documentNode in the last example
console.log(ruleStrWithAllFieldsComment)

ruleStrWithAllFieldsComment: /*
# -- test detail comment --
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    # test comment0
    abnormalOrderId
    abnormalOrderStatus
    customer {
      # test comment1
      customerId
      customerName
      customerType
    }
  }
}
*/

buildOperationNodeForField

function buildOperationNodeForField({
  schema,
  kind,
  field,
  models,
  ignore,
  depthLimit,
  circularReferenceDepth,
  argNames,
  selectedFields,
}: {
  schema: GraphQLSchema
  kind: OperationTypeNode
  /** eg. search */
  field: string
  models?: string[]
  ignore?: Ignore
  depthLimit?: number
  circularReferenceDepth?: number
  argNames?: string[]
  selectedFields?: SelectedFields
}): OperationDefinitionNodeGroupType

Given a schema, return a type of "OperationDefinitionNodeGroupType" object.

// demo1
buildOperationNodeForField({
  schema: schema,
  kind: OperationTypeNode.QUERY,
  field: 'adjustBillDetailQuery',
})

img-buildOperationNodeForField

getOperationNodesForFieldAstBySchema

getOperationNodesForFieldAstBySchema(schema: GraphQLSchema): OperationNodesForFieldAstBySchemaReturnType[];

Given a schema, return a type of "OperationNodesForFieldAstBySchemaReturnType[]" array.

// demo1
getOperationNodesForFieldAstBySchema(schema)

img-getOperationNodesForFieldAstBySchema

onSchemaDiffToOperationDefs

function onSchemaDiffToOperationDefs(
  leftSchema: GraphQLSchema,
  rightSchema: GraphQLSchema,
): OnSchemaDiffToOperationDefsItem[]

Given two schemas, return a type of "OnSchemaDiffToOperationDefsItem[]" array.

// demo1
import { buildSchema } from 'graphql'

const typeDefs0 = `#graphql
  # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
  enum AllowedColor {
    RED
    GREEN
    BLUE
  }

  # This "Book" type defines the queryable fields for every book in our data source.
  type Book {
    """
     备注title
    """
    title: String
    wyq: Int
    author: String
    favoriteColor: [AllowedColor]
  }

  input C {
   age:Int 
   hight:String
  }

  input B {
     name: String
     obj: C
  }

input A {
  date: Int!
  remark: B
}

input TaskBoardQueryInput {
  date: Int!
  remark: String
  favoriteColor: [AllowedColor]
  var: A!
}

  type Query {
  """
  分类: s书籍信息
  """
    books(contains: TaskBoardQueryInput): [Book]
  """
  信息: text
  """
    text:Book
    removeName:Int
  }
`
const typeDefs1 = `#graphql
  # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
  enum AllowedColor {
    RED
    GREEN
    BLUE
  }

  # This "Book" type defines the queryable fields for every book in our data source.
  type Book {
    """
     备注title
    """
    title: String
    wyq: Int
    author: String
    favoriteColor: [AllowedColor]
  }

  input C {
   age:Int 
   hight:String
  }

  input B {
     name: String
     obj: C
  }

input A {
  date: Int!
  remark: B
}

input TaskBoardQueryInput {
  date: Int!
  remark: String
  favoriteColor: [AllowedColor]
  var: A!
}

  type Query {
  """
  分类: s书籍信息
  """
    books(contains: TaskBoardQueryInput): [Book]
  """
  信息: text
  """
    text:Int
    addName:Int
  }
`
const leftSchema = buildSchema(typeDefs0)
const rightSchema = buildSchema(typeDefs1)
onSchemaDiffToOperationDefs(leftSchema, rightSchema)

img-onSchemaDiffToOperationDefs

updateOperationDefAst

function updateOperationDefAst(
  leftDefNode: DefinitionNode | FieldNode | InlineFragmentNode,
  rightDefNode: DefinitionNode | FieldNode | InlineFragmentNode,
  remoteConflictingVariablesNames?: ConflictingVariablesNames[],
): DefinitionNode | null

Pass in leftDefNode and rightDefNode, and update leftDefNode to rightDefNode.

// demo1
import {
  parseOperationWithDescriptions,
  printWithComments,
} from '@fruits-chain/qiufen-pro-helpers'

const gql0 = `
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    customer {
      customerId
      customerName
      customerType
    }
  }
}
`

const gql1 = `
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
  }
}
`

const gqlAts0 = parseOperationWithDescriptions(gql0)
const gqlAts1 = parseOperationWithDescriptions(gql1)

const operationDefAst = updateOperationDefAst(
  gqlAts0.definitions[0],
  gqlAts1.definitions[0],
)

const newGql = printWithComments(operationDefAst)
console.log(newGql)

newGql:/*
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    customer {
      customerId
      customerName
      customerType
    }
  }
}
*/
// demo2
import {
  parseOperationWithDescriptions,
  printWithComments,
} from '@fruits-chain/qiufen-pro-helpers'

const gql0 = `
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    customer {
      customerId
      customerName
      customerType
    }
  }
  name11 {
    id
    key
    customer {
      customerId
      customerName
    }
  }
}
`

const gql1 = `
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    title
  }
}
`

const gqlAts0 = parseOperationWithDescriptions(gql0)
const gqlAts1 = parseOperationWithDescriptions(gql1)

const operationDefAst = updateOperationDefAst(
  gqlAts0.definitions[0],
  gqlAts1.definitions[0],
)

const newGql = printWithComments(operationDefAst)
console.log(newGql)

newGql:/*
# 异常订单: 详情
query abnormalOrder($abnormalOrderInput: AbnormalOrderInput) {
  abnormalOrder(abnormalOrderInput: $abnormalOrderInput) {
    abnormalOrderId
    abnormalOrderStatus
    title
  }
  name11 {
    id
    key
    customer {
      customerId
      customerName
    }
  }
}
*/