graphql-faker icon indicating copy to clipboard operation
graphql-faker copied to clipboard

Expose a JS API (for use within Node.JS / browser?)

Open samdenty opened this issue 6 years ago • 8 comments

This tool would be super-useful inside tests, but it currently requires starting a HTTP server - which is not ideal.

If the API provided a way to provide a seed to the randomness, it would allow for the predictability required in tests

samdenty avatar Mar 18 '19 17:03 samdenty

@samdenty You can try to use fakeSchema directly: https://github.com/APIs-guru/graphql-faker/blob/master/src/fake_schema.ts#L66 Just build your SDL with buildSchema before passing it to fakeSchema.

IvanGoncharov avatar Mar 18 '19 19:03 IvanGoncharov

@samdenty You can try to use fakeSchema directly: https://github.com/APIs-guru/graphql-faker/blob/master/src/fake_schema.ts#L66 Just build your SDL with buildSchema before passing it to fakeSchema.

fakeSchema isn't exposed through the lib though. You can import it directly through const fakeSchema = require('graphql-faker/dist/fake_schema').fakeSchema but it doesn't seem to work with a basic use case.

jsphweid avatar Jul 08 '19 14:07 jsphweid

@jsphweid See here for a example of how I used it, for Jest tests

samdenty avatar Jul 09 '19 05:07 samdenty

How comfortable are the maintainers with people using this API directly? This is exactly what I want, happy to help out making it a fully supported API if needs be.

TimPerry avatar Sep 05 '19 08:09 TimPerry

@TimPerry I'm in a process of switching to the new internal API based on default resolvers: https://github.com/graphql/graphql-js/pull/1332 After that is done I have no problem if someone export and document default field and type resolvers.

IvanGoncharov avatar Sep 05 '19 10:09 IvanGoncharov

Awesome, sounds great.

TimPerry avatar Sep 05 '19 11:09 TimPerry

@TimPerry As promised, I switched internal implementation to expose fakeTypeResolver & fakeFieldResolver in 6b3e743e59977c7437f395efc5b2a0fde45f1151 and released it 2.0.0-rc.8 📦

IvanGoncharov avatar Sep 15 '19 17:09 IvanGoncharov

@IvanGoncharov It would be handy if you could export a function from the index, eg.

import { seed } from 'faker'

export const executeQuery = (idlPath: string, query: DocumentNode, variables?): Promise<{data,errors}> => {
seed(123)
}

so we don't have to directly use private APIs

samdenty avatar Oct 03 '19 11:10 samdenty