GithubHelp home page GithubHelp logo

Comments (4)

johannchopin avatar johannchopin commented on May 29, 2024 1

@IvanGoncharov I already implement the script for my use case so there won't be a lot of work there for you. What do you think of this idea? Could I have a green light to open a PR?

from graphql-faker.

johannchopin avatar johannchopin commented on May 29, 2024 1

Sadly the maintainers (@IvanGoncharov, @RomanHotsiy) aren't really active on this repo so I won't waste time by opening PR. Here is the simple getSchemaFromContent helper that takes a faker shema string as a parameter and return it without the faker directives:

const FAKER_DIRECTIVE_MATCHER = / @fake\((.*?)\)\n/g
const EXAMPLES_DIRECTIVE_MATCHER = / @examples\(values: \[(.*?)\]\)\n/g
const LISTLENGTH_DIRECTIVE_MATCHER = / @listLength\((.*?)\)\n/g
const MATCHERS = [FAKER_DIRECTIVE_MATCHER, EXAMPLES_DIRECTIVE_MATCHER, LISTLENGTH_DIRECTIVE_MATCHER]

const replaceAll = (str: string, find: string, replace: string): string => {
  return str.split(find).join(replace)
}

const getAllFakerDirectivesFromContent = (content: string): string[] => {
  let directives: string[] = []
  MATCHERS.forEach((matcher) => {
    directives = [...directives, ...Array.from(content.matchAll(matcher), (m) => { return m[0] })]
  })

  return directives
}

export const getSchemaFromContent = (content: string): string => {
  const directives = getAllFakerDirectivesFromContent(content)

  directives.forEach((directive) => {
    content = replaceAll(content, directive, '\n')
  })

  return content
}

Hope it will help you @ojczeo 👍

from graphql-faker.

ojczeo avatar ojczeo commented on May 29, 2024

@johannchopin that's sounds interesting. Could you share it somewhere?

from graphql-faker.

ojczeo avatar ojczeo commented on May 29, 2024

@johannchopin thanks man!

from graphql-faker.

Related Issues (20)

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.