oliyh / lacinia-gen

Generators for GraphQL
70 stars 5 forks source link

Cannot generate values for interface types #13

Open colinphill-mdsol opened 4 days ago

colinphill-mdsol commented 4 days ago

I think this might explain some of the other reported issues. The library appears to simply not support the concept of an interface. In this schema, you can generate values of Person, but not of NameHolder, because the latter has a field whose type is an interface.

Schema:

type NameHolder {
    named: Named
}

interface Named {
    name: String
}

type Person implements Named {
    name: String,
    age: Int
}

type Query {
    nameHolder: NameHolder
}

schema {
    query: Query
}

Code:

(ns repro
  (:require [clojure.java.io :as io]
            [clojure.test.check.generators :as gen]
            [com.walmartlabs.lacinia.parser.schema :as parser]
            [lacinia-gen.core :as lgen]))

(def ^:private schema
  (-> (io/resource "repro.graphql")
      slurp
      parser/parse-schema))

(gen/sample ((lgen/generator schema) :NameHolder))

Result:

Execution error (AssertionError) at clojure.test.check.generators/fmap (generators.cljc:103).
Assert failed: Second arg to fmap must be a generator
(generator? gen)
Evaluation of file repro.clj failed: class clojure.lang.Compiler$CompilerException
colinphill-mdsol commented 4 days ago

Ah, appears the same issue happens with unions.

type StrBox {
    value: String
}
type IntBox {
    value: Integer
}
union StrIntBox = StrBox | IntBox
type StrIntBoxBox {
    value: StrIntBox
}
(gen/sample ((lgen/generator schema) :StrIntBoxBox))
Execution error (AssertionError) at clojure.test.check.generators/fmap (generators.cljc:103).
Assert failed: Second arg to fmap must be a generator
(generator? gen)
oliyh commented 3 days ago

Hi,

Thanks for reporting this, and the reproduction. I'm not sure when I'll be able to get around to looking at this but in the meantime PRs are welcome.

Cheers