oliyh / lacinia-gen

Generators for GraphQL
70 stars 5 forks source link

Full graph generators not working on nested objects: Assert failed: Second arg to fmap must be a generator (generator? gen) #10

Open priyatam opened 5 years ago

priyatam commented 5 years ago

I have a working schema that compiles successfully with lacinia. However lacinia-gen throws multiple errors for both full graph and query result apis, for simple objects and objects with nested and input objects. It's not apparent from the test-check errors what's missing.

(:require [lacinia-gen.core :as lg]
              [lacinia-gen.query :as lg-query]
              [clojure.test.check.generators :as g])

 (def schema (-> "schema.edn" io/resource slurp edn/read-string))

  (let [gen (lg/generator schema)]
    (g/sample (gen :LenderReferral) 1))
Assert failed: Second arg to fmap must be a generator (generator? gen)

           generators.cljc:  100  clojure.test.check.generators$fmap/invokeStatic
           generators.cljc:   91  clojure.test.check.generators$fmap/invoke
                  core.clj:   42  lacinia-gen.core/object/fn/fn
                  core.clj: 7341  clojure.core/keep/fn

For most objects, I get a NullpointerException.

 java.lang.NullPointerException
 core.clj:   95  lacinia-gen.core/generator/fn

It is also not clear whether the 'schema' passed to lacinia-gen is a raw edn schema, or a compiled schema from lacinia (see load-schema)

(defn load-schema
  [component]
  (-> (io/resource "schema.edn"
      slurp
      edn/read-string
      (util/attach-scalar-transformers
        {:parse-datetime parse-datetime
         :serialize-datetime serialize-datetime})
      (util/attach-resolvers (resolve-schema component))
      schema/compile))

I've used both variations and get NPEs with a compiled schema.

With queries, I always get the same error:

{
:objects {

  :Countries
    {:description "List of countries"
     :fields {:countries {:type (list String)}}}
  }

:queries
 {:get_countries {:type :Countries
                  :description "List of countries"
                  :args {:query_str {:type (non-null String)}}
                  :resolve :query/get-countries}}
}
 (let [f (lg-query/generate-fn schema {})]
    (f "query { get_countries { countries } }" {:query_str "IN"}))

Cannot query field get_countries on type QueryRoot ... :extensions {:field get_countries, :type :QueryRoot}

The above query works fine on graphiql.

Any suggestions debugging these errors is much appreciated. I'm also using your re-graph library.

oliyh commented 5 years ago

Hi,

The schema should be the uncompiled schema. It looks like your schema has custom scalars, so are you supplying generators for those custom scalars to lacinia-gen?

The example on the readme shows how to use it if you have custom generators: https://github.com/oliyh/lacinia-gen#custom-scalars

Hope this helps.

oliyh commented 5 years ago

Additionally is your query here correct? Your schema declares that get_countries has a query_str parameter that you are providing as arguments but not supplying in the query, I would expect it to look like this (in it's simplest form):

 (let [f (lg-query/generate-fn schema {})]
    (f "query { get_countries(query_str: \"IN\") { countries } }"))
priyatam commented 5 years ago

Yes, I'm passing scalars, like so, and still get an error:


(let [gen (gen/generator schema {:scalars {:Datetime (local-date-time)}})]
    (g/sample (gen :LenderReferral) 1))

> Resolver specified in schema not provided.
   {:reference :mutation/save-application-data,
    :callbacks (:query/get-lender-options
                :query/get-validation-edits
                :query/get-countries
                :query/get-application-details
                :query/get-profile-lists
                :query/get-drop-downs
                :query/get-member-by-id
                :query/get-profile-options)}
oliyh commented 5 years ago

Hmm, that last error comes from Lacinia, not lacinia-gen. I guess you have :mutations in your schema? I think the issue might be that lacinia-gen is not mocking mutation resolvers and Lacinia is throwing the error. (This is only with lacinia-gen.query, lacinia-gen.core should be ok).

priyatam commented 5 years ago

I removed :mutations :input-objects :enums from the raw schema, and yet get this error:

 Assert failed: Second arg to fmap must be a generator (generator? gen)

           generators.cljc:  100  clojure.test.check.generators$fmap/invokeStatic
           generators.cljc:   91  clojure.test.check.generators$fmap/invoke

To take a step back, I'd be happy just relying on generating object graphs, instead of query api. What do you think is required to provide this support with a working schema.edn? I'd be happy to contribute if you can point to the code.

oliyh commented 5 years ago

Hi,

Can you share a minimum failing schema and I can have a look? Is it your get_countries one in your first post?

Thanks

priyatam commented 5 years ago

Sure, I'll provide this soon ...