Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-null array items in graphql schema #461

Open
zebapy opened this issue May 17, 2022 · 0 comments
Open

non-null array items in graphql schema #461

zebapy opened this issue May 17, 2022 · 0 comments

Comments

@zebapy
Copy link

zebapy commented May 17, 2022

We have an openapi spec endpoint that produces a gql query like this

type Query {
  """
  Equivalent to GET /datasets
  """
  datasets: [Dataset]
}

part of spec

   /datasets:
    get:
      responses:
        200:
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Dataset"
          description: A list of datasets
        400:
          $ref: "#/components/responses/BadRequest"
        401:
          $ref: "#/components/responses/Unauthorized"
        403:
          $ref: "#/components/responses/Forbidden"
        404:
          $ref: "#/components/responses/NotFound"
        500:
          $ref: "#/components/responses/InternalServerError"
      description: Get a list of the datasets available in the developer’s account
      operationId: getDatasets
      summary: List datasets
      tags:
        - datasets

dataset schema

    Dataset:
      type: object
      required:
        - created_at
        - id
        - identity_sets
        - output_to_streams
        - resource_type
        - updated_at
        - upload_directory
      properties:
        created_at:
          type: string
          description: When this resource was created.
          format: date-time
        id:
          type: string
          description: A unique ID for this resource.
          example: 8cd2dcf6-f2b3-4318-b8b3-eb19ab18d29d
          format: uuid
        identity_sets:
          allOf:
            - $ref: "#/components/schemas/IdentitySets"
            - type: object
              description: ""
        incremental_column:
          type: string
          description: ""
          example: updated_at
        output_to_streams:
          allOf:
            - $ref: "#/components/schemas/OutputToStreams"
            - type: object
              description: ""
        resource_type:
          type: string
          description: The type of this resource.
          example: datasets
        updated_at:
          type: string
          description: When this resource was last updated.
          format: date-time
        upload_directory:
          type: string
          description: ""
          example: abc_orders
          pattern: "^[A-Za-z0-9_][-A-Za-z0-9_ ]*$"
        upsert_columns:
          type: array
          items:
            type: string
          description: ""
          example:
            - id
      additionalProperties: false
      description: "Tabular data describing orders, customers, leads, etc."

But we never send back [null] arrays when fetching this data.

How can we ensure the schema generates as datasets: [Dataset!] instead? Either the object is in the array or it is an empty array.

My understanding is unless our spec explicitly says "this object can be null", then perhaps this is a bug?

A workaround for this is transforming the schema with graphql-tools https://www.graphql-tools.com/docs/schema-wrapping#modifying

const schema = wrapSchema({
    schema: originalSchema,
    transforms: [
      // convert all lists from [MyType] to [MyType!]
      new TransformRootFields((_operationName, _fieldName, fieldConfig) => {
        if (fieldConfig.type instanceof GraphQLList) {
          const item = fieldConfig.type.ofType;

          // if it's already nonnull, skip changing it
          if (item instanceof GraphQLNonNull) return fieldConfig;

          fieldConfig.type = new GraphQLList(new GraphQLNonNull(item));
        }

        return fieldConfig;
      }),
    ],
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant