Gqlgen: Schema merging

Created on 16 Apr 2019  路  5Comments  路  Source: 99designs/gqlgen

I would like to be able to have my queries/mutations in separate files and merge them while generation. I see that there is schema stitching, which i do use, but is there anyway i can have query type in more than one file and merge them together?

Thanks!

support

Most helpful comment

In order to have a valid schema across multiple files you need to use Type extensions

So define your main Query type somewhere, then in your other files:

extend type Query {
    // additional fields
}

All 5 comments

This is already supported - you can provide multiple schema files in the gqlgen config, see https://gqlgen.com/config/ for more details

hi @lwc i tried having two schemas with type Query in them and i get

modelgen: privsector.graphql: Cannot redeclare type Query.

This is how i have my gqlgen.yaml file:

schema:
- geojson.graphql
- schema.graphql
- privsector.graphql

And this is how i try to generate:

func main() {
    fmt.Fprintln(os.Stderr, "Generating graphql code ...")
    log.SetOutput(os.Stderr)

    config, err := config.LoadConfigFromDefaultLocations()
    if err != nil {
        die("load config: %v", err)
    }

    if err = api.Generate(config); err != nil {
        die("codegen: %v", err)
    }

    fmt.Fprintln(os.Stderr, "Done generating graphql code.")
}

These are my schemas:

type SampleType{
  sample: String
}

type Query {
  querySampleA: SampleType
}
type Query {
  querySampleB: SampleType
}

In order to have a valid schema across multiple files you need to use Type extensions

So define your main Query type somewhere, then in your other files:

extend type Query {
    // additional fields
}

Thank you! that worked!! 馃挴

What about having an interface type defined in one schema file, and implementing it in the other? Does give me a undefined type at the moment?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ksoda picture ksoda  路  3Comments

lynntobing picture lynntobing  路  3Comments

coderste picture coderste  路  3Comments

jszwedko picture jszwedko  路  3Comments

steebchen picture steebchen  路  3Comments