graphql#ListValueNode TypeScript Examples

The following examples show how to use graphql#ListValueNode. 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: generate-query.ts    From graphql-query-generator with MIT License 5 votes vote down vote up
/**
 * Returns the first slicing argument defined in the field's @listSize
 * directive, if:
 *  - The @listSize directive is indeed present, and defines slicing arguments
 *  - The requiredArguments do not already include any of the defined slicing
 *    arguments
 *  - The @listSize directive doesn't also set requireOneSlicingArgument to
 *    false
 *
 * TODO: add link to specification / documentation of @listSize directive
 */
function getMissingSlicingArg(
  requiredArguments: InputValueDefinitionNode[],
  field: FieldDefinitionNode,
  schema: GraphQLSchema
): InputValueDefinitionNode {
  // Return null if there is no @listSize directive:
  const listSizeDirective = getListSizeDirective(field)
  if (!listSizeDirective) return null

  // Return null if @listSize directive defines no slicing arguments:
  const slicingArgumentsArg = getSlicingArguments(listSizeDirective)
  if (!slicingArgumentsArg) return null

  // Return null if requireOneSlicingArgument is set to false:
  const requireOneSlicingArg = getRequireOneSlicingArgument(listSizeDirective)
  if (
    requireOneSlicingArg &&
    (requireOneSlicingArg.value as BooleanValueNode).value === false // already checked requireOneSlicingArg is a boolean
  )
    return null

  // Return null if a slicing argument is already used:
  const slicingArguments = (slicingArgumentsArg.value as ListValueNode).values // already checked slicingArgumentsArg is a list
    .filter((value) => value.kind === 'StringValue')
    .map((value) => (value as StringValueNode).value)
  const usesSlicingArg = slicingArguments.some((slicingArg) =>
    requiredArguments
      .map((existing) => existing.name.value)
      .includes(slicingArg)
  )
  if (usesSlicingArg) return null

  // Returns the first slicing argument if slicing argument can be split on '.'
  // then we just need to check the first level match
  return field.arguments.find((arg) => {
    return slicingArguments.find((slicingArgument) => {
      const tokenizedPath = slicingArgument.split('.')
      if (tokenizedPath.length < 1) return false

      return arg.name.value === tokenizedPath[0]
    })
  })
}
Example #2
Source File: generate-query.ts    From graphql-query-generator with MIT License 5 votes vote down vote up
/**
 * For a given listSize directive, return a default value for it
 */
function getListSizeDirectiveDefaultValue(
  listSizeDirective: DirectiveNode,
  typeNode: TypeNode,
  config: InternalConfiguration,
  schema: GraphQLSchema
) {
  // if requiredOneSlicingArg is false then we do not add anything because none of the slicingArguments are required
  const requireOneSlicingArg = getRequireOneSlicingArgument(listSizeDirective)
  if (
    requireOneSlicingArg &&
    (requireOneSlicingArg.value as BooleanValueNode).value === false
  ) {
    return undefined
  }

  const slicingArgumentsArg = getSlicingArguments(listSizeDirective)
  if (!slicingArgumentsArg) return undefined

  /**
   * Already checked slicingArgumentsArguments is a list
   *
   * Extract paths from slicingArgumentsArguments
   */
  const slicingArgumentsPaths = (slicingArgumentsArg.value as ListValueNode).values.map(
    (value) => {
      if (value.kind === 'StringValue') return value.value
    }
  )

  if (slicingArgumentsPaths.length > 0) {
    const slicingArgumentsTokenizedPath = slicingArgumentsPaths[0].split('.')
    return getListSizeDirectiveDefaultValueHelper(
      slicingArgumentsTokenizedPath,
      typeNode,
      config,
      schema
    )
  }
}