Git Product home page Git Product logo

Comments (9)

IvanGoncharov avatar IvanGoncharov commented on May 10, 2024 1

@sepehr500 I released support of @example on objects in 1.4.0 🎉
Thank you for reporting this issue and highlighting inconsistency with values 👍

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 10, 2024

@sepehr500 Just to be sure, you trying to write something like this?

type Query {
  allCourtCases: [CourtCase!]!
     @examples(values: [{jurisdiction: "DFT"},{jurisdiction: "FUR"}])
}

type CourtCase {
  jurisdiction: String
}

And you want allCourtCases always return [{jurisdiction: "DFT"},{jurisdiction: "FUR"}]?

If you don't depend on the exact value you can write something like this:

type CourtCase {
  jurisdiction: String @examples(values: ["DFT", "FUR"])
}

If above solution doesn't solve your problem can you please describe your use case in more details?

from graphql-faker.

sepehr500 avatar sepehr500 commented on May 10, 2024

I am trying to do something like this:

 type Query {
  allCourtCases: [CourtCase!]!
     @examples(values: [[{state: "WASH", capital: "SEA"},{state: "VA", capital: "RMD"}]])
}

type CourtCase {
  capital: String
  state: String
}

Sometimes I need certain object properties to always appear together. For example, if I have city and a capital property, I may have logic that makes assumptions about about what other fields can be given some other field, so I would like to be able to mock on an object level. Otherwise, the API would return a random combination of values, and this does not always make sense.

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 10, 2024

@sepehr500 I made @examples to work on objects.
It was pretty simple, however supporting an array of fixed size is way more complicated.
Not sure if it fully solves your issue, please see example below:

type Query {
  allCourtCases: [CourtCase!]!
     @examples(values: [
       # if this object selected 'name' is always null
       {state: "WASH", capital: "SEA", name: null},
       # in this case 'name' is faked as usual
       {state: "VA", capital: "RMD"}
     ])
}

type CourtCase {
  name: String @fake(type:firstName)
  capital: String
  state: String
}

Example of possible result:

{
  "data": {
    "allCourtCases": [
      {
        "name": null,
        "capital": "SEA",
        "state": "WASH"
      },
      {
        "name": "Bria",
        "capital": "RMD",
        "state": "VA"
      },
      {
        "name": null,
        "capital": "SEA",
        "state": "WASH"
      },
      {
        "name": "Guadalupe",
        "capital": "RMD",
        "state": "VA"
      }
    ]
  }
}

Does it solve your issue?
Or you always need an exact number of elements in the array?

from graphql-faker.

sepehr500 avatar sepehr500 commented on May 10, 2024

When I was trying this out it was not working. Do you need to define every property on the object for it to work? Will the ones you don't define not fall through to the fake object property definitions?

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 10, 2024

When I was trying this out it was not working.

I pushed changes to examplesOnObjects a few minutes ago. You can try it there:

npm install
npm start

Do you need to define every property on the object for it to work?

Only properties for which you want fixed values.

Will the ones you don't define not fall through to the fake object property definitions?

By default, they will fall through and would be faked as usual to explicitly prevent this just define the property with null as the values.

from graphql-faker.

sepehr500 avatar sepehr500 commented on May 10, 2024

Looks great! Have not had time to try it yet, but I had question. Given that `values' takes an array of objects that match the properties type, would it make more sense to pass in an array of objects into values like I did in my example? That way if you want to mock a single object, the same pattern of an array of objects would work.

from graphql-faker.

IvanGoncharov avatar IvanGoncharov commented on May 10, 2024

Looks great! Have not had time to try it yet

@sepehr500 I pulished experimental NPM package with this change so you only need to run:

npm install -g [email protected]

Can you please test it and If it solves your problem I will include this change into the next release.

Given that `values' takes an array of objects that match the properties type, would it make more sense to pass in an array of objects into values like I did in my example? That way if you want to mock a single object, the same pattern of an array of objects would work.

Didn't fully understand your question can you please provide some examples?

from graphql-faker.

sepehr500 avatar sepehr500 commented on May 10, 2024

Tried it out, and it works great! Let me rephrase my previous question

Currently, if want to fake a string, I do the following

type CourtCase {
  jurisdiction: String @examples(values: ["DFT", "FUR"])
}

This makes the think that values expects it's value to be an array of whatever the property type is. So it would follow, that if the type was the following,

type CourtCase {
  jurisdiction: [String] @examples(values: [["DFT", "FUR"],["DFS","DNA"])
}

Since the property type is now an array of strings, it would make sense to pass in an array of strings into the array to match the return type. So if we take this same logic an apply it to Objects, then we would expect a property that has a type that is an array of objects, to look like the following,

type Query {
  allCourtCases: [CourtCase!]!
     @examples(values: [[{state: "WASH", capital: "SEA"},{state: "VA", capital: "RMD"}], [{state:"VA"}]])
}

This way, you have a consistent rule that values always expects an array of whatever type the return type of the property is.

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.