Git Product home page Git Product logo

Comments (4)

bengro avatar bengro commented on June 27, 2024 3

Nice one, @jhodges10 👍 !

I wrote my own gatsby plugin because we rely on OpenApi Spec 3.0 features (hence transforming to OpenApi Spec 2.0 wasn't an option). Unfortunately I cannot share the code as it's closed source, but here the gist for peeps in a similar situation.

The hardest part was building the GraphQL model correctly, which is shown here:

import { parseOpenApi3 } from "./api-parser/openapi3parser"
import { ApiOperation } from "./api-parser/types/ApiOperation"

export interface Options {
  specs: string[]
}

exports.sourceNodes = async ({ actions }: any, options: Options) => {
  const allOperations: ApiOperation[] = []

  for (let spec of options.specs) {
    const operations = await parseOpenApi3(spec)

    for (let operation of operations) {
      allOperations.push(operation)
    }
  }

  allOperations.forEach((operation: ApiOperation) => {
    actions.createNode({
      id: operation.path, // assigning a unique id through helper function
      children: [], // no children at this point
      parent: undefined, // no parent
      internal: {
        type: "ApiOperation", // A globally unique name for this node type
        contentDigest: toHashCode(JSON.stringify(operation)),
      },
      ...operation,
    })
  })
}

const toHashCode = (input: string): string => {
  let h: number = 0

  for (let i = 0; i < input.length; i++) {
    h = (Math.imul(31, h) + input.charCodeAt(i)) | 0
  }

  return h.toString()
}

The parsing of the spec is delegating to @apidevtools/swagger-parser.

from gatsby-source-openapi-aggregate.

devdigital avatar devdigital commented on June 27, 2024 1

Hi, thanks, that's been a plan for a while now, but currently struggling for time, so I would imagine it would be a couple of months away at least unfortunately.

from gatsby-source-openapi-aggregate.

jhodges10 avatar jhodges10 commented on June 27, 2024

+1 on support for OAS 3.0!

from gatsby-source-openapi-aggregate.

jhodges10 avatar jhodges10 commented on June 27, 2024

Figured out a solution for the meantime @bengro!

npm install -g api-spec-converter

Then in your package.json, add this task:

"transform-docs": "api-spec-converter --from=openapi_3 --to=swagger_2 --syntax=json --order=alpha [Your OAS spec URL] > ./src/data/swagger.json"

Then also add this before your dev / build phases:

    "dev": "yarn transform-docs && gatsby develop",
    "build": "yarn transform-docs && gatsby build",

from gatsby-source-openapi-aggregate.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.