GithubHelp home page GithubHelp logo

Comments (18)

IvanGoncharov avatar IvanGoncharov commented on May 14, 2024 16

@ahmedlhanafy Not ATM, it's just a proposed solution.
I will try to find time to implement it this month

from graphql-faker.

hotsezus avatar hotsezus commented on May 14, 2024 12

I made NPM module based on my PR which enables fields overriding.
If you need this feature in main module, you can subscribe to my PR's notifications and use my fork-module until it gets merged.

https://www.npmjs.com/package/@proscom/graphql-faker

from graphql-faker.

mxstbr avatar mxstbr commented on May 14, 2024 9

just to use the proxy mode where some data are coming from actual API and some are mocked locally

That's what I want!

# Remote schema
type User {
  username: String!
}
# Local schema
extend type User {
  # ...some more fields...
  username: String! @fake(type: username)
}

Sounds like @override would work for my use case but I am not sure why I even need to manually specify thatβ€”if it's in the local schema, just override it instead of throwing an error?!

from graphql-faker.

mxstbr avatar mxstbr commented on May 14, 2024 6

This is unfortunately a deal-breaker for us, I would love to use this package but not being able to return mock data for existing fields is very annoying. Is there any workaround for this nowadays?

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 14, 2024 6

@mxstbr Do you want to mock the entire GraphQL API or just to use the proxy mode where some data are coming from actual API and some are mocked locally?

How about @override directive, e.g.:

extend type Post {
  acticle: String! @override @fake(type: lorem, options: {loremSize: paragraphs})
}

Will it work in your use case?

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 14, 2024 3

just override it instead of throwing an error?!

@mxstbr In this case, we are changing the behavior of extend as defined in GraphQL specification and implemented in any other GraphQL tool/lib.
So I don't want to enable this functionality by default.
How about using --override CLI Flag instead of individually marking fields?

from graphql-faker.

rostislav-simonik avatar rostislav-simonik commented on May 14, 2024 1

@IvanGoncharov would like to check if you will have time to look at this any soon. Thx

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 14, 2024

@blackxored I want this tool to be compliant with GraphQL spec and graphql-js so I can't change how extendSchema works. We also design extend mode to be used for applying non-breaking changes to existing GraphQL API and your use case is really different from that.

Hower, I have an idea how to accommodate your use case without introducing too much complexity into graphql-faker. In source code we have fakeSchema function that accepts schema and attaches resolvers to it. I can expose it as an NPM package so you would be able to write:

import { fakeSchema } from 'graphql-faker';
import { makeExecutableSchema } from 'graphql-tools';
const typeDefs = `
  type User {
    id: ID!
    firstName: String @fake(type: firstName)
    lastName: String @fake(type: firstName)
  }
`;
const resolvers = {
  User: {
     id() {
        // ...
     }
  }
};
const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

// Assing missing resolvers
fakeSchema(executableSchema);

// Pass executableSchema to your server

Will it solve your use case?

from graphql-faker.

liuchong avatar liuchong commented on May 14, 2024

Seems we need to keep multiple version of schema in this case, maybe we could added a "patcher" function.

In fact, I've just defined another fake version of the exact same type and append it onto the end of the original schema, and it works...like:

type Post {
  id: ID!
  article: String!
}

type Post {
  id: ID! @fake(type: uuid)
  article: String! @fake(type: lorem, options: {loremSize: paragraphs})
}

by the way, I find this "fakeSchema" πŸ‘ solves my issue: #32

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 14, 2024

Seems we need to keep multiple version of schema in this case, maybe we could added a "patcher" function.

@liuchong All GraphQL tools (including graphql-tools) I'm aware of will silently ignore unknown directives, so you can safely use one schema.

by the way, I find this "fakeSchema" πŸ‘ solves my issue: #32

Implementing it requires me to switch from custom fork of graphql-js to official version + a lot of refactoring. I plan to start working on v2.0.0 release next month.
I will ping you after initial refactoring is done so you could help with PR if you have time for it.

from graphql-faker.

liuchong avatar liuchong commented on May 14, 2024

@IvanGoncharov Great, thanks!

from graphql-faker.

mxstbr avatar mxstbr commented on May 14, 2024

That sounds great to me!

from graphql-faker.

ahmedlhanafy avatar ahmedlhanafy commented on May 14, 2024

@IvanGoncharov Are @override or --override available ATM?

from graphql-faker.

adaam2 avatar adaam2 commented on May 14, 2024

Maybe I'm misunderstanding significantly πŸ˜† , but why would you want to define new fields via extend that don't exist in your original (remote) schema?

I would think that that main use case for this library is to mock existing schema field data?

πŸ‘€

from graphql-faker.

steezeburger avatar steezeburger commented on May 14, 2024

@adaam2
I'd like to use graphql-faker to mock the api for new features so the front end can implement a feature without all backend work being done. Sometimes new features require adding fields to existing types. For example, after creating a new Course type, I'd want to add a courses field to Contact:

type Course {
  id: ID! @fake(type: uuid)
  title: String @fake(type: word)
  description: String @fake(type: lorem)
}

extend type Contact {
  courses: [Course!] @listLength(min: 1, max: 1)
}

from graphql-faker.

adaam2 avatar adaam2 commented on May 14, 2024

@steezeburger fair enough, but i think there's a place for both use cases here :)

from graphql-faker.

orefalo avatar orefalo commented on May 14, 2024

Wouaou... this issue was open back in 2017 and there is still no solution?
It's a pretty common use case, judging by the people who interacted on this thread.

I am also looking at the @OverRide or { fakeSchema } solution -

Sincerely

from graphql-faker.

supaspoida avatar supaspoida commented on May 14, 2024

This is exactly my use case as well. Have an existing GraphQL schema from a Hasura instance that I want to use to generate faked seed data to feed into mutations.

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.